-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_example.py
More file actions
190 lines (159 loc) · 5.92 KB
/
Copy pathclass_example.py
File metadata and controls
190 lines (159 loc) · 5.92 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
class Node:
"""
Tree node: left child, right child and data
"""
def __init__(self, parent=None, left=None, right=None, data=None):
"""
Node constructor
@param A node data object
"""
self.parent = parent
self.left = left
self.right = right
self.data = data #vrednost, ujedno ce biti i kljuc
#dodata klasa
class Data:
''' Tree data: Any object which is used as a tree node data'''
def __init__(self, val1,val2):
'''Data constructor'''
self.a1 = val1 # broj kao int, ovde ubaci 5
self.a2 = val2 # broj kao karakter, ovde ubaci "pet"
#automatski se poziva metoda kada pokusamo da printamo intigere
def __str__(self):
#prikaz kada se koristi print()
return f"{self.a1}"
def __repr__(self):
#prikaz u interaktivnom modu / debbugeru
return f"Data(a1={self.a1}, a2='{self.a2}')"
class Tree:
# dodat konstruktor
def __init__(self, root = None):
self.root = root
"""
Tree: root
Methods: insert, inorder_tree_walk, search, search_iterative,
minimum, maximum, successor, delete, print
"""
def tree_insert(self,z):
y = None
x = self.root
while x is not None: # da li sam mogla i !=None??
y=x
if z.data.a1 < x.data.a1:
x = x.left
else:
x = x.right
z.parent = y
if y is None: #takodje da li moze is umesto ==??
#Tree was empty, stablo je bilo prazno
self.root = z
elif z.data.a1 < y.data.a1:
y.left = z
else:
y.right = z
#ispis elemenata stabla inorder, ima jos preorder i postorder
def in_order_tree_walk(self,x): #mora self, TO MI NIJE JASNO WDYM mora??
if x is not None:
self.in_order_tree_walk(x.left) # za sta zapravo sluzi to self?
print(x.data.a1)
self.in_order_tree_walk(x.right)
#cvor od kog nadalje pretrazujemo stablo, k je kljuc koji trazimo
def tree_search(self,x, k):
if x is None or k == x.data.a1:
return x
if k < x.data.a1:
return self.tree_search(x.left, k)
else:
return self.tree_search(x.right, k)
#ista funkcija samo nije rekurzivna
def iterative_tree_search(self,x, k):
while x is not None and k != x.data.a1:
if k < x.data.a1:
x = x.left
else:
x = x.right
return x
def tree_minimum(self,x):
while x.left is not None:
x = x.left
return x
def tree_maximum(self,x):
while x.right is not None:
x = x.right
return x
def tree_successor(self,x):
if x.right is not None:
return self.tree_minimum(x.right)
y= x.parent
while y is not None and x == y.right:
x=y
y = y.parent
return y
def tree_delete(self,z):
if z.left is None:
self.transplant(z,z.right)
elif z.right is None:
self.transplant(z,z.left)
else:
y = self.tree_minimum(z.right)
if y.parent != z:
self.transplant(y,y.right)
y.right = z.right
y.righ.parent = y
self.transplant(z,y)
y.left = z.left
y.left.parent = y
def transplant(self,u,v):
if u.parent is None:
self.root = v
elif u == u.parent.left:
u.parent.left = v
else:
u.parent.right = v
if v is not None:
v.parent = u.parent
def print_tree(self):
def display(root):
"""Returns list of strings, width, height, and horizontal coordinate of the root."""
# No child.
if root.right is None and root.left is None:
line = '%s' % root.data
width = len(line)
height = 1
middle = width // 2
return [line], width, height, middle
# Only left child.
if root.right is None:
lines, n, p, x = display(root.left)
s = '%s' % root.data
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 root.left is None:
lines, n, p, x = display(root.right)
s = '%s' % root.data
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 = display(root.left)
right, m, q, y = display(root.right)
s = '%s' % root.data
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
lines, *_ = display(self.root)
for line in lines:
print(line)