-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL_TREE.py
75 lines (69 loc) · 2.08 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
#AVL TREE:
class node:#class for tree node
def __init__(self,data): #define linked list for node
self.val=data
self.left=None
self.right=None
self.height=1#first node height is one always
def insert(root,super):
if not root:
return node(super)
if super<root.val:
root.left=insert(root.left,super)
else:
root.right=insert(root.right,super)
root.height=1+max(ght(root.left),ght(root.right))
bf=getbf(root)
#RR rotation
if bf>1 and super<root.left.val:
return rightRotate(root)
#RL rotation
if bf>1 and super>root.left.val:
root.left=leftRotate(root.left)
return rightRotate(root)
#LL rotation
if bf<-1 and super>root.right.val:
return leftRotate(root)
#LR rotation
if bf<-1 and super<root.right.val:
root.right=rightRotate(root.right)
return leftRotate(root)
return root
def ght(root):
if not root:
return 0
return root.height
def getbf(root):
if not root:
return 0
return ght(root.left)-ght(root.right)
def leftRotate(A):
B=A.right
temp=B.left
B.left=A
A.right=temp
#height of both a and b updaed
A.height=1+max(ght(A.left),ght(A.right))
B.height=1+max(ght(B.left),ght(B.right))
return B
def rightRotate(A):
B=A.left
temp=B.right
B.right=A
A.left=temp
#height of both a and b updaed
A.height=1+max(ght(A.left),ght(A.right))
B.height=1+max(ght(B.left),ght(B.right))
return B
def inorder(root): #print the data inorder format
if not root:
return
inorder(root.left)
print(root.val)
inorder(root.right)
if __name__=="__main__":
root=None
vl=[19,99,75,7,21,34,38,27,134,100,29,0,12,17,143]
for i in vl:
root=insert(root,i)
inorder(root)