-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL.java
240 lines (223 loc) · 4.53 KB
/
AVL.java
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
import java.util.*;
public class AVL<X extends Comparable<X>>
{
Node root;
class Node
{
X data;
Node left, right, parent;
int height;
public Node(X d)
{
data = d;
left = right = parent = null;
height = 0;
}
}
private Node find(Node r, X d)
{
if(r == null) return null;
if(r.data.compareTo(d) == 0)
return r;
if(r.data.compareTo(d) < 0)
return find(r.right, d);
return find(r.left, d);
}
public boolean isMember(X d)
{
return (find(root, d) != null);
}
private Node bstInsert(Node r, Node d)
{
if(r == null) return d;
if(d.data.compareTo(r.data) <= 0)
{
Node tmp = bstInsert(r.left, d);
tmp.parent = r;
r.left = tmp;
}
else
{
Node tmp = bstInsert(r.right, d);
tmp.parent = r;
r.right = tmp;
}
return r;
}
private int newHeight(Node n)
{
if(n == null) return -1;
int l,r;
if(n.left != null) l = n.left.height;
else l = -1;
if(n.right != null) r = n.right.height;
else r = -1;
return 1 + Math.max(l, r);
}
private boolean isImbalanced(Node n)
{
if(n == null) return false;
int l,r;
if(n.left != null) l = n.left.height;
else l = -1;
if(n.right != null) r = n.right.height;
else r = -1;
return (Math.abs(l-r) > 1);
}
private Node higherChild(Node n)
{
if(n == null) return null;
if(n.right == null && n.left == null) return null;
if(n.right == null) return n.left;
if(n.left == null) return n.right;
if(n.left.height >= n.right.height) return n.left;
return n.right;
}
public void Insert(X d)
{
Node n = new Node(d);
root = bstInsert(root, n);
n = n.parent;
while(n != null)
{
if(n.height == newHeight(n)) return;
n.height = newHeight(n);
if(n.parent == null) return;
if(isImbalanced(n.parent)) break;
n = n.parent;
}
if(n == null) return;
Node z = n.parent, y = n;
Node x = higherChild(y);
if(y == z.right && x == y.left)
{
x.parent = z.parent;
if(z.parent != null)
{
if(z.parent.right == z) z.parent.right = x;
else z.parent.left = x;
}
else
root = x;
Node leftx = x.left, rightx = x.right;
x.left = z; x.right = y;
y.parent = z.parent = x;
z.right = leftx; y.left = rightx;
if(leftx != null) leftx.parent = z;
if(rightx != null) rightx.parent = y;
int h = y.height;
x.height = h; y.height = z.height = h-1;
}
else if(y == z.right && x == y.right)
{
y.parent = z.parent;
if(z.parent != null)
{
if(z.parent.right == z) z.parent.right = y;
else z.parent.left = y;
}
else
root = y;
Node lefty = y.left;
y.left = z; y.right = x;
z.parent = x.parent = y;
z.right = lefty;
if(lefty != null) lefty.parent = z;
int h = y.height;
x.height = z.height = h-1;
}
else if(y == z.left && x == y.right)
{
x.parent = z.parent;
if(z.parent != null)
{
if(z.parent.right == z) z.parent.right = x;
else z.parent.left = x;
}
else
root = x;
Node leftx = x.left, rightx = x.right;
x.left = y; x.right = z;
y.parent = z.parent = x;
y.right = leftx; z.left = rightx;
if(leftx != null) leftx.parent = y;
if(rightx != null) rightx.parent = z;
int h = y.height;
x.height = h; y.height = z.height = h-1;
}
else
{
y.parent = z.parent;
if(z.parent != null)
{
if(z.parent.right == z) z.parent.right = y;
else z.parent.left = y;
}
else
root = y;
Node righty = y.right;
y.left = x; y.right = z;
z.parent = x.parent = y;
z.left = righty;
if(righty != null) righty.parent = z;
int h = y.height;
x.height = z.height = h-1;
}
}
private void inOrder(Node r, MyLinkedList<X> list)
{
if(r != null)
{
inOrder(r.left, list);
list.Insert(r.data);
inOrder(r.right, list);
}
}
private void preOrder(Node r, MyLinkedList<X> list)
{
if(r != null)
{
list.Insert(r.data);
preOrder(r.left, list);
preOrder(r.right, list);
}
}
private void postOrder(Node r, MyLinkedList<X> list)
{
if(r != null)
{
postOrder(r.left, list);
postOrder(r.right, list);
list.Insert(r.data);
}
}
public MyLinkedList<X> getElements()
{
MyLinkedList<X> list = new MyLinkedList<X>();
inOrder(root, list);
return list;
}
public X inOrderSuccessor(X d)
{
Node n = find(root, d);
if(n != null)
{
if(n.right != null)
{
Node ans = n.right;
while(ans.left != null)
ans = ans.left;
return ans.data;
}
else
{
Node ans = n;
while(ans.parent != null && ans.parent.right == ans)
ans = ans.parent;
if(ans.parent == null) return null;
return ans.parent.data;
}
}
return null;
}
}