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    ---------------------------

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.

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 

 

---------------------------

 

 

DriverMain.java
ProblemSolution.java ® entrypoint.z
1• import java.util.*;
2 import java.lang.*;
3 import java.io.*;
4 //Your program will be evaluated by this DriverMain class and several test cases.
5
6 - public class DriverMain {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
7
8
int N = s.nextInt();
int A[]
9
= new int[N];
for (int i = 0; i < N; i++) {
10
11 .
12
A[i] = s.nextInt();
13
}
14
ProblemSolution problemSolution = new ProblemSolution();
15
problemSolution.solution(A, N);
16
}
17 }
Transcribed Image Text:DriverMain.java ProblemSolution.java ® entrypoint.z 1• import java.util.*; 2 import java.lang.*; 3 import java.io.*; 4 //Your program will be evaluated by this DriverMain class and several test cases. 5 6 - public class DriverMain { public static void main(String[] args) { Scanner s = new Scanner (System.in); 7 8 int N = s.nextInt(); int A[] 9 = new int[N]; for (int i = 0; i < N; i++) { 10 11 . 12 A[i] = s.nextInt(); 13 } 14 ProblemSolution problemSolution = new ProblemSolution(); 15 problemSolution.solution(A, N); 16 } 17 }
DriverMain.java
ProblemSolution.java
entrypoint.cz
1- import java.util.*;
2 import java.lang.*;
3 import java.io.*;
4 v public class ProblemSolution {
5
public void solution(int[] bfs, int N) {
6
//write your recursive code here to convert bfs to preorder traversal of a complete
7
//You can add new methods
8
9
10
11 }
12 }
13
Transcribed Image Text:DriverMain.java ProblemSolution.java entrypoint.cz 1- import java.util.*; 2 import java.lang.*; 3 import java.io.*; 4 v public class ProblemSolution { 5 public void solution(int[] bfs, int N) { 6 //write your recursive code here to convert bfs to preorder traversal of a complete 7 //You can add new methods 8 9 10 11 } 12 } 13
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Quicksort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education