Javatpoint Logo
Javatpoint Logo

Q. Program to convert Binary Tree to Binary Search Tree.

Explanation

In this program, we need to convert given binary tree to a corresponding binary search tree. A tree is said to be the binary tree if each of the nodes has at most two children. Whereas, the binary search tree is the special case of the binary tree in which all the nodes to the left of root node should be less than root node and nodes to the right should be greater than root node.

This problem can be resolved by converting given binary tree to its corresponding array representation. Sort the array. Calculate the middle node from array elements as it will become the root node of the corresponding binary search tree.

Program to convert Binary Tree to Binary Search Tree

Algorithm

  1. Define Node class which has three attributes namely: data left and right. Here, left represents the left child of the node and right represents the right child of the node.
  2. When a node is created, data will pass to data attribute of the node and both left and right will be set to null.
  3. Define another class which has two attribute root and treeArray.
    1. Root represents the root node of the tree and initializes it to null.
    2. treeArray will store the array representation of the binary tree.
  4. convertBTBST() will convert binary tree to the corresponding binary search tree:
    1. It will convert the binary tree to corresponding array by calling convertBTtoArray().
    2. Sort the resultant array from step 1 in ascending order.
    3. Convert the array to the binary search tree by calling createBST().
  5. calculateSize() will count the number of nodes present in the tree.
  6. convertBTtoArray() will convert binary tree to its array representation by traversing the tree and adding elements to treeArray.
  7. createBST() will create a corresponding binary search tree by selecting a middle node of sorted treeArray as it the root node. treeArray will be divided into two parts i.e [0, mid-1] and [mid+1, end]. Recursively find middle node from each array to create left subtree and right subtree respectively.
  8. Inorder() will display the nodes of the tree in inorder fashion, i.e., left child followed by root node followed by the right child.

Solution

Python

Output:

Inorder representation of binary tree: 
4 2 5 1 6 3 7 
Inorder representation of resulting binary search tree: 
1 2 3 4 5 6 7 

C

Output:

Inorder representation of binary tree: 
4 2 5 1 6 3 7 
Inorder representation of resulting binary search tree: 
1 2 3 4 5 6 7 

JAVA

Output:

Inorder representation of binary tree: 
4 2 5 1 6 3 7 
Inorder representation of resulting binary search tree: 
1 2 3 4 5 6 7 

C#

Output:

Inorder representation of binary tree: 
4 2 5 1 6 3 7 
Inorder representation of resulting binary search tree: 
1 2 3 4 5 6 7 

PHP

Output:

Inorder representation of binary tree: 
4 2 5 1 6 3 7 
Inorder representation of resulting binary search tree: 
1 2 3 4 5 6 7 

Next TopicPrograms List





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA