Binary Tree to Binary Search Tree Conversion using STL set C++?

C++Server Side ProgrammingProgramming

In case of a given Binary Tree, convert it to a Binary Search Tree in such a way that keeps the original structure of Binary Tree intact.

Sets of C++ STL will be used by this solution instead of array based solution.

Examples

Example 1

Input

     11
    /  \
   3    8
/     \
9 5

Output

     9
   /   \
  5     11
 /        \
3        8

Example 2

Input

      11
     /   \
    31    16
   /        \
 21         6

Output

     16
    /   \
  11     21
 /         \
 6          31

Solution

  • We have to copy the items of binary tree in a set while performing inorder traversal. This consumes O(n log n) time. Note that set in C++ STL(Standard Template Library) is implemented using a Self Balancing Binary Search Tree like Red Black Tree, AVL Tree, etc.

  • There is no need for sorting the set because sets in C++ are used implementing Self-balancing binary search trees due to which each operation such as insertion, searching, deletion etc consumes O(log n) time.

  • Now we can easily copy the elements of set one by one from starting to the tree while performing inorder traversal of tree. Care should be taken as when copying each item of set from its starting, we first copy it to the tree while performing inorder traversal, then delete it from the set as well.

  • At present the above mentioned solution is simpler and easier to implement than the array based conversion of Binary tree to Binary search tree explained here.

The following program, to convert a binary tree to binary search tree(BST) using set, is explained here.

Example

/* CPP program for converting a Binary tree to BST implementing sets as containers. */
#include <bits/stdc++.h>
using namespace std;
struct Node1 {
   int data;
   struct Node1 *left, *right;
};
// function for storing the nodes in set while performing inorder traversal.
void storeinorderInSet(Node1* root1, set<int>& s){
   if (!root1)
   return;
   // Left subtree is visited first
   storeinorderInSet(root1->left, s);
   Order of O(logn) for sets is taken by insertion
   s.insert(root1->data);
   // We visit the right subtree
   storeinorderInSet(root1->right, s);
} // Time complexity = O(nlogn)
// function for copying elements of set one by one to the tree while performing inorder traversal
void setToBST(set<int>& s, Node1* root1){
   // base condition
   if (!root1) return;
   // We first move to the left subtree and update elements
   setToBST(s, root1->left);
   // iterator initially pointing to the starting of set
   auto it = s.begin();
   // We copy the element at sarting of set(sorted) to the tree.
   root1->data = *it;
   // now we erase the starting element from set.
   s.erase(it);
   // now we move to right subtree and update elements
   setToBST(s, root1->right);
}
// T(n) = O(nlogn) time
// We convert Binary tree to BST.
void binaryTreeToBST(Node1* root1){
   set<int> s;
   // We populate the set with the tree's inorder traversal data
   storeinorderInSet(root1, s);
   // At present sets are by default sorted as they are used
   implementing self-balancing BST
   // We copy elements from set to the tree while inorder traversal
   which makes a BST
   setToBST(s, root1);
}
// Time complexity = O(nlogn),
// Auxiliary Space = O(n) for set.
// helper function for creating a node
Node1* newNode(int data){
   // dynamically allocating memory
   Node1* temp = new Node1();
   temp->data = data;
   temp->left = temp->right = NULL;
   return temp;
}
// function for doing inorder traversal
void inorder(Node1* root1){
   if (!root1)
   return;
   inorder(root1->left);
   cout<< root1->data << " ";
   inorder(root1->right);
}
int main(){
   Node1* root1 = newNode(6);
   root1->left = newNode(8);
   root1->right = newNode(10);
   root1->right->left = newNode(11);
   root1->left->left = newNode(2);
   root1->left->right = newNode(7);
   root1->right->right = newNode(12);
   /* Building tree given in the following figure
      6
      / \
      8 10
      /\ / \
      2 7 11 12 */
   // We convert the above Binary tree to BST
   binaryTreeToBST(root1);
   cout<< "Inorder traversal of BST is: " << endl;
   inorder(root1);
   return 0;
}

Output

Inorder traversal of BST is:
1 5 6 7 9 10 11

Time Complexity is denoted as : O(n Log n)

Auxiliary Space is denoted as : (n)

raja
Published on 29-Jan-2020 16:35:43
Advertisements