Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello there, I am having a difficulty in my Data Structures homework and I need

ID: 3833143 • Letter: H

Question

Hello there,

I am having a difficulty in my Data Structures homework and I need the solutions for these 2 questions urgently if u could solve them..

To class Tree, add the following functions:

* nodesInLevel(int x): returns number of nodes in a specific level x

* BreadthFirst(): which is a traversal approach that prints the nodes of the tree level by level, for this tree this method should print 11 6 19 4 8 17 43 5 10 31 49

the tree looks like this 11

6 19

4 8 17 43

5 10 31 49

if u didn't understand the tree I tried to draw, 11 has 2 children (6 and 19), 6 has 2 children(4 and 8), 19 has 2 children(17 and 43), 4 has one right child which is 5,

8 has one right child which is 10,

17 has no children (it's a leaf), and finally 43 has 2 children (31 and 49)..

Thank you guys in advance I really appreciate it...

Explanation / Answer

since the language is not defined so using java

and even the tree class structure is not defined so assung the below structure of the binary search tree

class Tree{
   int value;
   Tree leftChild;
   Tree rightChild;
  
   Tree(int val){
       this.value = val;
       leftChild = null;
       rightChild = null;
   }
}

public class mainProg(){

   void nodesInLevel(int x)
   {
   nodesInLevel(root, x);// calling another function with the root of the tree and level x
   }

   void nodesInLevel(Tree node, int x){
   if (node == null)
   return;
   if (x == 0)
   {
   System.out.print(node.value + " ");
   return;
   }
   else
   {
   nodesInLevel(node.leftChild, x-1);
   nodesInLevel(node.rightChild, x-1);
   }
   }

   void BreadFirst()
{
int h = treeHeight(root);
int i;
for (i=1; i<=h; i++)
BreadFirstUtil(root, i);
}

   void BreadFirstUtil(Tree node ,int h)
{
if (node == null)
return;
if (h == 1)
System.out.print(node.value + " ");
else if (h > 1)
{
BreadFirstUtil(node.leftChild, h-1);
BreadFirstUtil(node.rightChild, h-1);
}
}

   int treeHeight(Tree node)
   {
       if (node == null)
           return 0;
       else
       {
           int lefth= treeHeight(node.leftChild);
           int righth = treeHeight(node.rightChild);
           if (lefth > righth)
               return(lefth+1);
           else
               return(righth+1);
       }
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote