-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
267 lines (201 loc) · 7.72 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import sys
from os import system
from BinaryTrees.Node import *
from time import sleep
import random
def display_menu(menu):
"""
Display a menu where the key identifies the name of a function.
:param menu: dictionary, key identifies a value which is a function name
:return:
"""
print(Fore.LIGHTBLUE_EX + " -> " + Fore.WHITE + " Binary Tree Options " + Fore.LIGHTBLUE_EX + " <-\n\n")
try :
if tree.root == None :
print("tree is not defind\n\n")
else :
print("Main tree -> " , tree.data , "\n\n")
except :
print("Tree is not defined\n\n")
try :
print("Second tree -> " , lary.data ,"\n\n")
except :
print("Second tree is not defined\n\n")
for k, function in menu.items():
if k < 10 :
k = "0" + str(k)
print(Fore.MAGENTA ,"|",k,"| -> ", Fore.YELLOW ,function.__name__)
def Binarytree():
system("clear")
print("you have selected menu option BinaryTree making") # Simulate function output.
Nodes = []
global tree
select = input("Manually(1) or random(2) ? ")
if select == "1" :
system("clear")
while True :
n = int(input("Enter the node : "))
system("clear")
if (n != -1) :
Nodes.append(n)
else :
Tree = BinaryTree(Nodes)
tree = Tree
break
elif select == "2" :
system("clear")
rang = int(input("How many nodes do you want to be generated ? "))
for i in range(rang):
r = random.randint(0,100)
if r in Nodes :
continue
else :
Nodes.append(r)
Tree = BinaryTree(Nodes)
tree = Tree
print(Tree.data)
input("Press Enter to Continue\n")
system('clear') # clears stdout
def Comparing() :
system("clear")
print("you have selected menu option Comparing") # Simulate function output.
Nodes = []
global lary
while True :
n = int(input("Enter the node : "))
system("clear")
if (n != -1) :
Nodes.append(n)
else :
Lary = BinaryTree(Nodes)
lary = Lary
break
print(Lary.data)
input("Press Enter to compare\n")
if lary.PrintTree(str(2)) == tree.PrintTree(str(2)) :
print("True")
else :
print("False")
input("Press Enter to Continue\n")
system('clear') # clears stdout
def Draw(nodes=[]):
system("clear")
print("you have selected menu option drawing") # Simulate function output.
if len(nodes) != 0:
s_tree = BinaryTree(nodes)
s_tree.draw()
else:
try :
tree.draw()
except Exception as e:
print("There is no tree or it's been deleted")
print(e)
input("Press Enter to Continue\n")
system('clear') # clears stdout
def max():
system("clear")
print("you have selected menu option max ") # Simulate function output.
print(tree.MAX())
input("Press Enter to Continue\n")
system('clear') # clears stdout
def min() :
system("clear")
print("you have selected menu option min") # Simulate function output.
print(tree.MIN())
input("Press Enter to Continue\n")
system('clear') # clears stdout
def count_leafs() :
system("clear")
print("you have selected menu option count leafs") # Simulate function output.
print(tree.numberOfLeafs(tree.root))
input("Press Enter to Continue\n")
system('clear') # clears stdout
def delete_tree() :
system("clear")
print("you have selected menu option delete tree") # Simulate function output.
print(tree.delete_tree())
input("Press Enter to Continue\n")
system('clear') # clears stdout
def number_of_floors() :
system("clear")
print("you have selected menu option number of floors") # Simulate function output.
print(tree.floors_number(tree.root))
input("Press Enter to Continue\n")
system('clear') # clears stdout
def traversal() :
system("clear")
while True :
print(Fore.LIGHTBLUE_EX + " -> " + Fore.WHITE + " Traversal Types " + Fore.LIGHTBLUE_EX + " <-")
print(Fore.MAGENTA , "\n\n | 1 | ->",Fore.YELLOW," inorder\n",Fore.MAGENTA,"| 2 | ->",Fore.YELLOW," preorder\n",Fore.MAGENTA,"| 3 | ->",Fore.YELLOW," postorder\n",Fore.MAGENTA,"| 4 | ->",Fore.YELLOW," levelorder\n",Fore.MAGENTA,"| 5 | ->",Fore.YELLOW," Exit\n\n")
select = int(input("Please enter your selection number : "))
system("clear")
if (select == 5 ) :
break
else :
print(tree.PrintTree(str(select)))
input("Press Enter to Continue\n")
system('clear') # clears stdout
def Search() :
system("clear")
print("you have selected menu option search") # Simulate function output.
SearchCase = int(input("\n\nPlease enter your number : "))
print(tree.search(tree.root , SearchCase))
input("Press Enter to Continue\n")
system('clear') # clears stdout
def check_for_full_Tree() :
system("clear")
print("you have selected menu option Checking for full tree ") # Simulate function output.
print(tree.FullTree(tree.root))
input("Press Enter to Continue\n")
system('clear') # clears stdout
def is_Complete() :
system("clear")
print("you have selected menu option is complete tree ") # Simulate function output.
print(tree.isComplete())
input("Press Enter to Continue\n")
system('clear') # clears stdout
def get_count_of_children() :
system("clear")
print("you have selected menu option is count of children ") # Simulate function output.
print(tree.get_count_of_children())
input("Press Enter to Continue\n")
system('clear') # clears stdout
def number_of_nodes() :
system("clear")
print("you have selected menu option is count of children ") # Simulate function output.
print(tree.number_of_nodes())
input("Press Enter to Continue\n")
system('clear') # clears stdout
def Depth() :
system("clear")
print("you have selected menu option depth ") # Simulate function output.
print(tree.depth(tree.root))
input("Press Enter to Continue\n")
system('clear') # clears stdout
def Contributors() :
system("clear")
print(Fore.RESET,"\nAmirhossein Sabry 40011573\nKimia Keivanloo 40015753\n\nWWW.GEEKFORGEEKS.COM \u2764\uFE0F")
sleep(5)
input("Press Enter to Continue\n")
system('clear') # clears stdout
def done():
system('clear') # clears stdout
print("Goodbye")
sys.exit()
def get_word():
return tree
def main():
# Create a menu dictionary where the key is an integer number and the
# value is a function name.
functions_names = [Binarytree , Comparing , Draw, max , min , count_leafs , delete_tree , number_of_floors , Depth , traversal , Search , check_for_full_Tree , is_Complete , get_count_of_children , number_of_nodes , Contributors , done]
menu_items = dict(enumerate(functions_names, start=1))
while True:
display_menu(menu_items)
selection = int(
input(Fore.LIGHTMAGENTA_EX + "\nPlease enter your selection number: ")) # Get function key
selected_value = menu_items[selection] # Gets the function name
selected_value() # add parentheses to call the function
if __name__ == "__main__":
from colorama import *
print("\n",Fore.WHITE , Back.RED ,"Please define the tree before using other options !!!",Back.RESET , "\n")
main()