-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
235 lines (200 loc) · 6.37 KB
/
main.py
File metadata and controls
235 lines (200 loc) · 6.37 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""AVL Tree"""
class Node:
"""Node class"""
def __init__(self, val=None):
"""constructor"""
self.val = val
self._left = None
self._right = None
self._height = 0
self._balance = 0 # good: {-1, 0, 1}
@property
def left(self):
return self._left
@property
def right(self):
return self._right
@property
def height(self):
return self._height
@property
def balance(self):
return self._balance
def _update(self):
"""updates height and balance of node"""
if self.left:
l = self.left._height
else:
l = -1
if self.right:
r = self.right._height
else:
r = -1
self._height = 1 + max(l, r)
self._balance = r - l
@left.setter
def left(self, node):
self._left = node
self._update()
@right.setter
def right(self, node):
self._right = node
self._update()
def display(self):
""" Function for printing binary tree,
source: https://stackoverflow.com/questions/34012886/print-binary-tree-level-by-level-in-python"""
lines, *_ = self._display_aux()
for line in lines:
print(line)
def _display_aux(self):
"""Returns list of strings, width, height, and horizontal coordinate of the root."""
# No child.
if self.right is None and self.left is None:
line = '%s' % self.val
width = len(line)
height = 1
middle = width // 2
return [line], width, height, middle
# Only left child.
if self.right is None:
lines, n, p, x = self.left._display_aux()
s = '%s' % self.val
u = len(s)
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s
second_line = x * ' ' + '/' + (n - x - 1 + u) * ' '
shifted_lines = [line + u * ' ' for line in lines]
return [first_line, second_line] + shifted_lines, n + u, p + 2, n + u // 2
# Only right child.
if self.left is None:
lines, n, p, x = self.right._display_aux()
s = '%s' % self.val
u = len(s)
first_line = s + x * '_' + (n - x) * ' '
second_line = (u + x) * ' ' + '\\' + (n - x - 1) * ' '
shifted_lines = [u * ' ' + line for line in lines]
return [first_line, second_line] + shifted_lines, n + u, p + 2, u // 2
# Two children.
left, n, p, x = self.left._display_aux()
right, m, q, y = self.right._display_aux()
s = '%s' % self.val
u = len(s)
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s + y * '_' + (m - y) * ' '
second_line = x * ' ' + '/' + (n - x - 1 + u + y) * ' ' + '\\' + (m - y - 1) * ' '
if p < q:
left += [n * ' '] * (q - p)
elif q < p:
right += [m * ' '] * (p - q)
zipped_lines = zip(left, right)
lines = [first_line, second_line] + [a + u * ' ' + b for a, b in zipped_lines]
return lines, n + m + u, max(p, q) + 2, n + u // 2
class Tree:
def __init__(self):
self.node = None
# a = h
# /
# b = h + {1, 2}
# /
# c = h + 2
def _ll(self, root):
"""left left rotation"""
t = root.left # t = b
root.left = t.right # a.l = t.r
t.right = root # t.r = a
return t
# a = h
# \
# b = h + {1, 2}
# \
# c = h + 2
def _rr(self, root):
"""right right rotation"""
t = root.right # t = b
root.right = t.left # a.r = b.l
t.left = root # t.l = a
return t
# a
# /
# b
# \
# c
def _lr(self, root):
"""left right rotation"""
root.left = self._rr(root.left)
return self._ll(root)
# a
# \
# b
# /
# c
def _rl(self, root):
"""right left rotation"""
root.right = self._ll(root.right)
return self._rr(root)
def _rebalance(self, root):
"""balancing tree via rotations"""
if root.balance > 1:
if root.right.balance >= 0:
root = self._rr(root)
else:
root = self._rl(root)
elif root.balance < -1:
if root.right.balance <= 0:
root = self._ll(root)
else:
root = self._lr(root)
return root
def _add(self, root, element):
"""Recursive function for adding element to tree"""
if not root: # Creating new Node
return Node(element)
if element < root.val:
root.left = self._add(root.left, element)
else:
root.right = self._add(root.right, element)
return self._rebalance(root) # We go down and when we go up we balance the tree
def add(self, element):
"""add element to tree"""
self.node = self._add(self.node, element)
def _search(self, element, node):
"""Recursive function for searching element in tree"""
if node is None:
return False
if node.val == element:
return True
if element >= node.val:
return self._search(element, node.right)
else:
return self._search(element, node.left)
def search(self, element):
"""Search element in tree"""
if self.node is None:
return False
if self.node.val == element:
return True
return self._search(element, self.node)
def _inorder(self, root):
"""recursive function for inorder()"""
if root:
self._inorder(root.left)
self.inorder_list.append(root.val)
self._inorder(root.right)
def inorder(self):
"""Returns a list with items in ascending order"""
self.inorder_list = []
self._inorder(self.node)
return self.inorder_list
def postorder(self):
"""Returns a list with items in descending order"""
return self.inorder()[::-1]
if __name__ == "__main__":
tree = Tree()
tree.add(1)
tree.add(2)
for j in range(3, 10):
tree.add(j)
tree.node.display()
for j in range(3, 10):
tree.add(j)
tree.node.display()
print(tree.inorder())
print(tree.postorder())