Binary Search Tree insert with Parent Pointer

2.6

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++ program to demonstrate insert operation
// in binary search tree with parent pointer
#include<stdio.h>
#include<stdlib.h>

struct Node
{
    int key;
    struct Node *left, *right, *parent;
};

// A utility function to create a new BST Node
struct 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 BST
void 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 functions
int 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 iNoder traversal of the BST
    inorder(root);

    return 0;
}

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 contribute@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

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.6 Average Difficulty : 2.6/5.0
Based on 5 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.