Sharjeel Tree
Sharjeel Tree
class TreeNode:
def __init__(self):
self.data = ""
self.leftpointer = NULLPOINTER
self.rightpointer = NULLPOINTER
def initialisetree():
tree = [TreeNode() for _ in range(8)]
rootpointer = NULLPOINTER
freeptr = 0
for index in range(7):
tree[index].leftpointer = index + 1
return (tree, rootpointer, freeptr)
if rootpointer == NULLPOINTER:
rootpointer = newnodeptr
else:
thisnodeptr = rootpointer
previousnodeptr = NULLPOINTER
turnedleft = True
if turnedleft:
tree[previousnodeptr].leftpointer = newnodeptr
else:
tree[previousnodeptr].rightpointer = newnodeptr
else:
print("No space for more data")
return (tree, rootpointer, freeptr)
def getoption():
print("1: Add data")
print("2: Find data")
print("3: Traverse tree")
print("4: End program")
return input("Enter your choice: ")
option = getoption()
while option != "4":
if option == "1":
data = input("Enter the value: ")
tree, rootpointer, freeptr = insertnode(tree, rootpointer, freeptr, data)
print("Tree after insertion:")
traversetree(tree, rootpointer)
elif option == "2":
data = input("Enter search value: ")
thisnodeptr = findnode(tree, rootpointer, data)
if thisnodeptr == NULLPOINTER:
print("Value not found")
else:
print("Value found at index", thisnodeptr)
elif option == "3":
print("Tree traversal:")
traversetree(tree, rootpointer)
option = getoption()