GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

construct a binary tree

You are given an array of numbers. Based on values present in the array, construct a binary tree. 
Input: 
Index 0 1 2 3 4 5 
Array -1 0 0 1 2 3 
In the array, first we will find -1, the index of -1 within the array will be root of our tree. 
Tree 

Then find the root node value within the array. In the given example, they are present at index position 1 & 2.Now 1 & 2 will be our children. As 1 is less than 2, 1 should be our left child and 2 our right child 
Tree 
   0
 /  \
1    2 
Then, repeat the process for all children, in this case 1 & 2 
Final Tree: 
   0
  /   \
1      2
  \      \
    3    4
     \
     5 

3 become right child of 1 as there is only children of 1 possible. In such case, check if index value is greater than parent or not. If greater, the child would be right child else left child. 
The final output should be reverse level order traversal 
Output: 5 3 4 1 2 0 

asked Sep 24, 2015 by nish

1 Answer

// Author :: Gaurav Ahirwar
#include<bits/stdc++.h>
using namespace std;
  
struct Pair {
    int x;
    int y;
};
  
struct node {
      
    int data;
    struct node *left;
    struct node *right;
};
typedef struct node* Node;
  
Node newNode(int key) {
      
    Node temp = new node;
    temp->data = key;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Search the indices corresponding to key
Pair getindex(int arr[], int index[], int n, int key) {
      
    Pair temp;
    temp.x = -1;
    temp.y = -1;
      
    for(int i = 0; i < n; i++) {
          
        if(arr[i] == key) {
              
            if(temp.x == -1) temp.x = index[i];
            else temp.y = index[i];
        }
    }
      
    return temp;
}
  
void inorder(Node root) {
    if(!root) return;
      
    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
}
  
Node solve(int arr[], int index[], int n, int key) {
     
     
    // Get the indices corresponding to the value "key" in index array
    Pair temp = getindex(arr, index, n, key);
    Node root;  // Node at this call
     
    // This is a special condition which will be executed only in the first function call.
    // If this the very first call and than make the corresponding index as root
    // and once again get the indices corresponding to the root value.
    if(key == -1) {
        root = newNode(temp.x);
        temp = getindex(arr, index, n, temp.x);
    }
     
    // If this call is not the very first call than create a new node with key value.
    else root = newNode(key);
     
    // if Node(key) has two children, than recur accodingly as per question requirement
    if(temp.x != -1 && temp.y != -1) {
         
        if(temp.x <= temp.y) {
                  
            root->left = solve(arr, index, n, temp.x);
            root->right = solve(arr, index, n, temp.y);  
        }
              
        else {
                  
            root->right = solve(arr, index, n, temp.x);
            root->left = solve(arr, index, n, temp.y);   
        }
    }   
     
    // If Node(key) has only one child
    else if(temp.x != -1) {
          
         // If it's value is smaller than parent value than make it left child else make it right child
        if(temp.x <= root->data) root->left = solve(arr, index, n, temp.x);
        else root->right = solve(arr, index, n, temp.x);
    }
  
    // return this node for parent function call
    return root;
}
  
int main() {
      
    int index[] = {0, 1, 2, 3, 4, 5};
    int arr[] =   {-1, 0, 0, 1, 2, 3};
    int n = sizeof arr / sizeof arr[0];
     
    // Passing initially root i.e., -1
    Node root = solve(arr, index, n, -1);
    // Traverse the created tree.
    inorder(root);
    return 0;
}

 

answered Sep 24, 2015 by Mr.Lazy
edited Oct 6, 2015 by Mr.Lazy
...