-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL.py
162 lines (141 loc) · 5.41 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Created by Elshad Karimov
# Copyright © 2021 AppMillers. All rights reserved.
import QueueLinkedList as queue
class AVLNode:
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
self.height = 1
def preOrderTraversal(rootNode):
if not rootNode:
return
print(rootNode.data)
preOrderTraversal(rootNode.leftChild)
preOrderTraversal(rootNode.rightChild)
def inOrderTraversal(rootNode):
if not rootNode:
return
inOrderTraversal(rootNode.leftChild)
print(rootNode.data)
inOrderTraversal(rootNode.rightChild)
def postOrderTraversal(rootNode):
if not rootNode:
return
postOrderTraversal(rootNode.leftChild)
postOrderTraversal(rootNode.rightChild)
print(rootNode.data)
def levelOrderTraversal(rootNode):
if not rootNode:
return
else:
customQueue = queue.Queue()
customQueue.enqueue(rootNode)
while not(customQueue.isEmpty()):
root = customQueue.dequeue()
print(root.value.data)
if root.value.leftChild is not None:
customQueue.enqueue(root.value.leftChild)
if root.value.rightChild is not None:
customQueue.enqueue(root.value.rightChild)
def searchNode(rootNode, nodeValue):
if rootNode.data == nodeValue:
print("The value is found")
elif nodeValue < rootNode.data:
if rootNode.leftChild.data == nodeValue:
print("The value is found")
else:
searchNode(rootNode.leftChild, nodeValue)
else:
if rootNode.rightChild.data == nodeValue:
print("The value is found")
else:
searchNode(rootNode.rightChild, nodeValue)
def getHeight(rootNode):
if not rootNode:
return 0
return rootNode.height
def rightRotate(disbalanceNode):
newRoot = disbalanceNode.leftChild
disbalanceNode.leftChild = disbalanceNode.leftChild.rightChild
newRoot.rightChild = disbalanceNode
disbalanceNode.height = 1 + max(getHeight(disbalanceNode.leftChild), getHeight(disbalanceNode.rightChild))
newRoot.height = 1 + max(getHeight(newRoot.leftChild), getHeight(newRoot.rightChild))
return newRoot
def leftRotate(disbalanceNode):
newRoot = disbalanceNode.rightChild
disbalanceNode.rightChild = disbalanceNode.rightChild.leftChild
newRoot.leftChild = disbalanceNode
disbalanceNode.height = 1 + max(getHeight(disbalanceNode.leftChild), getHeight(disbalanceNode.rightChild))
newRoot.height = 1 + max(getHeight(newRoot.leftChild), getHeight(newRoot.rightChild))
return newRoot
def getBalance(rootNode):
if not rootNode:
return 0
return getHeight(rootNode.leftChild) - getHeight(rootNode.rightChild)
def insertNode(rootNode, nodeValue):
if not rootNode:
return AVLNode(nodeValue)
elif nodeValue < rootNode.data:
rootNode.leftChild = insertNode(rootNode.leftChild, nodeValue)
else:
rootNode.rightChild = insertNode(rootNode.rightChild, nodeValue)
rootNode.height = 1 + max(getHeight(rootNode.leftChild), getHeight(rootNode.rightChild))
balance = getBalance(rootNode)
if balance > 1 and nodeValue < rootNode.leftChild.data:
return rightRotate(rootNode)
if balance > 1 and nodeValue > rootNode.leftChild.data:
rootNode.leftChild = leftRotate(rootNode.leftChild)
return rightRotate(rootNode)
if balance < -1 and nodeValue > rootNode.rightChild.data:
return leftRotate(rootNode)
if balance < -1 and nodeValue < rootNode.rightChild.data:
rootNode.rightChild = rightRotate(rootNode.rightChild)
return leftRotate(rootNode)
return rootNode
def getMinValueNode(rootNode):
if rootNode is None or rootNode.leftChild is None:
return rootNode
return getMinValueNode(rootNode.leftChild)
def deleteNode(rootNode, nodeValue):
if not rootNode:
return rootNode
elif nodeValue < rootNode.data:
rootNode.leftChild = deleteNode(rootNode.leftChild, nodeValue)
elif nodeValue > rootNode.data:
rootNode.rightChild = deleteNode(rootNode.rightChild, nodeValue)
else:
if rootNode.leftChild is None:
temp = rootNode.rightChild
rootNode = None
return temp
elif rootNode.rightChild is None:
temp = rootNode.leftChild
rootNode = None
return temp
temp = getMinValueNode(rootNode.rightChild)
rootNode.data = temp.data
rootNode.rightChild = deleteNode(rootNode.rightChild, temp.data)
balance = getBalance(rootNode)
if balance > 1 and getBalance(rootNode.leftChild) >= 0:
return rightRotate(rootNode)
if balance < -1 and getBalance(rootNode.rightChild) <= 0:
return leftRotate(rootNode)
if balance > 1 and getBalance(rootNode.leftChild) < 0:
rootNode.leftChild = leftRotate(rootNode.leftChild)
return rightRotate(rootNode)
if balance < -1 and getBalance(rootNode.rightChild) > 0:
rootNode.rightChild = rightRotate(rootNode.rightChild)
return leftRotate(rootNode)
return rootNode
def deleteAVL(rootNode):
rootNode.data = None
rootNode.leftChild = None
rootNode.rightChild = None
return "The AVL has been successfully deleted"
newAVL = AVLNode(5)
newAVL = insertNode(newAVL, 10)
newAVL = insertNode(newAVL, 15)
newAVL = insertNode(newAVL, 20)
deleteAVL(newAVL)
levelOrderTraversal(newAVL)