-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathAvlTree(平衡树).js
175 lines (171 loc) · 4.63 KB
/
AvlTree(平衡树).js
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
function AvlTree(key){
this.left = null;
this.right = null;
this.depth = 1;
this.nodeCount = 1;
this.key = typeof(key) == "number" ? key : null;
}
AvlTree.prototype.getDepth = function(obj){
if(obj == null)
return 0;
return obj.depth;
}
AvlTree.prototype.update = function(){
this.depth = 1;
this.nodeCount = 1;
if(this.left != null){
this.depth = this.left.depth + 1;
this.nodeCount += this.left.nodeCount;
}
if(this.right != null){
if(this.depth <= this.right.depth)
this.depth = this.right.depth + 1;
this.nodeCount += this.right.nodeCount;
}
return;
}
AvlTree.prototype.rotateRight = function(){
var preKey = this.key;
var preRight = this.right;
this.key = this.left.key;
this.right = this.left;
this.left = this.left.left;
this.right.left = this.right.right;
this.right.right = preRight;
this.right.key = preKey;
this.right.update();
this.update();
return;
}
AvlTree.prototype.rotateLeft = function(){
var preKey = this.key;
var preLeft = this.left;
this.key = this.right.key;
this.left = this.right;
this.right = this.right.right;
this.left.right = this.left.left;
this.left.left = preLeft;
this.left.key = preKey;
this.left.update();
this.update();
return;
}
AvlTree.prototype.balance = function(){
var depthL = this.getDepth(this.left);
var depthR = this.getDepth(this.right);
if(depthL > depthR+1){
var depthLL = this.getDepth(this.left.left);
var depthLR = this.getDepth(this.left.right);
if(depthLL < depthLR)
this.left.rotateLeft();
this.rotateRight();
}
else if(depthR > depthL+1){
var depthRL = this.getDepth(this.right.left);
var depthRR = this.getDepth(this.right.right);
if(depthRR < depthRL)
this.right.rotateRight();
this.rotateLeft();
}
}
AvlTree.prototype.compare = function(a, b){
if(a < b)
return -1;
if(a > b)
return 1;
return 0;
}
AvlTree.prototype.insert = function(key){
var rst = this.compare(key, this.key);
if(rst == 0){
return false;
}
var ret = false;
if(rst == -1){
if(this.left == null){
this.left = new AvlTree(key);
ret = true;
}
else{
ret = this.left.insert(key);
if(ret)
this.balance();
}
}
else if(rst == 1){
if(this.right == null){
this.right = new AvlTree(key);
ret = true;
}
else{
ret = this.right.insert(key);
if(ret)
this.balance();
}
}
if(ret)
this.update();
return ret;
}
AvlTree.prototype.erase = function(key, _parent_){
var rst = this.compare(key, this.key);
if(rst == 0){
if(this.left == null && this.right == null){
if(this == _parent_.left)
_parent_.left = null;
else
_parent_.right = null;
return;
}
var depthL = this.getDepth(this.left);
var depthR = this.getDepth(this.right);
if(depthL >= depthR){
var depthLL = this.getDepth(this.left.left);
var depthLR = this.getDepth(this.left.right);
if(depthLL < depthLR)
this.left.rotateLeft();
this.rotateRight();
this.right.erase(key, this);
}
else{
var depthRL = this.getDepth(this.right.left);
var depthRR = this.getDepth(this.right.right);
if(depthRR < depthRL)
this.right.rotateRight();
this.rotateLeft();
this.left.erase(key, this);
}
}
if(rst == -1)
this.left.erase(key, this);
if(rst == 1)
this.right.erase(key, this);
this.balance();
}
AvlTree.prototype.sort = function(){
var ret = [];
if(this.left != null)
ret = ret.concat(this.left.sort());
ret = ret.concat(this.key);
if(this.right != null)
ret = ret.concat(this.right.sort());
return ret;
}
AvlTree.prototype.findByRank = function(k){
var leftCount = this.left == null ? 0 : this.left.nodeCount;
if(leftCount == k)
return this;
if(leftCount < k)
return this.right.findByRank(k-leftCount-1);
if(leftCount > k)
return this.left.findByRank(k);
}
AvlTree.prototype.findByKey = function(k){
var rst = this.compare(k, this.key);
if(rst == 0)
return this;
if(rst == -1)
return this.left.findByKey(k);
if(rst == 1)
return this.right.findByKey(k);
}