&web_id=" language="JavaScript">
Fork me on GitHub

JavaScript文件夹模拟【代码笔记】

这是这次大实验的编码实现,由于匆忙来不及做可视化。尽管如此我会抽出时间进行可视化的操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function Node(element,child,parent,brother){
this.data = element;
this.child = child;
this.parent = parent;
this.brother = brother;
this.show = show;
this.flog = 0;
}
function show(){
return this.data;
}
function BST(){
this.root = new Node('我的电脑',null,null,null);
this.insert = insert;
this.remove = remove;
this.find = find;
this.clearFind = clearFind;
this.stick = stick;
this.rename = rename;
}
function find(element){
var root = this.root;
if(root.child == null){
console.log("本机无文件夹,无法查找你所需要的文件夹!");
}
else{
var currout = root;
while(1){
if((currout.child != null)&&(currout.child.flog != 1)){
currout.flog = 1;
currout = currout.child;
}
else{
if(currout.data == element){
console.log("查找成功!节点为:" + currout);
return currout;
}
else{
currout.flog = 1;
if(currout == this.root){
console.log("本机没有你想要查找的文件夹,请确定输入文件夹名称正确!");
return;
}
else{
if(currout.brother != null){
currout = currout.brother;
}
else{
currout = currout.parent;
}
}
}
}
}
}
}
function clearFind(){
var root = this.root;
if(root.child == null){
console.log("本机无文件夹,无法清除文件夹标记!");
}
else{
var currout = root;
var count = 0;
while(1){
if((currout.child != null)&&((count != 1)||(currout.child.flog == 1))){
currout = currout.child;
}
else{
count = 1;
if(currout == this.root){
console.log("清除成功!");
return;
}
else{
currout.flog = 0;
if(currout.brother != null){
currout = currout.brother;
}
else{
currout = currout.parent;
}
}
}
}
}
}
function insert(parentNodeElement,element){
var newNode = new Node(element,null,null,null);
var parent = this.find(parentNodeElement);
this.clearFind();
if(parent.child == null){
parent.child = newNode;
newNode.parent = parent;
}
else{
newNode.brother = parent.child;
parent.child = newNode;
newNode.parent = parent;
}
}
function remove(parentNodeElement,element){
var parent = this.find(parentNodeElement);
this.clearFind();
var currout = parent.child;
var Brother;
while(1){
if(parent.child.data == element){
parent.child = parent.child.brother;
return;
}
else{
Brother = currout;
currout = currout.brother;
if(currout.data == element){
Brother.brother = currout.brother;
return;
}
}
}
}
function stick(oldParentNodeElement,newParentNodeElement,element){
var oldold = this.find(oldParentNodeElement);
this.clearFind();
var newnew = this.find(newParentNodeElement);
this.clearFind();
var findNode = this.find(element);
this.clearFind();
if((oldold.child == findNode)&&(findNode.brother == null)){
findNode.brother = newnew.child;
newnew.child = findNode;
findNode.parent = newnew;
oldold.child = null;
}
else if((oldold.child == findNode)&&(findNode.brother != null)){
if(newnew.child == null){
newnew.child = findNode;
oldold.child = findNode.brother;
findNode.brother = null;
findNode.parent = newnew;
}
else{
var currout = oldold.child;
while(1){
if(currout.brother != null){
currout = currout.brother;
}
else{
currout.brother = findNode;
oldold.child = findNode.brother;
findNode.brother = null;
findNode.parent = newnew;
return;
}
}
}
}
else if((oldold.child != findNode)){
while(1){
var currout1 = oldold.child;
var father = null;
if(currout1.brother != findNode){
father = currout1;
currout1 = currout1.brother;
}
else{
father.brother = findNode.brother;
findNode.brother = newnew.child;
newnew.child = findNode;
findNode.parent = newnew;
return;
}
}
}
}
function remove(element,newElement){
var oldold = this.find(element);
this.clearFind();
oldold.data = newElement;
}
Godlike Meteor wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
我知道不会有人点开,但万一真有人想不开呢?
------ 本文结束 ------