A recursive approach to insert a new node in a BST is already discussed in the post: Binary Search Tree | SET 1. In this post, an iterative approach to insert a node in BST is discussed.
Insertion of a Key
A new key is always inserted at the leaf node. Start searching a key from root till we hit a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.
Input:To the given BST insert 40
Explanation:The new node 40 is a leaf node. Start searching from the root till a leaf node is hit, i.e while searching if a new value is greater than current node move to right child else to left child.
Input:To the given BST insert 600
Explanation:The new node 600 is a leaf node. Start searching from the root till a leaf node is hit, i.e while searching if a new value is greater than current node move to right child else to left child.
- It is to be noted that new keys are always inserted at the leaf node.
- Start from root and run a loop until a null pointer is reached.
- Keep the previous pointer of the current node stored.
- If the current node is null then create and insert the new node there and make it as one of the children of the parent/previous node depending on its value.
- If the value of current node is less than the new value then move to the right child of current node else move to the left child.
Below is the implementation of the above approach:
20 30 40 50 60 70 80
- Time Complexity : O(h), where h is height of binary search tree. In worst case the height is equal to number of nodes.
- Space Complexity: O(1), no extra space is required.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Binary Search Tree insert with Parent Pointer
- Binary Tree to Binary Search Tree Conversion
- Minimum swap required to convert binary tree to binary search tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Difference between Binary Tree and Binary Search Tree
- Binary Search Tree | Set 1 (Search and Insertion)
- Find the node with minimum value in a Binary Search Tree
- Find the node with maximum value in a Binary Search Tree
- Search a node in Binary Tree
- Find the node with minimum value in a Binary Search Tree using recursion
- Find the node with maximum value in a Binary Search Tree using recursion
- Postorder predecessor of a Node in Binary Search Tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Count the Number of Binary Search Trees present in a Binary Tree
- 2-3 Trees | (Search and Insert)
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a Generic Tree(N-array Tree) to Binary Tree
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.