Sudo Placement[1.4] | Jumping the Subtree

Given a binary search tree of n nodes with distinct values. Also given are Q queries. Each query consists of a node value that has to be searched in the BST and skip the subtree that has given node as its root. If the provided node is the root itself then print “Empty” without quotes. After that print the preorder traversal of the BST.

Examples:

Input:
N = 7, Q = 2
BST elements: 8 4 10 15 14 88 64
Query1: 15
Query2: 88

Output: 8 4 10
        8 4 10 15 14

The tree below will be formed from the elements given 
           
               8
            /     \
          4        10 
                     \ 
                     15
                   /    \ 
                  14    88 
                        /
                       64 


Query1 = 15. So, skip the subtree with 15 as root.
The remaining tree is :
                                               
         8
       /    \
     4       10
The preorder traversal of the above tree is: 8 4 10

Query2 = 88. So we skip the subtree with 88 as root. 
The remaining tree is :
          8
      /       \
    4          10
                 \
                  15
                /       
             14                  
The preorder traversal of the above tree is: 8 4 10 15 14    

A naive approach is to traverse the entire tree and store its pre-order traversal. In every query, perform a pre-order traversal treating node as root. Print the entire tree’s pre-order traversal except the elements that are in the pre-order traversal of the tree which treats node as the root.

An efficient approach is to store the entire pre-order traversal of the tree in a container. While finding the pre-order traversal of the tree, store the number of recursive calls from the node and store it in a hash-table(mp). This effectively stores the entire size of the subtree treating any node as the root. While performing every query, print the pre-order traversal of the tree, till the node is found, once it is found, perform a jump of mp[node] steps so that the subtree is skipped.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to insert nodes
// and print the preorder traversal
#include <bits/stdc++.h>
using namespace std;
  
// vector to store pre-order
vector<int> pre;
  
// map to store the heigth
// of every subtree
unordered_map<int, int> mp;
  
// structure to store the BST
struct Node {
    int data;
    Node* left = NULL;
    Node* right = NULL;
};
  
// locates the memory space
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
  
// inserts node in the BST
Node* insertNode(Node* head, int key)
{
    // if first node
    if (head == NULL)
        head = newNode(key);
    else {
  
        // move to left
        if (key < head->data)
            head->left = insertNode(head->left, key);
        // move to right
        else
            head->right = insertNode(head->right, key);
    }
    return head;
}
  
// Function to compute the pre-order
// and compute the height of every sub-tree
int preOrder(Node* head)
{
    // leaf node is null
    if (head == NULL)
        return 0;
  
    pre.push_back(head->data);
  
    mp[head->data] += preOrder(head->left);
    mp[head->data] += preOrder(head->right);
    mp[head->data] += 1;
  
    return mp[head->data];
}
  
// Function to perform every queries
void performQueries(int node)
{
  
    // traverse in the pre-order
    // jump the subtree which has node
    for (int i = 0; i < pre.size();) {
  
        // jump the subtree which has the node
        if (pre[i] == node) {
            i += mp[pre[i]];
        }
  
        // print the pre-order
        else {
            cout << pre[i] << " ";
            i++;
        }
    }
    cout << endl;
}
  
// Driver Code
int main()
{
  
    Node* root = NULL;
  
    /*           8
            /     \
          4        10 
                     
                     15
                   /    \ 
                  14    88 
                        /
                       64  */
  
    root = insertNode(root, 8);
    root = insertNode(root, 4);
    root = insertNode(root, 10);
    root = insertNode(root, 15);
    root = insertNode(root, 14);
    root = insertNode(root, 88);
    root = insertNode(root, 64);
  
    // Pre-order traversal of tree
    preOrder(root);
  
    // Function call to perform queries
    performQueries(15);
    performQueries(88);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to insert nodes
// and print the preorder traversal
import java.util.*;
  
class Node
{
    int data;
    Node left, right;
    Node(int key)
    {
        data = key;
        left = right = null;
    }
}
  
class GFG
{
    // ArrayList to 
    // store pre-order
    static ArrayList<Integer> pre = 
                     new ArrayList<Integer>();
  
   // map to store the heigth
   // of every subtree
   static HashMap<Integer, Integer> mp = 
                  new HashMap<Integer, Integer>();
     
public static Node insertNode(Node head, int key)
{
    // if first node
    if (head == null)
        head = new Node(key);
    else 
    {
  
        // move to left
        if (key < head.data)
            head.left = insertNode(head.left, key);
              
        // move to right
        else
            head.right = insertNode(head.right, key);
    }
    return head;
}
  
public static int preOrder(Node head)
{
    // leaf node is null
    if (head == null)
        return 0;
  
    pre.add(head.data);
  
    mp.put(head.data, head.data + 
                      preOrder(head.left));
    mp.put(head.data, head.data + 
                      preOrder(head.right));
    mp.put(head.data, head.data + 1);
  
    return mp.get(head.data);
}
  
// Function to perform 
// every queries
public static void performQueries(int node)
{
  
    // traverse in the pre-order
    // jump the subtree which has node
    for (int i = 0; i < pre.size();) 
    {
  
        // jump the subtree 
        // which has the node
        if (pre.get(i) == node)
        {
            i += mp.get(pre.get(i));
        }
  
        // print the pre-order
        else 
        {
            System.out.print(pre.get(i) + " ");
            i++;
        }
    }
     System.out.println();
}
      
public static void main (String[] args) 
{
      
Node root = null;
  
/*         8
        /     \
    4     10 
                
                15
            / \ 
            14 88 
                    /
                64 */
  
root = insertNode(root, 8);
root = insertNode(root, 4);
root = insertNode(root, 10);
root = insertNode(root, 15);
root = insertNode(root, 14);
root = insertNode(root, 88);
root = insertNode(root, 64);
  
// Pre-order traversal of tree
preOrder(root);
  
// Function call to 
// perform queries
performQueries(15);
performQueries(88);    
}
}

chevron_right



Output:

8 4 10 
8 4 10 15 14 


My Personal Notes arrow_drop_up

This article is contributed by Team Geeksforgeeks. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.