-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL Tree Insertion.py
152 lines (112 loc) · 3.48 KB
/
AVL Tree Insertion.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
# User function Template for python3
''' structure of tree node:
class Node:
def __init__(self,x):
self.data=x
self.left=None
self.right=None
self.height=1
'''
class Solution:
def getHeight(self, root):
if root == None:
return 0
return root.height
def getBalance(self, root):
if root == None:
return 0
return self.getHeight(root.left) - self.getHeight(root.right)
def leftRotate(self, root):
y = root.right
t2 = y.left
root.right = t2
y.left = root
root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right))
y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right))
return y
def rightRotate(self, root):
y = root.left
t2 = y.right
root.left = t2
y.right = root
root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right))
y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right))
return y
def insertToAVL(self, root, key):
if not root:
return Node(key)
if key < root.data:
root.left = self.insertToAVL(root.left, key)
else:
root.right = self.insertToAVL(root.right, key)
root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right))
balance = self.getBalance(root)
# Left Heavy
if balance > 1:
# Left-Left case
if key < root.left.data:
return self.rightRotate(root)
# Left-Right case
if key > root.left.data:
root.left = self.leftRotate(root.left)
return self.rightRotate(root)
# Right Heavy
if balance < -1:
# Right-Right case
if key > root.right.data:
return self.leftRotate(root)
# Right-Left case
if key < root.right.data:
root.right = self.rightRotate(root.right)
return self.leftRotate(root)
return root
# {
# Driver Code Starts
# Initial Template for Python 3
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
self.height = 1
def isBST(n, lower, upper):
if not n:
return True
if n.data <= lower or n.data >= upper:
return False
return isBST(n.left, lower, n.data) and isBST(n.right, n.data, upper)
def isBalanced(n):
if not n:
return (0, True)
lHeight, l = isBalanced(n.left)
rHeight, r = isBalanced(n.right)
if abs(lHeight - rHeight) > 1:
return (0, False)
return (1 + max(lHeight, rHeight), l and r)
def isBalancedBST(root):
if not isBST(root, -1000000000, 1000000000):
print("BST voilated, inorder traversal :", end=' ')
elif not isBalanced(root)[1]:
print("Unbalanced BST, inorder traversal :", end=' ')
else:
return True
return False
def printInorder(n):
if not n:
return
printInorder(n.left)
print(n.data, end=' ')
printInorder(n.right)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
ip = [int(x) for x in input().strip().split()]
root = None
for i in range(n):
root = Solution().insertToAVL(root, ip[i])
if not isBalancedBST(root):
break
printInorder(root)
print()
# } Driver Code Ends