0% found this document useful (0 votes)
5 views2 pages

Sharjeel Tree

The document outlines a Python implementation of a binary tree with functionalities for initializing the tree, inserting nodes, finding nodes, and traversing the tree. It includes a user interface for adding data, searching for values, and displaying the tree structure. The implementation uses a fixed-size array to represent the tree nodes and manages pointers to navigate through the tree.

Uploaded by

zluffy392
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Sharjeel Tree

The document outlines a Python implementation of a binary tree with functionalities for initializing the tree, inserting nodes, finding nodes, and traversing the tree. It includes a user interface for adding data, searching for values, and displaying the tree structure. The implementation uses a fixed-size array to represent the tree nodes and manages pointers to navigate through the tree.

Uploaded by

zluffy392
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

NULLPOINTER = -1

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)

def insertnode(tree, rootpointer, freeptr, newitem):


if freeptr != NULLPOINTER:
newnodeptr = freeptr
tree[newnodeptr].data = newitem
freeptr = tree[freeptr].leftpointer
tree[newnodeptr].leftpointer = NULLPOINTER
tree[newnodeptr].rightpointer = NULLPOINTER # Initialize right pointer

if rootpointer == NULLPOINTER:
rootpointer = newnodeptr
else:
thisnodeptr = rootpointer
previousnodeptr = NULLPOINTER
turnedleft = True

while thisnodeptr != NULLPOINTER:


previousnodeptr = thisnodeptr
if tree[thisnodeptr].data > newitem:
turnedleft = True
thisnodeptr = tree[thisnodeptr].leftpointer
else:
turnedleft = False
thisnodeptr = tree[thisnodeptr].rightpointer

if turnedleft:
tree[previousnodeptr].leftpointer = newnodeptr
else:
tree[previousnodeptr].rightpointer = newnodeptr
else:
print("No space for more data")
return (tree, rootpointer, freeptr)

def findnode(tree, rootpointer, searchitem):


thisnodeptr = rootpointer
while thisnodeptr != NULLPOINTER and tree[thisnodeptr].data != searchitem:
if tree[thisnodeptr].data > searchitem:
thisnodeptr = tree[thisnodeptr].leftpointer
else:
thisnodeptr = tree[thisnodeptr].rightpointer
return thisnodeptr

def traversetree(tree, rootpointer):


if rootpointer != NULLPOINTER:
traversetree(tree, tree[rootpointer].leftpointer)
print(tree[rootpointer].data)
traversetree(tree, tree[rootpointer].rightpointer)

def getoption():
print("1: Add data")
print("2: Find data")
print("3: Traverse tree")
print("4: End program")
return input("Enter your choice: ")

tree, rootpointer, freeptr = initialisetree()

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()

You might also like