Line-by-Line Summary of Binary Tree Code
class Node: → Defines a class representing a single node in the binary tree.
def __init__(self): → Constructor initializes a node with data and two child pointers.
[Link] = "" → Stores the data value for the node (initially empty).
[Link] = None → Pointer (index) to the left child node (initially None).
[Link] = None → Pointer (index) to the right child node (initially None).
def initialiseTree(): → Initializes the tree with empty nodes and sets up free list.
global Tree, FreePointer, RootPointer, size → Declares global variables used to
manage the tree.
size = 20 → Sets the maximum number of nodes in the tree.
Tree = [Node() for _ in range(size)] → Creates a list of empty Node objects.
for i in range(size - 1): → Loop to link each node to the next as a free list.
Tree[i].Left = i + 1 → Set Left pointer to next free node's index.
Tree[size - 1].Left = None → Last node in the free list points to None.
FreePointer = 0 → Free list starts at index 0.
RootPointer = None → No root yet — tree is currently empty.
def FindInsertionPoint(NewData): → Finds where to insert new data in the binary tree.
Pointer = RootPointer → Start searching from the root node.
Parent = None → Tracks parent node while traversing tree.
while Pointer is not None: → Keep looping until a null pointer is found.
Parent = Pointer → Save current node as parent.
Current = Tree[Pointer].Data → Get data at current node.
if NewData < Current: → Go left if new data is smaller.
Pointer = Tree[Pointer].Left → Move to left child.
Direction = "Left" → Save direction as left.
else: → Otherwise, go right.
Pointer = Tree[Pointer].Right → Move to right child.
Direction = "Right" → Save direction as right.
return Parent, Direction → Return insertion point and which direction to insert.
def addToTree(NewData): → Adds a new node with given data to the binary tree.
global FreePointer, RootPointer → Access global control variables.
if FreePointer is None: → Check if the tree has space.
print("Tree is full") → If no space, print message.
return → Exit function.
NewPointer = FreePointer → Take the next free node index.
Tree[NewPointer].Data = NewData → Store new data in that node.
FreePointer = Tree[NewPointer].Left → Advance FreePointer to the next free node.
Tree[NewPointer].Left = None → Reset left pointer.
Tree[NewPointer].Right = None → Reset right pointer.
if RootPointer is None: → If tree is empty, this is the root.
RootPointer = NewPointer → Set new node as root.
else: → Otherwise, find place to insert.
Parent, Direction = FindInsertionPoint(NewData) → Find where to attach this
node.
if Direction == "Left": → Attach to left child if applicable.
Tree[Parent].Left = NewPointer → Link parent to new node (left).
else: → Otherwise, attach right.
Tree[Parent].Right = NewPointer → Link parent to new node (right).
def TraverseTree(Pointer=None): → In-order traversal (left, root, right).
global Tree, RootPointer → Access global tree structure.
if Pointer is None: → Start from root if no pointer given.
Pointer = RootPointer → Set pointer to root.
if Pointer is not None: → If pointer is valid, traverse recursively.
TraverseTree(Tree[Pointer].Left) → Visit left subtree.
print(Tree[Pointer].Data) → Print current node data.
TraverseTree(Tree[Pointer].Right) → Visit right subtree.
def SearchTree(Data): → Search for a value in the binary tree.
global Tree, RootPointer → Use global tree structure.
Pointer = RootPointer → Start from root.
while Pointer is not None: → Loop until we find or exhaust the tree.
Current = Tree[Pointer].Data → Get data at current node.
if Current == Data: → Check if found.
print(f"{Data} was found at position {Pointer}") → Print found message.
return → Exit if found.
elif Data < Current: → Go left if data is smaller.
Pointer = Tree[Pointer].Left → Update pointer to left child.
else: → Otherwise go right.
Pointer = Tree[Pointer].Right → Update pointer to right child.
print(f"{Data} was not found") → Print not found message.