Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of Binary Tree.
This solution will use Sets of C++ STL instead of array based solution.
Examples:
Example 1
Input:
10
/ \
2 7
/ \
8 4
Output:
8
/ \
4 10
/ \
2 7
Example 2
Input:
10
/ \
30 15
/ \
20 5
Output:
15
/ \
10 20
/ \
5 30
Solution
- Copy the items of binary tree in a set while doing inorder traversal. This takes O(n log n) time. Note that set in C++ STL is implemented using a Self Balancing Binary Search Tree like Red Black Tree, AVL Tree, etc
- There is no need to sort the set as sets in C++ are implemented using Self-balancing binary search trees due to which each operation such as insertion, searching, deletion etc takes O(log n) time.
- Now simply copy the items of set one by one from beginning to the tree while doing inorder traversal of tree. Care should be taken as when copying each item of set from its beginning, we first copy it to the tree while doing inorder traversal, then remove it from the set as well.
Now the above solution is simpler and easier to implement than the array based conversion of Binary tree to Binary search tree explained here- Conversion of Binary Tree to Binary Search tree (Set-1), where we had to separately make a function to sort the items of the array after copying the items from tree to it.
C++ program to convert a binary tree to binary search tree using set.
/* CPP program to convert a Binary tree to BST using sets as containers. */#include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node *left, *right; }; // function to store the nodes in set while // doing inorder traversal. void storeinorderInSet(Node* root, set<int>& s) { if (!root) return; // visit the left subtree first storeinorderInSet(root->left, s); // insertion takes order of O(logn) for sets s.insert(root->data); // visit the right subtree storeinorderInSet(root->right, s); } // Time complexity = O(nlogn) // function to copy items of set one by one // to the tree while doing inorder traversal void setToBST(set<int>& s, Node* root) { // base condition if (!root) return; // first move to the left subtree and // update items setToBST(s, root->left); // iterator initially pointing to the // beginning of set auto it = s.begin(); // copying the item at beginning of // set(sorted) to the tree. root->data = *it; // now erasing the beginning item from set. s.erase(it); // now move to right subtree and update items setToBST(s, root->right); } // T(n) = O(nlogn) time // Converts Binary tree to BST. void binaryTreeToBST(Node* root) { set<int> s; // populating the set with the tree's // inorder traversal data storeinorderInSet(root, s); // now sets are by default sorted as // they are implemented using self- // balancing BST // copying items from set to the tree // while inorder traversal which makes a BST setToBST(s, root); } // Time complexity = O(nlogn), // Auxiliary Space = O(n) for set. // helper function to create a node Node* newNode(int data) { // dynamically allocating memory Node* temp = new Node(); temp->data = data; temp->left = temp->right = NULL; return temp; } // function to do inorder traversal void inorder(Node* root) { if (!root) return; inorder(root->left); cout << root->data << " "; inorder(root->right); } int main() { Node* root = newNode(5); root->left = newNode(7); root->right = newNode(9); root->right->left = newNode(10); root->left->left = newNode(1); root->left->right = newNode(6); root->right->right = newNode(11); /* Constructing tree given in the above figure 5 / \ 7 9 /\ / \ 1 6 10 11 */ // converting the above Binary tree to BST binaryTreeToBST(root); cout << "Inorder traversal of BST is: " << endl; inorder(root); return 0; } |
Inorder traversal of BST is: 1 5 6 7 9 10 11
Time Complexity : O(n Log n)
Auxiliary Space : (n)
Recommended Posts:
- Binary Tree to Binary Search Tree Conversion
- Minimum swap required to convert binary tree to binary search tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Optimal Binary Search Tree | DP-24
- Floor in Binary Search Tree (BST)
- Search a node in Binary Tree
- Binary Search Tree | Set 2 (Delete)
- Iterative Search for a key 'x' in Binary Tree
- Binary Search Tree | Set 1 (Search and Insertion)
- How to handle duplicates in Binary Search Tree?
- Print all even nodes of Binary Search Tree
- Implementation of Binary Search Tree in Javascript
- Print all odd nodes of Binary Search Tree
- Construct a Binary Search Tree from given postorder
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.



