Binary Search Tree (BST)





Definition: A binary tree in which all values to the bottom left of each individual node are less than it and all values to the bottom right of each individual node are greater than it. In the picture above, all values to the bottom left of 20 (15, 8, 17) are less than 20, and all values to the bottom right of 20 (35, 22, 60) are greater than 20. This is true for any node in question.



Real Life Example: Imagine that you are golfing. Everytime you hit the ball, you are aiming to get the ball in the hole, meaning that you are adjusting which direction you are hitting based on where your ball previousy landed. For example, if I know that the hole is to the right of where I am standing, I will hit it in that direction. If I overshoot, then I know I need to hit left. This calculation and resulting readjustment continues until I get the ball in the hole (the equivalent of finding your target in a BST). In summary, reaching the target is a result of multiple step by step comparisons that narrow down the end target.


Basic Properties (See operations section below for applications):



Basic BST Operations:

  1. Inserting a node. This is done by starting from the root node and comparing the value of the new node (less than or greater than) to the current node. After a comparison has been made, the node moves in the appropriate direction down one level of the tree. This process repeats until the new node is a leaf node, where it is finally placed in accordance with BST properties.
  2. Deleting a node. There are 3 possible cases for this:
    • The node is a leaf node. In this case, the node is simply removed from the BST with no other actions taken.


    • The node has one child. In this case, the node is removed from the BST and the child simply takes its place.



    • The node has two children. In this case, the node is removed from the BST and its successor or predecessor (see properties section above) replaces it.





Fun Fact: Similar to sets, BST's must have all unique data. No duplicate node values are allowed.



Commonly Used In:



Other Similar Data Structures: