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

YOUR CODE MUST (COPY & PASTE) COMPILE AND PASS THE TWO DRIVERS BELOW!!! I NEED C

ID: 3540017 • Letter: Y

Question

YOUR CODE MUST (COPY & PASTE) COMPILE AND PASS THE TWO DRIVERS BELOW!!!

I NEED COMMENTS USING // TO EXPLAIN YOUR CODE!!!

THANK YOU IN ADVANCE!

You will write two methods for this assignment. The first method you will write will be the insert method. It has the following header:

The method will insert the value num into t in such a way that the special structure of trees (i.e., that smaller values are always on the left of a tree object, and larger values are on the right). The psuedocode for the method is as follows:


The second method is called toArray. It has the following header:

The method, when called toArray(t, null, 0), creates and returns an array containing all the elements of t in ascending order. The psuedocode for the method is as follows:

Explanation / Answer

/* Save this file as HW10.java

this file uses the both of your attached codes (Tree class, and TreeMethods class)

Tested with both your drivers

Comment on answer if any doubt ( please clear your doubts before rating )

*/

public class HW10

{

public static Tree insert(Tree t, int num)

{

if(t== null)

{

t=new Tree(num,null,null);

return t;

}

else

{

t.size++;

if( num<t.value)

t.left=insert(t.left,num);

else

t.right=insert(t.right,num);

  

return t;

}

}

  

public static int[] toArray(Tree t, int[] array, int sL)

{

if(t==null)

{

return (new int[0]);

}

else if(array == null)

{

int[] newarray=new int[t.size];

return toArray(t,newarray,sL);

}

else

{

toArray(t.left,array,sL);

  

if(t.left!=null)

sL+=t.left.size;

  

array[sL]=t.value;

toArray(t.right,array,sL+1);

return array;

}

}

}