-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathindex1.html
executable file
·305 lines (289 loc) · 8.18 KB
/
index1.html
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<htmleft lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<script>
class Node {
constructor(data) {
this.data = data;
this.size = 1
this.left = null;
this.right = null;
this.count = 1;
this.parent = null;
}
maintain() {
this.size = this.count;
if (this.left) {
this.size += this.left.size;
}
if (this.right) {
this.size += this.right.size;
}
}
};
function rotateImpl(tree, node, dir, setParent) {
var other = dir == "left" ? "right" : "left";
if (!node[other]) {
return;
}
var top = node[other]; //会上浮的子节点
node[other] = top[dir]; //过继孩子
if (setParent) {
if (!node.parent) {
tree.root = top;
} else if (node == node.parent.left) {
node.parent.left = top;
} else {
node.parent.right = top;
}
Object(top[dir]).parent = node; //父属性修正1
top.parent = node.parent; //父属性修正2
node.parent = top; //父属性修正3
}
top[dir] = node; //旋转
node.maintain(); //先处理下面的再处理上面的
top.maintain();
return top;
}
class SBT {
constructor() {
this.root = null;
}
leftRotate(node) {
return rotateImpl(this, node, "left", true);
}
rightRotate(node) {
return rotateImpl(this, node, "right", true);
}
getSize(node) {
return node ? node.size : 0
}
maintain(node, rightDeeper) {
if (!node) {
return
}
var left = node.left;
var right = node.right;
if (!rightDeeper) {
if (!left) {
return
}
var rightSize = this.getSize(right)
var llSize = this.getSize(left.left)
var lrSize = this.getSize(left.right)
if (llSize > rightSize) {
this.rightRotate(node);
} else if (lrSize > rightSize) {
this.leftRotate(left);
this.rightRotate(node);
} else {
return;
}
} else {
if (!right) {
return
}
var leftSize = this.getSize(left)
var rrSize = this.getSize(right.right)
var rlSize = this.getSize(right.left)
if (rrSize > leftSize) {
this.leftRotate(node);
} else if (rlSize > leftSize) {
this.rightRotate(right);
this.leftRotate(node);
} else {
return;
}
}
this.maintain(left, false);
this.maintain(right, true);
this.maintain(node, false);
this.maintain(node, true);
}
find(data) {
var node = this.root;
while (node) {
var diff = data - node.data
if (diff == 0) {
break
} else if (diff < 0) {
node = node.left;
} else {
node = node.right;
}
}
if (node) {
return node
}
return null
}
insert(data) {
if (!this.root) {
this.root = new Node(data);
return true;
}
var node = this.root,
parent
while (node) {
var diff = data - node.data;
parent = node;
if (diff == 0) {
return false;
} else if (diff < 0) {
node = node.left;
} else {
node = node.right;
}
}
var node = new Node(data);
node.parent = parent
if (diff < 0) {
parent.left = node;
} else {
parent.right = node;
}
while (parent) {
parent.size++;
this.maintain(parent, data >= parent.data);
parent = parent.parent;
}
return true;
}
remove(data) {
if (!this.root) {
return false
}
var node = this.find(data);
if (node) {
//两个孩子的情况
if (node.left && node.right) {
var succ = this.maxNode(node.left); //求后继
node.data = succ.data;
node = succ; //转为一个孩子的情况
}
//一个或零个孩子的情况
var child = node.left || node.right || null;
var parent = node.parent;
if (parent.left == node) {
parent.left = child
} else {
parent.right = child
}
if (child) {
child.parent = parent; //parent的size发生变化
}
while (parent) {
parent.size--
this.maintain(parent, data >= parent.data);
parent = parent.parent
}
}
}
maxNode(node) {
var current = node || this.root;
while (current.right) {
current = node.right
}
return current;
}
getRank(value) {
var node = this.find(value);
if (node) {
return this.getSize(node.left) + 1;
} else {
return 0;
}
}
getKth(k) {
var node = this.root;
while (node) {
if (k <= this.getSize(node.left)) {
node = node.left;
} else if (k > this.getSize(node.left) + node.count) {
k -= this.getSize(node.left) + node.count;
node = node.right;
} else {
return node.data;
}
}
return null;
}
inOrder(cb) {
function recursion(node) {
if (node) {
recursion(node.left);
cb(node);
recursion(node.right);
}
}
recursion(this.root);
}
keys() {
var ret = [];
this.inOrder(function (p) {
ret.push(p.data);
});
return ret;
}
toString(printNode) {
if (printNode === void 0) printNode = function (n) {
return n.data;
};
var out = [];
printRow(this.root, '', true, function (v) {
return out.push(v);
}, printNode);
return out.join('');
};
}
function printRow(root, prefix, isTail, out, printNode) {
if (root) {
out(("" + prefix + (isTail ? '└── ' : '├── ') + (printNode(root)) + "\n"));
var indent = prefix + (isTail ? ' ' : '│ ');
if (root.left) {
printRow(root.left, indent, false, out, printNode);
}
if (root.right) {
printRow(root.right, indent, true, out, printNode);
}
}
}
var array = [7, 11, 13, 8, 44, 78, 15, 9, 77, 89, 1, 2] //[11,7,14,3,9,18,16,15]
var t = new SBT();
array.forEach(function (el) {
t.insert(el)
})
var rank = t.find(44)
console.log('t.find(44)', rank, t)
var a = t.getRank(44)
console.log('t.getRank(44)', a);
var b = t.getKth(a)
console.log(`t.getKth(${a})`, b);
t.remove(11);
t.remove(44);
t.remove(77);
console.log("移除11,44,77后");
console.log(t.keys(1) + "");
t.insert(11)
t.insert(43)
t.insert(76)
t.insert(77)
var tree = new SBT();
[7, 11, 13, 8, 44, 78, 15, 9, 77, 89, 1, 2, 52, 51, 56].forEach(function (el, i) {
tree.insert(el)
console.log(tree + "")
})
console.log("delete...")
tree.remove(60)
console.log(tree + "")
console.log(tree)
</script>
<div id="root"></div>
</body>
</html>