Binary Tree

Binary Search Tree
This is a simple unbalanced BST that is implemented with two class modules (TreeNode and BinarySearchTree) and a Random_Main testing module, which contains the macro to run all the BST functions.
The example fills a BST with 10 random integers and completes several functions:


TreeNode Class

Here is the "TreeNode" class module with a variable to hold a value (Value) in the TreeNode, a variable to count duplicates (ValueCount), and links to the left and right child nodes (LeftChild and RightChild).
Everything is declared Public for easy access outside the class.



BinarySearchTree Class

Here is the "insert" function of the "BinarySearchTree" class module. This function creates the root TreeNode if it doesn't yet exist, adds a ValueCount to existing values instead of allowing duplicates, and recursively goes down to the first left or right Nothing TreeNode to add a new TreeNode then returns a TreeNode.
Recursively insert value with no duplicates


This function recursively deletes a TreeNode based upon Value, whether that TreeNode is a leaf, has no children, or is a binary tree and then returns a TreeNode.


This function loops to find the successor TreeNode to the TreeNode that has two children and is being deleted, then returns a TreeNode.
This function is called by the "delete" function.


This function recursively finds the Value then returns a boolean.


This function loops right to find the maximum Value, then returns a Variant.
Get the max value from right tree recursively


This function loops left to find the minmum Value then returns a Variant.
Get the min value from left tree recursively


This function recurves the BST in the order of the left TreeNode, then the root TreeNode, then the right TreeNode in order to display the values from smallest to largest.
A Collection is returned but that variable could be an Array.
Display the values in order from lowest to highest (left, root, right) recursively


This function recurses the BST in the order of the root TreeNode, then the left TreeNode, then the right TreeNode.
This could be used to get a prefix or to copy the BST but here I am displaying them. A Collection is returned but that variable could be an Array.
Display the values from left to right (root, left, right) recursively


This function recurses the BST in the order of the left TreeNode, then the right TreeNode, then the right root TreeNode.
This could be used to get a postfix or to delete the BST but here I am displaying them. A Collection is returned but that variable could be an Array.
Display the values from right to left (left, right, root) recursively


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext