Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this binary search tree each Node holds a char value. // Method wordsAtDepth() is called in TreeMain. This method will display all Node's char

In this binary search tree each Node holds a char value. // Method wordsAtDepth() is called in TreeMain. This method will display all Node's char values in the Binary // // // Search Tree, but the nodes at the same depth will be displayed on the same line as each other. void wordsAtDepth1() { int depth = 0; int numOfNodes; Queue queue = new Queue(32); // Make a new Queue called queue. queue.insert(root); // Insert the root into the queue // While queue is not empty we will continue the while loop while (!queue.isEmpty()) { // numOfNodes keeps track of how many times we are going to continue the for loop in the current while //loop. numOfNodes = (int) Math.pow(2, depth); // for loop will continue until i is equal to or is greater than numOfNodes. for (int i = 0; i < numOfNodes; i++) { // Current will equal to the removed element from the queue. Node Current = queue.remove(); // If Current does not equal null then print current and insert its' left and right child. // If Current does equal null then do nothing go to the next iteration of the for loop if(Current != null){ System.out.print(Current.cData); queue.insert(Current.leftChild); queue.insert(Current.rightChild); } } // Makes new line every time we finish the while loop. Which means new line for the next iteration of the // while loop to separate the different depths. System.out.println(" "); depth++; } }

Using OPTIMAL, OUTPUT: O IP AMT L This is correct, but if I use a word like SUPERMAN: S PU ER AMN

SPUERAMNPUERAMN ERAMNAMNNEMNNAMNN AMNNNNAMNNNNNNNN NNNNNNNN

I really want to use the numOfNodes = (int) Math.pow(2, depth);, since the number of nodes at a certain depth is 2^Depth. But it seems to not work. Please use numOfNodes = (int) Math.pow(2, depth);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions