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

Commit bfa7e38

Browse files
committed
Menu
1 parent 2fa911d commit bfa7e38

File tree

7 files changed

+41
-9
lines changed

7 files changed

+41
-9
lines changed

Node.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def addRightChild(self, value: int):
2020

2121

2222
def __repr__(self):
23-
return "value of node equals by -> " + str(self.value)
23+
return "Node data : " + str(self.value)
2424

2525

2626
class BinaryTree :
@@ -212,4 +212,23 @@ def numberOfLeafs(self, node: Node) :
212212
return 1
213213
else:
214214
return self.numberOfLeafs(node.left) + self.numberOfLeafs(node.right)
215+
216+
def search(self, node: Node , Arg: int) :
217+
""" Binary search
218+
implanting binary search on the tree and compare two nodes with each other
219+
search will go to right node if arg is bigger and go to left if arg is smaller
220+
221+
Returns:
222+
Node
223+
"""
224+
if node is None or node.value == Arg:
225+
return node
215226

227+
if node.value > Arg:
228+
return self.search(node.left, Arg)
229+
elif node.value < Arg:
230+
return self.search(node.right, Arg)
231+
232+
233+
B = BinaryTree([10,1,12])
234+
print(B.search(B.root,15))

__pycache__/Node.cpython-310.pyc

551 Bytes
Binary file not shown.

__pycache__/Node.cpython-39.pyc

5.44 KB
Binary file not shown.

__pycache__/graphical.cpython-310.pyc

0 Bytes
Binary file not shown.

__pycache__/graphical.cpython-39.pyc

2.14 KB
Binary file not shown.

graphical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ def draw(node, x, y, dx):
6363

6464

6565
# BINARY TREE EXPLANTION
66-
66+

main.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
21
import sys
32
from os import system
43
from Node import *
54
from time import sleep
65

7-
86
def display_menu(menu):
97
"""
108
Display a menu where the key identifies the name of a function.
@@ -13,6 +11,11 @@ def display_menu(menu):
1311
"""
1412
print(Fore.LIGHTBLUE_EX + " -> " + Fore.WHITE + " Binary Tree Options " + Fore.LIGHTBLUE_EX + " <-\n\n")
1513

14+
try :
15+
print("Current tree -> " , tree.data ,"\n\n")
16+
except :
17+
print("Tree is not defined\n\n")
18+
1619
for k, function in menu.items():
1720
print(Fore.MAGENTA ,"|",k,"| -> ", Fore.YELLOW ,function.__name__)
1821

@@ -104,6 +107,17 @@ def traversal() :
104107
system('clear') # clears stdout
105108

106109

110+
def Search() :
111+
system("clear")
112+
print("you have selected menu option search") # Simulate function output.
113+
114+
SearchCase = int(input("\n\nPlease enter your number : "))
115+
print(tree.search(tree.root , SearchCase))
116+
117+
input("Press Enter to Continue\n")
118+
system('clear') # clears stdout
119+
120+
107121
def done():
108122
system('clear') # clears stdout
109123
print("Goodbye")
@@ -116,7 +130,7 @@ def get_word():
116130
def main():
117131
# Create a menu dictionary where the key is an integer number and the
118132
# value is a function name.
119-
functions_names = [Binarytree, Draw, MAX_and_MIN, CountLeafs , DeleteTree , numberOfFloors ,traversal, done]
133+
functions_names = [Binarytree, Draw, MAX_and_MIN, CountLeafs , DeleteTree , numberOfFloors ,traversal, Search , done]
120134
menu_items = dict(enumerate(functions_names, start=1))
121135

122136
while True:
@@ -128,7 +142,6 @@ def main():
128142

129143

130144
if __name__ == "__main__":
131-
from colorama import Fore
132-
print(Fore.RED ,"\nPlease define the tree before using other options !!!\n")
133-
print(Fore.WHITE)
134-
main()
145+
from colorama import *
146+
print("\n",Fore.WHITE , Back.RED ,"Please define the tree before using other options !!!",Back.RESET , "\n")
147+
main()

0 commit comments

Comments
 (0)