Given an array that represents Breadth First Search or BFS traversal of a Complete Binary Search Tree, implement a recursive void method to print the preOrder traversal of the same Complete Binary Search Tree. You do not need to construct the BST. A Complete Binary Search Tree is a BST in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Following is an example of a Complete Binary Search Tree: A. BFS traversal array (level by level from left to right): bfs[] = {50, 35, 55, 30, 45} B. PreOrder traversal (middle, left, right) : 50 35 30 45 55 Your recursive method "void convertBFStoPreOrder" will get an array A as an input and will print B. Hints: In the given BFS traversal array, bfs[], if i is the index of a parent, 2i+1 will give you the index of the left child, and 2i+2 will give you the index of the right child. "Assume that input array is always correct and represent a correct BFS traversal of a Complete BST." ------------------------------------------------------------------------------------------------ Sample input1: 5 50 35 55 30 45 Sample output1: 50 35 30 45 55 ------------------------------------------------------------------------------ Sample input2: 13 45 35 55 25 40 50 60 20 27 37 43 47 54 Sample output2: 45 35 25 20 27 40 37 43 55 50 47 54 60 ---------------------------
Given an array that represents Breadth First Search or BFS traversal of a Complete Binary Search Tree, implement a recursive void method to print the preOrder traversal of the same Complete Binary Search Tree. You do not need to construct the BST.
A Complete Binary Search Tree is a BST in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Following is an example of a Complete Binary Search Tree:
A. BFS traversal array (level by level from left to right): bfs[] = {50, 35, 55, 30, 45}
B. PreOrder traversal (middle, left, right) : 50 35 30 45 55
Your recursive method "void convertBFStoPreOrder" will get an array A as an input and will print B.
Hints:
In the given BFS traversal array, bfs[], if i is the index of a parent, 2i+1 will give you the index of the left child, and 2i+2 will give you the index of the right child.
"Assume that input array is always correct and represent a correct BFS traversal of a Complete BST."
------------------------------------------------------------------------------------------------
Sample input1:
5
50 35 55 30 45
Sample output1:
50 35 30 45 55
------------------------------------------------------------------------------
Sample input2:
13
45 35 55 25 40 50 60 20 27 37 43 47 54
Sample output2:
45 35 25 20 27 40 37 43 55 50 47 54 60
---------------------------
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images