Q. Program to convert Binary Tree to Binary Search Tree.ExplanationIn 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. Algorithm
SolutionPythonOutput: 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 COutput: 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 JAVAOutput: 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 PHPOutput: 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 |