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.
100 100 / \ Insert 40 / \ 20 500 ---------> 20 500 / \ / \ 10 30 10 30 \ 40
Approach: The idea is to note that new keys are always inserted at the leaf node.
- Take a temporary pointer named and start from the root node to traverse the tree downwards to find the correct leaf node at which the key is to be inserted.
- Also, keep track of the trailing pointer y to find the parent of the new node.
- After getting the final values of x and y, assign x as the right child of y if x->key is less than y->key. Else, if y->key is greater than x->key, assign x as the right child of y.(If the tree is NULL then the new node is made the root node.)
Below is the implementation of the above approach:
“””Python3 program to demonstrate insert
operation in binary search tree “””
# A Binary Tree Node
# Utility function to create a
# new tree node
# Constructor to create a newNode
def __init__(self, data):
self.left = None
self.right = self.parent = None
# A utility function to insert a new
# Node with given key in BST
def insert(root, key):
# Create a new Node containing
# the new element
newnode = newNode(key)
# Pointer to start traversing from root
# and traverses downward path to search
# where the new node to be inserted
x = root
# Pointer y maintains the trailing
# pointer of x
y = None
while (x != None):
y = x
if (key < x.key): x = x.left else: x = x.right # If the root is None i.e the tree is # empty. The new node is the root node if (y == None): y = newnode # If the new key is less then the leaf node key # Assign the new node to be its left child elif (key < y.key): y.left = newnode # else assign the new node its # right child else: y.right = newnode # Returns the pointer where the # new node is inserted return y # A utility function to do inorder # traversal of BST def Inorder(root) : if (root == None) : return else: Inorder(root.left) print( root.key, end = " " ) Inorder(root.right) # Driver Code if __name__ == '__main__': """ Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 """ root = None root = insert(root, 50) insert(root, 30) insert(root, 20) insert(root, 40) insert(root, 70) insert(root, 60) insert(root, 80) # Prinoder traversal of the BST Inorder(root) # This code is contributed by # SHUBHAMSINGH10 [tabby title="C#"]
20 30 40 50 60 70 80
- Binary Search Tree insert with Parent Pointer
- Search a node in Binary Tree
- Find the node with maximum value in a Binary Search Tree
- Postorder predecessor of a Node in Binary Search Tree
- Find the node with minimum value in a Binary Search Tree
- Find the node with maximum value in a Binary Search Tree using recursion
- Find the node with minimum value in a Binary Search Tree using recursion
- Minimum swap required to convert binary tree to binary search tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Binary Search Tree | Set 1 (Search and Insertion)
- Count the Number of Binary Search Trees present in a Binary Tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Binary Tree to Binary Search Tree Conversion
- K-th ancestor of a node in Binary Tree | Set 3
- K-th ancestor of a node in Binary Tree
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.