-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl.py
144 lines (130 loc) · 4.24 KB
/
avl.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
import bst
def height(node):
if node is None:
return -1
else:
return node.height
def update_height(node):
node.height = max(height(node.left), height(node.right)) + 1
class AVL(bst.BST):
"""
AVL binary search tree implementation.
Supports insert, find, and delete-min operations in O(lg n) time.
"""
def left_rotate(self, x):
y = x.right
y.parent = x.parent
if y.parent is None:
self.root = y
else:
if y.parent.left is x:
y.parent.left = y
elif y.parent.right is x:
y.parent.right = y
x.right = y.left
if x.right is not None:
x.right.parent = x
y.left = x
x.parent = y
update_height(x)
update_height(y)
def right_rotate(self, x):
y = x.left
y.parent = x.parent
if y.parent is None:
self.root = y
else:
if y.parent.left is x:
y.parent.left = y
elif y.parent.right is x:
y.parent.right = y
x.left = y.right
if x.left is not None:
x.left.parent = x
y.right = x
x.parent = y
update_height(x)
update_height(y)
def insert(self, t):
"""Insert key t into this tree, modifying it in-place."""
print "Inserting",t,"\n"
node = bst.BST.insert(self, t)
self.rebalance(node)
print self
print("\n\n")
def rebalance(self, node):
while node is not None:
update_height(node)
if height(node.left) >= 2 + height(node.right):
print "Rebalancing Left Subtree of",node.key
if height(node.left.left) >= height(node.left.right):
print "Height of left Subtree",height(node.left.left),">=","Height of Right Subtree",height(node.left.right)
print "Applying Right Rotate at",node.key
self.right_rotate(node)
else:
print "Applying Left Rotate at",node.left.key
self.left_rotate(node.left)
print "Applying Right Rotate at",node.key
self.right_rotate(node)
elif height(node.right) >= 2 + height(node.left):
print "Rebalancing Right Subtree of",node.key
if height(node.right.right) >= height(node.right.left):
print "Height of left Subtree",height(node.right.left),"<=","Height of Right Subtree",height(node.right.right)
print "Applying left Rotate at",node.key
self.left_rotate(node)
else:
print "Applying Right Rotate at",node.right.key
self.right_rotate(node.right)
print "Applying Left Rotate at",node.key
self.left_rotate(node)
node = node.parent
def delete_min(self):
node, parent = bst.BST.delete_min(self)
self.rebalance(parent)
#raise NotImplemented('AVL.delete_min')
def delete(self, t):
node, parent = bst.BST.delete(self,t)
#self.rebalance(parent)
#node = self.root
#while node is not None:
#if t == node.key:
#print "Search Success"
#if(node.left == None and node.right == None):
#self.left = None
#self.right = None
#self.parent = None
#node.parent.left = None
#return
#if(node.parent.left):
#node.parent.left = node.right
#if(node.parent.right):
#node.parent.right = node.left
#return node
#elif t < node.key:
# node = node.left
#else:
# node = node.right
# node, parent = bst.BST.delete_min(self)
#self.rebalance(parent)
#def test(args=None):
# bst.test(args, BSTtype=AVL)
#if __name__ == '__main__': test()
t = AVL()
t.insert(1)
t.insert(6)
t.insert(9)
t.insert(30)
t.insert(156)
t.insert(20)
t.insert(5)
t.insert(19)
t.insert(18)
t.insert(154)
t.insert(158)
t.insert(157)
t.insert(125)
#t.delete(158)
t.insert(155)
t.insert(180)
t.delete(30)
print t