Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 372dac7

Browse files
committed
Hahahah
1 parent 6f011ff commit 372dac7

File tree

7 files changed

+124
-33
lines changed

7 files changed

+124
-33
lines changed

Node.py

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,12 @@ def levelorder_tree(self , start) :
159159
ans.append(currList)
160160
return ans
161161

162-
def floors_number(self) :
163-
result = self.PrintTree("4")
164-
return len(result)
162+
def floors_number(self , node) :
163+
if node is None:
164+
return 0
165+
left = self.floors_number(node.left)
166+
right = self.floors_number(node.right)
167+
return max(left, right) + 1
165168

166169
def delete_tree(self) :
167170

@@ -172,25 +175,43 @@ def delete_tree(self) :
172175
"""
173176
self.root = None
174177

175-
def MAX_MIN(self) :
178+
def MAX(self) :
179+
180+
""" Max and Min in the tree :
181+
used a traversal so we can itrate
182+
183+
Returns:
184+
_type_: _description_
185+
"""
186+
result = self.PrintTree("1")
187+
result = result.split(",")
188+
result.pop()
189+
result = [int(x) for x in result ]
176190

191+
return max(result)
192+
193+
def MIN(self) :
194+
177195
""" Max and Min in the tree :
178196
used a traversal so we can itrate
179197
180198
Returns:
181199
_type_: _description_
182200
"""
201+
183202
result = self.PrintTree("1")
184203
result = result.split(",")
185204
result.pop()
186205
result = [int(x) for x in result ]
187206

188-
return max(result),min(result)
207+
return min(result)
208+
189209

190210
def compare(self) :
211+
""" been made in the main function
212+
"""
191213
pass
192214

193-
194215
def draw(self) :
195216

196217
""" Draw tree
@@ -230,8 +251,8 @@ def search(self, node: Node , Arg: int) :
230251
return self.search(node.right, Arg)
231252

232253

233-
def FullTree(node: Node) :
234-
queue = []
254+
def FullTree(self , node ) :
255+
queue = list()
235256
queue.append(node)
236257

237258
while queue != []:
@@ -245,5 +266,36 @@ def FullTree(node: Node) :
245266
else:
246267
return False
247268
return True
269+
270+
271+
def isComplete(self):
272+
def RisComplete(node: Node, index: int, nodeCount: int) :
273+
if node == None:
274+
return True
275+
if index >= nodeCount:
276+
return False
277+
return RisComplete(node.left, 2 * index + 1, nodeCount) and RisComplete(node.right, 2 * index + 2, nodeCount)
248278

249-
279+
def get_count_of_children(self) :
280+
def count_of_children(node: Node) : # recursive call
281+
if (node == None):
282+
return 0;
283+
return (1 + count_of_children(node.left) + count_of_children(node.right))
284+
return count_of_children(self.root) - 1
285+
286+
def number_of_nodes(self) :
287+
def count_nodes(node, counter=0) :
288+
if node:
289+
counter += 1
290+
counter = count_nodes(node.right, counter)
291+
counter = count_nodes(node.left, counter)
292+
return counter
293+
294+
return count_nodes(self.root)
295+
296+
def depth(self , node ) :
297+
if node is None:
298+
return 0
299+
left = self.floors_number(node.left)
300+
right = self.floors_number(node.right)
301+
return max(left, right)

README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +0,0 @@
1-
# Graphical Binary tree using python's turtle module
2-
### made a binary tree -> 1
3-
### node class -> 1
4-
### insert method -> 1
5-
### NodesBeenMade for number of nodes -> 1
6-
### number of floors -> 1
7-
### how to define number of leaves -> 1
8-
### made the 4 method and its working -> 1
9-
### delete the tree -> 1
10-
### MAX and MIN in our tree -> 1
11-
### full tree -> 0
12-
### compare of two tree -> 1

__pycache__/Node.cpython-310.pyc

1.55 KB
Binary file not shown.

__pycache__/Node.cpython-311.pyc

-203 Bytes
Binary file not shown.

__pycache__/graphical.cpython-310.pyc

-97 Bytes
Binary file not shown.

__pycache__/graphical.cpython-311.pyc

-171 Bytes
Binary file not shown.

main.py

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ def display_menu(menu):
1313
print(Fore.LIGHTBLUE_EX + " -> " + Fore.WHITE + " Binary Tree Options " + Fore.LIGHTBLUE_EX + " <-\n\n")
1414

1515
try :
16-
print("Main tree -> " , tree.data ,"\n\n")
16+
if tree.root == None :
17+
print("tree is not defind\n\n")
18+
else :
19+
print("Main tree -> " , tree.data , "\n\n")
1720
except :
1821
print("Tree is not defined\n\n")
1922

@@ -24,6 +27,8 @@ def display_menu(menu):
2427

2528

2629
for k, function in menu.items():
30+
if k < 10 :
31+
k = "0" + str(k)
2732
print(Fore.MAGENTA ,"|",k,"| -> ", Fore.YELLOW ,function.__name__)
2833

2934

@@ -33,6 +38,7 @@ def Binarytree():
3338
Nodes = []
3439
global tree
3540
select = input("Manually(1) or random(2) ? ")
41+
3642
if select == "1" :
3743
system("clear")
3844
while True :
@@ -82,7 +88,7 @@ def Comparing() :
8288
print(Lary.data)
8389
input("Press Enter to compare\n")
8490

85-
if lary.PrintTree(str(1)) == tree.PrintTree(str(1)) :
91+
if lary.PrintTree(str(2)) == tree.PrintTree(str(2)) :
8692
print("True")
8793
else :
8894
print("False")
@@ -105,16 +111,25 @@ def Draw():
105111
system('clear') # clears stdout
106112

107113

108-
def MAX_and_MIN():
114+
def max():
115+
system("clear")
116+
print("you have selected menu option max ") # Simulate function output.
117+
118+
print(tree.MAX())
119+
120+
input("Press Enter to Continue\n")
121+
system('clear') # clears stdout
122+
123+
def min() :
109124
system("clear")
110-
print("you have selected menu option max and min") # Simulate function output.
125+
print("you have selected menu option min") # Simulate function output.
111126

112-
print(tree.MAX_MIN())
127+
print(tree.MIN())
113128

114129
input("Press Enter to Continue\n")
115130
system('clear') # clears stdout
116131

117-
def CountLeafs() :
132+
def count_leafs() :
118133
system("clear")
119134
print("you have selected menu option count leafs") # Simulate function output.
120135

@@ -123,7 +138,7 @@ def CountLeafs() :
123138
input("Press Enter to Continue\n")
124139
system('clear') # clears stdout
125140

126-
def DeleteTree() :
141+
def delete_tree() :
127142
system("clear")
128143
print("you have selected menu option delete tree") # Simulate function output.
129144

@@ -132,11 +147,11 @@ def DeleteTree() :
132147
input("Press Enter to Continue\n")
133148
system('clear') # clears stdout
134149

135-
def numberOfFloors() :
150+
def number_of_floors() :
136151
system("clear")
137152
print("you have selected menu option number of floors") # Simulate function output.
138153

139-
print(tree.floors_number())
154+
print(tree.floors_number(tree.root))
140155

141156
input("Press Enter to Continue\n")
142157
system('clear') # clears stdout
@@ -167,14 +182,50 @@ def Search() :
167182
input("Press Enter to Continue\n")
168183
system('clear') # clears stdout
169184

170-
def CheckForFullTree() :
185+
def check_for_full_Tree() :
171186
system("clear")
172187
print("you have selected menu option Checking for full tree ") # Simulate function output.
173-
print(tree.FullTree())
188+
print(tree.FullTree(tree.root))
189+
190+
input("Press Enter to Continue\n")
191+
system('clear') # clears stdout
192+
193+
def is_Complete() :
194+
system("clear")
195+
print("you have selected menu option is complete tree ") # Simulate function output.
196+
197+
print(tree.isComplete())
198+
199+
input("Press Enter to Continue\n")
200+
system('clear') # clears stdout
201+
202+
def get_count_of_children() :
203+
system("clear")
204+
print("you have selected menu option is count of children ") # Simulate function output.
205+
206+
print(tree.get_count_of_children())
207+
208+
input("Press Enter to Continue\n")
209+
system('clear') # clears stdout
210+
211+
def number_of_nodes() :
212+
system("clear")
213+
print("you have selected menu option is count of children ") # Simulate function output.
214+
215+
print(tree.number_of_nodes())
174216

175217
input("Press Enter to Continue\n")
176218
system('clear') # clears stdout
219+
220+
def Depth() :
221+
system("clear")
222+
print("you have selected menu option depth ") # Simulate function output.
177223

224+
print(tree.depth(tree.root))
225+
226+
input("Press Enter to Continue\n")
227+
system('clear') # clears stdout
228+
178229
def Contributors() :
179230
system("clear")
180231
print(Fore.RESET,"\nAmirhossein Sabry 40011573\nKimia Keivanloo 40015753\n\nWWW.GEEKFORGEEKS.COM \u2764\uFE0F")
@@ -194,7 +245,7 @@ def get_word():
194245
def main():
195246
# Create a menu dictionary where the key is an integer number and the
196247
# value is a function name.
197-
functions_names = [Binarytree,Comparing, Draw, MAX_and_MIN, CountLeafs , DeleteTree , numberOfFloors ,traversal, Search ,CheckForFullTree, Contributors ,done]
248+
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]
198249
menu_items = dict(enumerate(functions_names, start=1))
199250

200251
while True:

0 commit comments

Comments
 (0)