Note - The code examples used in this post are located at my GitHub account - Binary Search Trees.
A binary search tree or BST is a node based data structure that contains the following properties in each node -
Each node in the tree comprises of the following properties -
It is called a binary search tree because we can determine if a node exists in the tree by using the steps from a binary search. We will go into more detail about how this works in a followup post.
A BST is seen as a recursive data structure because it uses the same node structure repeatedly to build the tree in addition to all nodes containing the same properties. We can leverage these relationships to allow us to perform some pretty nifty operations on these trees with a small amount of code.
A binary search tree or BST is a node based data structure that contains the following properties in each node -
- A key or value - The item contained within the node that can be used in comparisons.
- A pointer to the left subtree - A reference to the children in its left subtree.
- A pointer to the right subtree - A reference to the children in its right subtree.
Figure 2 - A Binary Search Tree Node.
Each node in the tree comprises of the following properties -
- All nodes in its left subtree contain key/values that are smaller than the current key/value that is stored in the node.
- All nodes in its right subtree contain key/values that are larger than the current key/value that is stored in the node.
- If the current node is a right child of its parent, the key/value in the current node will be larger than that of its parent.
- If the current node is a left child of its parent, the key/value in the current node will be smaller than that of its parent.
Figure 3 - A Binary Search Tree composed of many individual nodes.
It is called a binary search tree because we can determine if a node exists in the tree by using the steps from a binary search. We will go into more detail about how this works in a followup post.
A BST is seen as a recursive data structure because it uses the same node structure repeatedly to build the tree in addition to all nodes containing the same properties. We can leverage these relationships to allow us to perform some pretty nifty operations on these trees with a small amount of code.