-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
210 lines (170 loc) · 5.15 KB
/
Main.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
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
import drawtree
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
self.lvl= None
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
def lookup(self, data, parent=None):
if data < self.data:
if self.left is None:
return None, None
return self.left.lookup(data, self)
elif data > self.data:
if self.right is None:
return None, None
return self.right.lookup(data, self)
else:
return self, parent
def children_count(self):
cnt = 0
if self.left:
cnt += 1
if self.right:
cnt += 1
return cnt
def delete(self, data):
node, parent = self.lookup(data)
if node is not None:
children_count = node.children_count()
if children_count == 0:
if parent:
if parent.left is node:
parent.left = None
else:
parent.right = None
del node
else:
self.data = None
elif children_count == 1:
if node.left:
n = node.left
else:
n = node.right
if parent:
if parent.left is node:
parent.left = n
else:
parent.right = n
del node
else:
self.left = n.left
self.right = n.right
self.data = n.data
else:
parent = node
successor = node.right
while successor.left:
parent = successor
successor = successor.left
node.data = successor.data
if parent.left == successor:
parent.left = successor.right
else:
parent.right = successor.right
def tree_to_list(self, node):
if node is None:
return []
return [node.data] + self.tree_to_list(node.left) + self.tree_to_list(node.right)
def infixe(self, root):
if root is not None:
self.infixe(root.left)
print(root.data)
self.infixe(root.right)
def prefixe(self, root):
if root is not None:
print(root.data)
self.prefixe(root.left)
self.prefixe(root.right)
def postfixe(self, root):
if root is not None:
self.postfixe(root.left)
self.postfixe(root.right)
print(root.data)
def profondeur(self, root):
pile = []
if root:
pile.append(root)
while (len(pile) != 0):
node = pile.pop()
print(node.data)
if node.right:
pile.append(node.right)
if node.left:
pile.append(node.left)
def Tlenght(self, root):
if (root.left is None and root.right is None):
return 0
else:
tmp1 = 0
tmp2 = 0
if root.left:
tmp1 = self.Tlenght(root.left)
if root.right:
tmp2 = self.Tlenght(root.right)
return 1+ max(tmp1,tmp2)
def Tweight(self, root):
if (root.left is None and root.right is None):
return 1
else:
tmp1 = 0
tmp2 = 0
if root.left:
tmp1 = self.Tweight(root.left)
if root.right:
tmp2 = self.Tweight(root.right)
return 1+ tmp1 + tmp2
def largeur(self, root):
file = []
if root:
print(root.data)
file.append(root)
while(len(file) != 0):
node = file.pop(0)
if node.left:
print(node.left.data)
file.append(node.left)
if node.right:
print(node.right.data)
file.append(node.right)
root = Node(8)
root.insert(3)
root.insert(10)
root.insert(1)
root.insert(6)
root.insert(4)
root.insert(7)
root.insert(53)
root.insert(14)
root.insert(13)
root.insert(72)
print('----------- Largeur ------------')
root.largeur(root)
print('---------- infixe -------------')
root.infixe(root)
print('----------- Prefixe ------------')
root.prefixe(root)
print('--------- Postfixe --------------')
root.postfixe(root)
print('--------- Tree --------------')
drawtree.draw_bst(root.tree_to_list(root))
print('--------- lenght --------------')
print(root.Tlenght(root))
print('--------- Weight --------------')
print(root.Tweight(root))
print('--------- Profondeur --------------')
root.profondeur(root)