Binary Search Tree Insertion

In this lesson, we'll study the binary search tree insertion algorithm!

Binary Search Tree Insertion Algorithm #

Here is a description of the algorithm you’d use to insert a new value into a BST.

  1. Start from the root node.

  2. Check if the value to be inserted is greater than the root/current node’s value.

  3. If yes, then repeat step 2 for the right subtree, otherwise,​ repeat step 2 for the left subtree of the current node.

  4. Repeat until you find a node that has no right/left child to move onto. Insert the given value there and update the parent node accordingly.

Study the animation below for a visual of this algorithm.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.