-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL-Tree.py
166 lines (120 loc) · 5.07 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
class Node:
def __init__(self, data):
self.data = data
self.height = 0
self.leftChild = None
self.rightChild = None
class AVL:
def __init__(self):
self.root = None
def calcHeight(self, node):
if not node:
return -1
return node.height
def calcBalance(self, node):
if not node:
return 0
return self.calcHeight(node.leftChild) -self.calcHeight(node.rightChild)
def insert(self, data):
self.root = self.insertNode(data, self.root)
def insertNode(self, data, node):
if not node:
return Node(data)
if data<node.data:
node.leftChild = self.insertNode(data, node.leftChild)
else:
node.rightChild = self.insertNode(data, node.rightChild)
node.height = max(self.calcHeight(node.leftChild), self.calcHeight(node.rightChild)) + 1
return self.settleViolations(data, node)
def settleViolations(self, data, node):
balance = self.calcBalance(node)
print("balance: ", balance)
if balance > 1 and data < node.leftChild.data:
print("Left Left heavy situation.")
return self.rotateRight(node)
if balance < -1 and data > node.rightChild.data:
print("Right Right heavy situation.")
return self.rotateLeft(node)
if balance > 1 and data > node.leftChild.data:
print("Left Right Heavy situation.")
node.leftChild = self.rotateLeft(node.leftChild)
return self.rotateRight(node)
if balance < -1 and data < node.rightChild.data:
print("Right Left Heavy situation.")
node.rightChild = self.rotateRight(node.rightChild)
return self.rotateLeft(node)
return node
def removeNode(self, data, node):
if not node:
return node
if data < node.data:
node.leftNode = self.removeNode(data, node.leftNode)
elif data > node.data:
node.rightNode = self.removeNode(data, node.rightNode)
else:
if not node.leftNode and not node.rightNode:
print("removing a leaf node.")
del node
return None
elif not node.leftNode:
print("removing a node with single right child.")
tempNode = node.rightNode
del node
return tempNode
elif not node.rightNode:
print("removing a node with single left child.")
tempNode = node.leftNode
del node
return tempNode
print("removing a node with two children.")
predeccorNode = self.getPredeccorNode(node.leftNode)
node.data = predeccorNode.data
node.leftNode = self.removeNode(predeccorNode.data, node.leftNode)
if not node:
return node
node.height = max(self.calcHeight(node.leftChild), self.calcHeight(node.rightChild)) + 1
balance = self.calcBalance(node)
if balance > 1 and self.calcBalance(node.leftChild)>=0:
print("Left Left heavy situation.")
return self.rotateRight(node)
if balance < -1 and self.calcBalance(node.rightChild)<=0:
print("Right Right heavy situation.")
return self.rotateLeft(node)
if balance > 1 and self.calcBalance(node.leftChild)<0:
print("Left Right Heavy situation.")
node.leftChild = self.rotateLeft(node.leftChild)
return self.rotateRight(node)
if balance < -1 and self.calcBalance(node.rightChild)>0:
print("Right Left Heavy situation.")
node.rightChild = self.rotateRight(node.rightChild)
return self.rotateLeft(node)
return node
def getPredeccorNode(self, node):
if node.rightNode:
return self.getPredeccorNode(node.rightNode)
return node
def rotateRight(self, node):
tempLeftNode = node.leftChild
t = tempLeftNode.rightChild
tempLeftNode.rightChild = node
node.leftChild = t
node.height = max(self.calcHeight(node.leftChild), self.calcHeight(node.rightChild)) + 1
tempLeftNode.height = max(self.calcHeight(tempLeftNode.leftChild), self.calcHeight(tempLeftNode.rightChild)) + 1
return tempLeftNode
def rotateLeft(self, node):
tempRightNode = node.rightChild
t = tempRightNode.leftChild
tempRightNode.leftChild = node
node.rightChild = t
node.height = max(self.calcHeight(node.leftChild), self.calcHeight(node.rightChild)) + 1
tempRightNode.height = max(self.calcHeight(tempRightNode.leftChild), self.calcHeight(tempRightNode.rightChild)) + 1
return tempRightNode
def traverse(self):
if self.root:
self.traverseInOrder(self.root)
def traverseInOrder(self, node):
if node.leftChild:
self.traverseInOrder(node.leftChild)
print(node.data)
if node.rightChild:
self.traverseInOrder(node.rightChild)