-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_tree_1.py
133 lines (117 loc) · 4.06 KB
/
avl_tree_1.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
class Node:
def __init__(self, data: float) -> None:
self.data = data
self.left = None
self.right = None
self.height = 1
def __str__(self) -> str:
return str(self.data)
class AVLTree:
def __init__(self) -> None:
self.root = None
def insert(self, root: Node, data: float) -> Node:
if not root:
return Node(data)
elif data < root.data:
root.left = self.insert(root.left, data)
else:
root.right = self.insert(root.right, data)
root.height = 1 + max(self.get_height(root.left),
self.get_height(root.right))
balance = self.get_balance(root)
if balance > 1 and data < root.left.data:
return self.right_rotate(root)
if balance < -1 and data > root.right.data:
return self.left_rotate(root)
if balance > 1 and data > root.left.data:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
if balance < -1 and data < root.right.data:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
def left_rotate(self, z: Node) -> Node:
y = z.right
T2 = y.left
y.left = z
z.right = T2
z.height = 1 + max(self.get_height(z.left),
self.get_height(z.right))
y.height = 1 + max(self.get_height(y.left),
self.get_height(y.right))
return y
def right_rotate(self, z: Node) -> Node:
y = z.left
T3 = y.right
y.right = z
z.left = T3
z.height = 1 + max(self.get_height(z.left),
self.get_height(z.right))
y.height = 1 + max(self.get_height(y.left),
self.get_height(y.right))
return y
def get_height(self, root: Node) -> int:
if not root:
return 0
return root.height
def get_balance(self, root: Node) -> int:
if not root:
return 0
return self.get_height(root.left) - self.get_height(root.right)
def min_data(self, root: Node) -> float:
if not root:
return
if not root.left:
return root.data
return self.min_data(root.left)
def max_data(self, root: Node) -> float:
if not root:
return
if not root.right:
return root.data
return self.max_data(root.right)
def print_preorder(self, root: Node) -> None:
if root:
print(root, end=' ')
self.print_preorder(root.left)
self.print_preorder(root.right)
def print_inorder(self, root: Node) -> None:
if root:
self.print_inorder(root.left)
print(root, end=' ')
self.print_inorder(root.right)
def print_postorder(self, root: Node) -> None:
if root:
self.print_postorder(root.left)
self.print_postorder(root.right)
print(root, end=' ')
def print_breadth(self, root: Node, l: list = []) -> None:
if root:
print(root, end=' ')
if root.left:
l.append(root.left)
if root.right:
l.append(root.right)
if len(l):
self.print_breadth(l.pop(0), l)
def print_tree(self, root: Node, level: int = 0) -> None:
if root != None:
self.print_tree(root.right, level + 1)
print(' ' * level, root)
self.print_tree(root.left, level + 1)
def main():
pass
# tree = AVLTree()
# root = None
# root = tree.insert(root, 10)
# root = tree.insert(root, 20)
# root = tree.insert(root, 30)
# tree.print_tree(root)
# tree.print_inorder(root) or print()
# tree.print_preorder(root) or print()
# tree.print_postorder(root) or print()
# tree.print_breadth(root) or print()
# print(tree.min_data(root))
# print(tree.max_data(root))
if __name__ == '__main__':
main()