forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL_tree.py
255 lines (237 loc) · 7.41 KB
/
AVL_tree.py
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
# -*- coding: utf-8 -*-
'''
An auto-balanced binary tree!
'''
import math
import random
class my_queue:
def __init__(self):
self.data = []
self.head = 0
self.tail = 0
def isEmpty(self):
return self.head == self.tail
def push(self,data):
self.data.append(data)
self.tail = self.tail + 1
def pop(self):
ret = self.data[self.head]
self.head = self.head + 1
return ret
def count(self):
return self.tail - self.head
def print(self):
print(self.data)
print("**************")
print(self.data[self.head:self.tail])
class my_node:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
self.height = 1
def getdata(self):
return self.data
def getleft(self):
return self.left
def getright(self):
return self.right
def getheight(self):
return self.height
def setdata(self,data):
self.data = data
return
def setleft(self,node):
self.left = node
return
def setright(self,node):
self.right = node
return
def setheight(self,height):
self.height = height
return
def getheight(node):
if node is None:
return 0
return node.getheight()
def my_max(a,b):
if a > b:
return a
return b
def leftrotation(node):
r'''
A B
/ \ / \
B C Bl A
/ \ --> / / \
Bl Br UB Br C
/
UB
UB = unbalanced node
'''
print("left rotation node:",node.getdata())
ret = node.getleft()
node.setleft(ret.getright())
ret.setright(node)
h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1
node.setheight(h1)
h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1
ret.setheight(h2)
return ret
def rightrotation(node):
'''
a mirror symmetry rotation of the leftrotation
'''
print("right rotation node:",node.getdata())
ret = node.getright()
node.setright(ret.getleft())
ret.setleft(node)
h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1
node.setheight(h1)
h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1
ret.setheight(h2)
return ret
def rlrotation(node):
r'''
A A Br
/ \ / \ / \
B C RR Br C LR B A
/ \ --> / \ --> / / \
Bl Br B UB Bl UB C
\ /
UB Bl
RR = rightrotation LR = leftrotation
'''
node.setleft(rightrotation(node.getleft()))
return leftrotation(node)
def lrrotation(node):
node.setright(leftrotation(node.getright()))
return rightrotation(node)
def insert_node(node,data):
if node is None:
return my_node(data)
if data < node.getdata():
node.setleft(insert_node(node.getleft(),data))
if getheight(node.getleft()) - getheight(node.getright()) == 2: #an unbalance detected
if data < node.getleft().getdata(): #new node is the left child of the left child
node = leftrotation(node)
else:
node = rlrotation(node) #new node is the right child of the left child
else:
node.setright(insert_node(node.getright(),data))
if getheight(node.getright()) - getheight(node.getleft()) == 2:
if data < node.getright().getdata():
node = lrrotation(node)
else:
node = rightrotation(node)
h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1
node.setheight(h1)
return node
def getRightMost(root):
while root.getright() is not None:
root = root.getright()
return root.getdata()
def getLeftMost(root):
while root.getleft() is not None:
root = root.getleft()
return root.getdata()
def del_node(root,data):
if root.getdata() == data:
if root.getleft() is not None and root.getright() is not None:
temp_data = getLeftMost(root.getright())
root.setdata(temp_data)
root.setright(del_node(root.getright(),temp_data))
elif root.getleft() is not None:
root = root.getleft()
else:
root = root.getright()
elif root.getdata() > data:
if root.getleft() is None:
print("No such data")
return root
else:
root.setleft(del_node(root.getleft(),data))
elif root.getdata() < data:
if root.getright() is None:
return root
else:
root.setright(del_node(root.getright(),data))
if root is None:
return root
if getheight(root.getright()) - getheight(root.getleft()) == 2:
if getheight(root.getright().getright()) > getheight(root.getright().getleft()):
root = rightrotation(root)
else:
root = lrrotation(root)
elif getheight(root.getright()) - getheight(root.getleft()) == -2:
if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()):
root = leftrotation(root)
else:
root = rlrotation(root)
height = my_max(getheight(root.getright()),getheight(root.getleft())) + 1
root.setheight(height)
return root
class AVLtree:
def __init__(self):
self.root = None
def getheight(self):
# print("yyy")
return getheight(self.root)
def insert(self,data):
print("insert:"+str(data))
self.root = insert_node(self.root,data)
def del_node(self,data):
print("delete:"+str(data))
if self.root is None:
print("Tree is empty!")
return
self.root = del_node(self.root,data)
def traversale(self): #a level traversale, gives a more intuitive look on the tree
q = my_queue()
q.push(self.root)
layer = self.getheight()
if layer == 0:
return
cnt = 0
while not q.isEmpty():
node = q.pop()
space = " "*int(math.pow(2,layer-1))
print(space,end = "")
if node is None:
print("*",end = "")
q.push(None)
q.push(None)
else:
print(node.getdata(),end = "")
q.push(node.getleft())
q.push(node.getright())
print(space,end = "")
cnt = cnt + 1
for i in range(100):
if cnt == math.pow(2,i) - 1:
layer = layer -1
if layer == 0:
print()
print("*************************************")
return
print()
break
print()
print("*************************************")
return
def test(self):
getheight(None)
print("****")
self.getheight()
if __name__ == "__main__":
t = AVLtree()
t.traversale()
l = list(range(10))
random.shuffle(l)
for i in l:
t.insert(i)
t.traversale()
random.shuffle(l)
for i in l:
t.del_node(i)
t.traversale()