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.
• Choice: Stores the user's choice from the menu options.
• NodeIndex: Used as a loop counter for iterating through nodes.
• Do Loop: Starts a loop to repeatedly display the menu and process user input.
• Console.WriteLine: Displays the menu options to the user.
• Choice = Console.ReadLine(): Reads the user's input and stores it in Choice.
• 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.
• Dim NodeData As Integer: Declares a variable to store the input data.
• If Integer.TryParse(Console.ReadLine(), NodeData) Then: Tries to parse the user input as an
integer and stores it in NodeData.
• 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).
• If RootPointer = -1 Then: Checks if the tree is empty (RootPointer is -1).
• 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.
• ArrayNodes(CurrentNode, 2) = FreeNode: Sets the right child pointer to FreeNode.
• Placed = True: Sets the flag to indicate the node has been placed.
• Else: If the right child exists, moves to the right child node.
• End While: Repeats the loop until the node is placed.
• End If: Ends the check for an empty tree.
• FreeNode += 1: Increments FreeNode to point to the next available index in the array.
• Else: If the input is not a valid integer, prints an error message.
• Else: If the tree is full, prints a message indicating so.
• End Sub: Ends the AddNode subroutine.
• Sub PrintAll: Starts the subroutine to print all nodes.
• For X = 0 To 9: Loops through each row in the ArrayNodes 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.
• Next: Moves to the next row.
• End Sub: Ends the PrintAll subroutine.
• Sub InOrder: Starts the subroutine for in-order traversal of the tree.
• ByRef ArrayNodes(,) As Integer: Passes the ArrayNodes array by reference.
• 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).
• If ArrayNodes(RootNode, 0) <> -1 Then: Checks if the left child exists.
• InOrder(ArrayNodes, ArrayNodes(RootNode, 0)): Recursively calls InOrder on the left child.
• Console.WriteLine(ArrayNodes(RootNode, 1)): Prints the data of the current node.
• If ArrayNodes(RootNode, 2) <> -1 Then: Checks if the right child exists.
• InOrder(ArrayNodes, ArrayNodes(RootNode, 2)): Recursively calls InOrder on the right child.
• End If: Ends the check for a valid node.
• End Sub: Ends the InOrder subroutine.
• Sub Search: Starts the subroutine to search for a 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
• For NodeIndex = 0 To 9: Loops through each node in the array.
• If ArrayNodes(NodeIndex, 1) = NodeSearch Then: Checks if the current node's data matches
the search value.
• Console.WriteLine("Value found at index" & NodeIndex & "."): Prints the index where the
value was found.
• Return: Exits the subroutine once the value is found.
• Next: Moves to the next node.
• Console.WriteLine("Value not found"): Prints a message if the value was not found.
• End Sub: Ends the Search subroutine.
• Sub PostOrder: Starts the subroutine for post-order traversal.
• ByVal RootPointer As Integer: Passes the index of the root node by value.
• If RootPointer <> -1 Then: Checks if the current node is valid.
• 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.
• PostOrder(leftChild): Recursively calls PostOrder on the left subtree.
• PostOrder(rightChild): Recursively calls PostOrder on the right subtree.
• Console.WriteLine("Node: {RootPointer}, Data: {ArrayNodes(RootPointer, 1)}"): Prints the
data of the current node.
• End If: Ends the check for a valid node.
• End Sub: Ends the PostOrder subroutine.
• Sub PreOrder: Starts the subroutine for pre-order traversal.
• ByVal RootPointer As Integer: Passes the index of the root node by value.
• If RootPointer <> -1 Then: Checks if the current node is valid.
• Console.WriteLine("Node: {RootPointer}, Data: {ArrayNodes(RootPointer, 1)}"): Prints the
data of the current node.
• 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.
• PreOrder(leftChild): Recursively calls PreOrder on the left subtree.
• PreOrder(rightChild): Recursively calls PreOrder on the right subtree.
• End If: Ends the check for a valid node.
• End Sub: Ends the PreOrder subroutine