Binary Tree Code Explanation
Binary Tree Code Explanation
• ArrayNodes(9, 2): Declares a 2D array with 10 rows and 3 columns to store the nodes of the
binary tree. The columns represent the left child pointer, data value, and right child pointer
respectively.
• RootPointer: Stores the index of the root node in the ArrayNodes array. Initialized to -1
indicating the tree is initially empty.
• FreeNode: Keeps track of the next available index in the ArrayNodes array where a new
node can be inserted.
• Do Loop: Starts a loop to repeatedly display the menu and process user input.
• Sub AddNode: Starts the subroutine to add a new node to the tree.
• If FreeNode <= 9: Checks if there is space available in the array to add a new node.
• Console.WriteLine("Enter the Data"): Prompts the user to enter the data for the new node.
• ArrayNodes(FreeNode, 1) = NodeData: Stores the input data in the second column of the
array at the index FreeNode.
• ArrayNodes(FreeNode, 0) = -1: Initializes the left child pointer to -1 (no left child).
• ArrayNodes(FreeNode, 2) = -1: Initializes the right child pointer to -1 (no right child).
• RootPointer = FreeNode: Sets the root pointer to the current FreeNode index, establishing
the first node as the root.
• Else: If the tree is not empty, the code attempts to place the new node in the correct
position.
• Dim Placed As Boolean = False: Initializes a flag to indicate if the node has been placed.
• Dim CurrentNode As Integer = RootPointer: Starts the search for the correct position from
the root node.
• If NodeData < ArrayNodes(CurrentNode, 1) Then: Compares the new node's data with the
current node's data.
• If ArrayNodes(CurrentNode, 0) = -1 Then: If the left child pointer is -1, places the new node
here.
• ArrayNodes(CurrentNode, 0) = FreeNode: Sets the left child pointer to FreeNode.
• Placed = True: Sets the flag to indicate the node has been placed.
• Else: If the left child exists, moves to the left child node.
• Else: If the new node's data is greater than or equal to the current node's data.
• If ArrayNodes(CurrentNode, 2) = -1 Then: If the right child pointer is -1, places the new node
here.
• Placed = True: Sets the flag to indicate the node has been placed.
• Else: If the right child exists, moves to the right child node.
• FreeNode += 1: Increments FreeNode to point to the next available index in the array.
• Console.WriteLine(ArrayNodes(X, 0) & " " & ArrayNodes(X, 1) & " " & ArrayNodes(X, 2)):
Prints the left child pointer, data, and right child pointer for each node.
• Sub InOrder: Starts the subroutine for in-order traversal of the tree.
• ByVal RootNode As Integer: Passes the index of the root node by value.
• If RootNode <> -1 Then: Checks if the current node is valid (not an empty node).
• Console.WriteLine("Please enter the node you would like to search"): Prompts the user to
enter the value to search for.
• Dim NodeSearch As Integer = Console.ReadLine: Reads the user's input and stores it in
NodeSearch
• Console.WriteLine("Value found at index" & NodeIndex & "."): Prints the index where the
value was found.
• Console.WriteLine("Value not found"): Prints a message if the value was not found.
• ByVal RootPointer As Integer: Passes the index of the root node by value.
• Dim leftChild As Integer = ArrayNodes(RootPointer, 0): Gets the index of the left child.
• Dim rightChild As Integer = ArrayNodes(RootPointer, 2): Gets the index of the right child.
• ByVal RootPointer As Integer: Passes the index of the root node by value.
• Dim leftChild As Integer = ArrayNodes(RootPointer, 0): Gets the index of the left child.
• Dim rightChild As Integer = ArrayNodes(RootPointer, 2): Gets the index of the right child.