-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl_tree.py
More file actions
159 lines (125 loc) · 3.64 KB
/
avl_tree.py
File metadata and controls
159 lines (125 loc) · 3.64 KB
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
# AVL Tree implementation
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
self.height = 1
def getHeight(root):
if root == None:
return 0
return root.height
def rightRotate(y):
# Create references
x = y.left
t2 = x.right
# Actual Rotation
y.left = t2
x.right = y
# Update heights
y.height = max(getHeight(y.left), getHeight(y.right)) + 1
x.height = max(getHeight(x.left), getHeight(x.right)) + 1
# Return the new root
return x
def leftRotate(y):
# Create references
x = y.right
t2 = x.left
# Rotate
y.right= t2
x.left = y
# Update heights
y.height = max(getHeight(y.left), getHeight(y.right)) + 1
x.height = max(getHeight(x.left), getHeight(x.right)) + 1
# Return new root
return x
def getBalance(root):
if root == None:
return 0
return getHeight(root.left) - getHeight(root.right)
def insert(root, data):
# Insert new element into the AVL tree
if root == None:
return Node(data)
if data < root.data:
root.left = insert(root.left, data)
elif data > root.data:
root.right = insert(root.right, data)
else:
return root
root.height = 1 + max(getHeight(root.left), getHeight(root.right))
balance = getBalance(root)
# Left left case
if root.left != None:
if balance > 1 and data < root.left.data:
return rightRotate(root)
# Left right case
if balance > 1 and data > root.left.data:
root.left = leftRotate(root.left)
return rightRotate(root)
# right right case
if root.right != None:
if balance < -1 and data > root.right.data:
return leftRotate(root)
if balance < -1 and data < root.right.data:
root.right = rightRotate(root.right)
return leftRotate(root)
return root
def minValueNode(root):
p = root;
while p.left != None:
p = p.left
return p
def delete(data, root):
if root == None:
return
if data < root.data:
root.left = delete(data, root.left)
elif data > root.data:
root.right = delete(data, root.right)
else:
if root.left == None or root.right == None:
temp = root.left if root.left != None else root.right
if temp == None:
temp = root
root = None
else:
root = temp
else:
temp = minValueNode(root.right)
root.data = temp.data
root.right = delete(root.right, temp.data)
if root == None:
return root
root.height = 1 + max(getHeight(root.left), getHeight(root.right))
balance = getBalance(root)
if balance > 1 and getBalance(root.left) >= 0:
return rightRotate(root)
if balance > 1 and getBalance(root.left) < 0:
root.left = leftRotate(root.left)
return rightRotate(root)
if balance < -1 and getBalance(root.right) <= 0:
return leftRotate(root)
if balance < -1 and getBalance(root.right) > 0:
root.right = rightRotate(root.right)
return leftRotate(root)
return root
def pre_order(root):
if root == None:
return
print(root.data, end=" ")
pre_order(root.left)
pre_order(root.right)
def testAVLTree():
root = Node(9)
data = [5,10,0,6,11,-1,1,2]
for d in data:
root = insert(root, d)
print("Preorder traversal is")
pre_order(root)
print("\n")
root = delete(10,root)
print("Preorder traversal is")
pre_order(root)
print("\n")
testAVLTree()