Binary Search Tree insert with Parent Pointer - GeeksforGeeks

25 Oct, 2021
We have discussed simple BST insert. How to insert in a tree where parent pointer needs to be maintained. Parent pointers are helpful to quickly find ancestors of a node, LCA of two nodes, successor of a node, etc. 
In recursive calls of simple insertion, we return pointer of root of subtree created in a subtree. So the idea is to store this pointer for left and right subtrees. We set parent pointers of this returned pointers after the recursive calls. This makes sure that all parent pointers are set during insertion. Parent of root is set to NULL. We handle this by assigning parent as NULL by default to all newly allocated nodes.  
C++
Java
Python3
C#
Javascript
// C++ program to demonstrate insert operation// in binary search tree with parent pointer#include<bits/stdc++.h> struct Node{    int key;    struct Node *left, *right, *parent;}; // A utility function to create a new BST Nodestruct Node *newNode(int item){    struct Node *temp =  new Node;    temp->key = item;    temp->left = temp->right = NULL;    temp->parent = NULL;    return temp;} // A utility function to do inorder traversal of BSTvoid inorder(struct Node *root){    if (root != NULL)    {        inorder(root->left);        printf("Node : %d, ", root->key);        if (root->parent == NULL)          printf("Parent : NULL \n");        else          printf("Parent : %d \n", root->parent->key);        inorder(root->right);    }} /* A utility function to insert a new Node with   given key in BST */struct Node* insert(struct Node* node, int key){    /* If the tree is empty, return a new Node */    if (node == NULL) return newNode(key);     /* Otherwise, recur down the tree */    if (key < node->key)    {        Node *lchild = insert(node->left, key);        node->left  = lchild;         // Set parent of root of left subtree        lchild->parent = node;    }    else if (key > node->key)    {        Node *rchild = insert(node->right, key);        node->right  = rchild;         // Set parent of root of right subtree        rchild->parent = node;    }     /* return the (unchanged) Node pointer */    return node;} // Driver Program to test above functionsint main(){    /* Let us create following BST              50           /     \          30      70         /  \    /  \       20   40  60   80 */    struct Node *root = NULL;    root = insert(root, 50);    insert(root, 30);    insert(root, 20);    insert(root, 40);    insert(root, 70);    insert(root, 60);    insert(root, 80);     // print inorder traversal of the BST    inorder(root);     return 0;}
// Java program to demonstrate insert operation// in binary search tree with parent pointerclass GfG { static class Node{    int key;    Node left, right, parent;} // A utility function to create a new BST Nodestatic Node newNode(int item){    Node temp = new Node();    temp.key = item;    temp.left = null;    temp.right = null;    temp.parent = null;    return temp;} // A utility function to do inorder traversal of BSTstatic void inorder(Node root){    if (root != null)    {        inorder(root.left);        System.out.print("Node : "+ root.key + " , ");        if (root.parent == null)        System.out.println("Parent : NULL");        else        System.out.println("Parent : " + root.parent.key);        inorder(root.right);    }} /* A utility function to insert a new Node withgiven key in BST */static Node insert(Node node, int key){    /* If the tree is empty, return a new Node */    if (node == null) return newNode(key);     /* Otherwise, recur down the tree */    if (key < node.key)    {        Node lchild = insert(node.left, key);        node.left = lchild;         // Set parent of root of left subtree        lchild.parent = node;    }    else if (key > node.key)    {        Node rchild = insert(node.right, key);        node.right = rchild;         // Set parent of root of right subtree        rchild.parent = node;    }     /* return the (unchanged) Node pointer */    return node;} // Driver Program to test above functionspublic static void main(String[] args){    /* Let us create following BST            50        /     \        30     70        / \ / \    20 40 60 80 */    Node root = null;    root = insert(root, 50);    insert(root, 30);    insert(root, 20);    insert(root, 40);    insert(root, 70);    insert(root, 60);    insert(root, 80);     // print iNorder traversal of the BST    inorder(root);}}
# Python3 program to demonstrate insert operation# in binary search tree with parent pointer # A utility function to create a new BST Nodeclass newNode:    def __init__(self, item):        self.key = item        self.left = self.right = None        self.parent = None # A utility function to do inorder# traversal of BSTdef inorder(root):    if root != None:        inorder(root.left)        print("Node :", root.key, ", ", end = "")        if root.parent == None:            print("Parent : NULL")        else:            print("Parent : ", root.parent.key)        inorder(root.right) # A utility function to insert a new# Node with given key in BSTdef insert(node, key):         # If the tree is empty, return a new Node    if node == None:        return newNode(key)     # Otherwise, recur down the tree    if key < node.key:        lchild = insert(node.left, key)        node.left = lchild         # Set parent of root of left subtree        lchild.parent = node    elif key > node.key:        rchild = insert(node.right, key)        node.right = rchild         # Set parent of root of right subtree        rchild.parent = node     # return the (unchanged) Node pointer    return node # Driver Codeif __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)     # print iNorder traversal of the BST    inorder(root) # This code is contributed by PranchalK
// C# program to demonstrate insert operation// in binary search tree with parent pointerusing System; class GfG{    class Node    {        public int key;        public Node left, right, parent;    }     // A utility function to create a new BST Node    static Node newNode(int item)    {        Node temp = new Node();        temp.key = item;        temp.left = null;        temp.right = null;        temp.parent = null;        return temp;    }     // A utility function to do    // inorder traversal of BST    static void inorder(Node root)    {        if (root != null)        {            inorder(root.left);            Console.Write("Node : "+ root.key + " , ");            if (root.parent == null)            Console.WriteLine("Parent : NULL");            else            Console.WriteLine("Parent : " +                                root.parent.key);            inorder(root.right);        }    }     /* A utility function to insert a new Node with    given key in BST */    static Node insert(Node node, int key)    {        /* If the tree is empty, return a new Node */        if (node == null) return newNode(key);         /* Otherwise, recur down the tree */        if (key < node.key)        {            Node lchild = insert(node.left, key);            node.left = lchild;             // Set parent of root of left subtree            lchild.parent = node;        }        else if (key > node.key)        {            Node rchild = insert(node.right, key);            node.right = rchild;             // Set parent of root of right subtree            rchild.parent = node;        }         /* return the (unchanged) Node pointer */        return node;    }     // Driver code    public static void Main(String[] args)    {        /* Let us create following BST                50            / \            30 70            / \ / \        20 40 60 80 */        Node root = null;        root = insert(root, 50);        insert(root, 30);        insert(root, 20);        insert(root, 40);        insert(root, 70);        insert(root, 60);        insert(root, 80);         // print iNorder traversal of the BST        inorder(root);    }} // This code is contributed 29AjayKumar
<script>// javascript program to demonstrate insert operation// in binary search tree with parent pointer      class Node {            constructor() {                this.key = 0;                this.left = null;                this.right = null;                this.parent = null;            }        }  // A utility function to create a new BST Nodefunction newNode(item){    var temp = new Node();    temp.key = item;    temp.left = null;    temp.right = null;    temp.parent = null;    return temp;} // A utility function to do inorder traversal of BSTfunction inorder(root){    if (root != null)    {        inorder(root.left);        document.write("Node : "+ root.key + " , ");        if (root.parent == null)        document.write("Parent : NULL<br/>");        else        document.write("Parent : " + root.parent.key+"<br/>");        inorder(root.right);    }} /* A utility function to insert a new Node withgiven key in BST */function insert(node , key){    /* If the tree is empty, return a new Node */    if (node == null) return newNode(key);     /* Otherwise, recur down the tree */    if (key < node.key)    {        var lchild = insert(node.left, key);        node.left = lchild;         // Set parent of root of left subtree        lchild.parent = node;    }    else if (key > node.key)    {        var rchild = insert(node.right, key);        node.right = rchild;         // Set parent of root of right subtree        rchild.parent = node;    }     /* return the (unchanged) Node pointer */    return node;} // Driver Program to test above functions      /* Let us create following BST            50        /     \        30     70        / \ / \    20 40 60 80 */    var root = null;    root = insert(root, 50);    insert(root, 30);    insert(root, 20);    insert(root, 40);    insert(root, 70);    insert(root, 60);    insert(root, 80);     // print iNorder traversal of the BST    inorder(root); // This code contributed by umadevi9616</script>
Output : 
Node : 20, Parent : 30 
Node : 30, Parent : 50 
Node : 40, Parent : 30 
Node : 50, Parent : NULL 
Node : 60, Parent : 70 
Node : 70, Parent : 50 
Node : 80, Parent : 70 
Exercise: How to maintain parent pointer during deletion.This article is contributed by Shubham Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
prerna saini
PranchalKatiyar
29AjayKumar
umadevi9616
adnanirshad158
ankita_saini
Binary Search Tree
Binary Search Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Print BST keys in the given range
Find the node with maximum value in a Binary Search Tree
set vs unordered_set in C++ STL
Threaded Binary Tree | Insertion
Floor and Ceil from a BST
Construct BST from given preorder traversal | Set 2
Red Black Tree vs AVL Tree
How to check if a given array represents a Binary Heap?
Construct a Binary Search Tree from given postorder
Find the largest BST subtree in a given Binary Tree | Set 1