GeeksforGeeks » Interview Questions

Amazon Banglore Online Written

(4 posts)
  • Started 3 months ago by gupta5035
  • Latest reply from kartik

Tags:

  1. gupta5035
    Member
    Posted 3 months ago #

    You are given a function getInorderSuccessor which takes a BST (Binary Search Tree) as it's parameter. Every node has an extra pointer "next" , which is intialized to null, fill next with node pointers which represent Inorder Successor.

    In a binary tree, inorder successor of a node is the next node in inorder traversal of the binary tree. Inorder successor is NULL for the last node in inorder traversal.

    In BST, inorder successor of an input node can also be defined as the node with the smallest key greater than the key of input node. So, it is sometimes important to find next node in sorted orde

  2. kartik
    Moderator
    Posted 3 months ago #

    Traverse the node in reverse Inorder and store the pointer to previously visited node

    /* Set next of all descendents of p */
    void connectRecur(struct node* p)
    {
        // Initialize next as NULL as the first visited node would be the righmost node
        static struct node *next = NULL;
    
        if(p)
        {
            connectRecur(p->right);
            p->next = next;
            next = p;
            connectRecur(p->left);
        }
    }
  3. kartik
    Moderator
    Posted 3 months ago #

    Following is complete program to test it.

    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int data;
        struct node *left;
        struct node *right;
        struct node *next;
    };
    
    /* Set next of all descendents of p */
    void connectRecur(struct node* p)
    {
        // Base case
        static struct node *next = NULL;
        if(p)
        {
            connectRecur(p->right);
            p->next = next;
            next = p;
            connectRecur(p->left);
        }
    }
    
    void print(struct node *root)
    {
        while(root)
        {
            printf("%d  ", root->data);
            root = root->next;
        }
    }
    
    /* UTILITY FUNCTIONS */
    /* Helper function that allocates a new node with the
       given data and NULL left and right pointers. */
    struct node* newnode(int data)
    {
        struct node* node = (struct node*)
                            malloc(sizeof(struct node));
        node->data = data;
        node->left = NULL;
        node->right = NULL;
        node->next = NULL;
    
        return(node);
    }
    
    /* Driver program to test above functions*/
    int main()
    {
    
        /* Constructed binary tree is
                  10
                /   \
              8      12
            /
          3
        */
        struct node *root = newnode(10);
        root->left        = newnode(8);
        root->right       = newnode(12);
        root->left->left  = newnode(3);
    
        // Populates nextRight pointer in all nodes
        connectRecur(root);
        print(root->left->left);
    
        getchar();
        return 0;
    }
    
  4. kartik
    Moderator
    Posted 3 months ago #

    Following function is a variation of above that doesn't use static variable.

    void connect(struct node *root)
    {
       struct node *next = NULL;
       connectRecur(root, &next);
    }
    
    /* Set next of all descendents of p */
    void connectRecur(struct node* p, struct node **next_ref)
    {
        if(p)
        {
            connectRecur(p->right, next_ref);
            p->next = *next_ref;
            *next_ref = p;
            connectRecur(p->left, next_ref);
        }
    }
    

Reply

You must log in to post.

RSS feed for this topic