-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_tree_2.py
161 lines (136 loc) · 4.68 KB
/
avl_tree_2.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
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, data: int) -> None:
self.root = self.insert_h(self.root, data)
def insert_h(self, root: Node, data: float) -> Node:
if not root:
return Node(data)
elif data < root.data:
root.left = self.insert_h(root.left, data)
else:
root.right = self.insert_h(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) -> float:
return self.min_data_h(self.root)
def min_data_h(self, root: Node) -> float:
if not root:
return
if not root.left:
return root.data
return self.min_data_h(root.left)
def max_data(self) -> float:
return self.max_data_h(self.root)
def max_data_h(self, root: Node) -> float:
if not root:
return
if not root.right:
return root.data
return self.max_data_h(root.right)
def print_preorder(self) -> None:
self.print_preorder_h(self.root)
print()
def print_preorder_h(self, root: Node) -> None:
if root:
print(root, end=' ')
self.print_preorder_h(root.left)
self.print_preorder_h(root.right)
def print_inorder(self) -> None:
self.print_inorder_h(self.root)
print()
def print_inorder_h(self, root: Node) -> None:
if root:
self.print_inorder_h(root.left)
print(root, end=' ')
self.print_inorder_h(root.right)
def print_postorder(self) -> None:
self.print_postorder_h(self.root)
print()
def print_postorder_h(self, root: Node) -> None:
if root:
self.print_postorder_h(root.left)
self.print_postorder_h(root.right)
print(root, end=' ')
def print_breadth(self) -> None:
self.print_breadth_h(self.root)
print()
def print_breadth_h(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_h(l.pop(0), l)
def print_tree(self) -> None:
self.print_tree_h(self.root)
def print_tree_h(self, root: Node, level: int = 0) -> None:
if root != None:
self.print_tree_h(root.right, level + 1)
print(' ' * level, root)
self.print_tree_h(root.left, level + 1)
def main():
pass
# tree = AVLTree()
# tree.insert(10)
# tree.insert(20)
# tree.insert(30)
# tree.print_tree()
# tree.print_inorder()
# tree.print_preorder()
# tree.print_postorder()
# tree.print_breadth()
# print(tree.min_data())
# print(tree.max_data())
if __name__ == '__main__':
main()