Question: Please Help!! Using Java. This is my third time for posting question, and I don't get the right code. Please put all the methods and

Please Help!!

Using Java. This is my third time for posting question, and I don't get the right code. Please put all the methods and classes! in the bottom, there is the description of the program. It has three classes. And I provide what I have for each method at the bottom.

Exercises - Requirements:

1. The main goal of this assignment is to implement several recursive methods, each is built to manipulate binary tree data structures. Most of these methods are static and contained in a utility class named TreeFactory. You also need a node class similar to the BTNode class in the book (p 490) but it will be an utterly simplified version. As a warm-up exercise to this assignment it is recommended but not mandatory that in a separate project you implement and test all three traversal methods for binary trees we discussed in class. The warm-up exercise is not graded and do not submit it. 2. Add a class named BinaryNode to the project. This class supports the linked representations of binary trees. As for a template, look at the BTNode class specified and implemented in your book. However, for the BinaryNode class generic implementation not needed, the nodes will store integer values the standard methods will not be needed in this exercise except the constructor 3. Add a toString( ) method to the BinaryNode class. The method creates a String description of the tree. The method is analogous to the toString( ) method of HW 4.

The toString() method of HW4:

public String toString(int count){

String field1 = "";

String field2 = "";

if(data == null)

field1 = "data: dummy";

else

field1 = "data: " + data.toString();

if(link == null)

field2 = " \tlink: null in tail!";

else

field2 = " link:"+ link.toString(count + 1);

return field1 +" "+ field2;

}

4. Add the print( ) method we discussed in class to the BinaryNode class such that it displays a binary tree of linked representation to the console with indentation. See the slides #24 25 of Chapter 9.

The slides #24-25:

Please Help!! Using Java. This is my third time for posting question,and I don't get the right code. Please put all the methods

5. Add a class named TreeFactory to the project. All the methods in this class will be static. Following the descriptions below add the methods to the class. 6. powerOfTwo( ). Given any integer n>=0 the method computes recursively 2^n and returns the computed power. The parameter n is of type int, the return type is long. 7. findDepth( ). Given the root of a binary tree (the parameter of the method) the method computes recursively the depth of the tree and returns the depth value.

8. loadOverToArray ( ).

Given the root of a binary tree the method creates an array to represent the same tree.

The method loads over the data from the linked tree to the array and returns the array.

To make the array long enough for the tree, the method calls findDepth( ) and initializes the array to the size of the corresponding full binary tree. To get the size, use the powerOfTwo( ) method.

The method loads the data recursively (double recursion) . It takes two parameters, the root of the tree, and a sliding index for the entry element in the array where the load over operation begins

Hint: suppose index is the name of the parameter, then the array element at index is assigned the (relative) root data, and in the recursive call the parameter 2*index + 1 applies for the left child and 2*index + 2 for the right child 9. loadOverToLinked( ). This methods is NOT recursive!

Given the array representation of a binary tree the method creates a linked binary tree to represent the same data. The array is the parameter for the method

The method loads over the data from the array to the nodes of the linked tree and returns the root (return type BinaryNode)

It may help to declare and instantiate a parallel array of nodes.

Instantiate root with the data of index 0 and make root the 0 entry in the node array.

Run a for loop over the data array from index 1.

For k as current index instantiate a node with the data of index k.

Go back to the parent index from k.

If k is the left child from the parent index, assign the node as the left link of the parent node

Else assign the node as the right link of the parent node

Assign the node as the current index element of the node array 10. displayTree ( ). Given the root of a binary tree (the parameter) the method displays a linked tree arrangement of the data on the console. See a template in Figure1

8 4 12 2 6 10 14

1 3 5 7 9 11 13 15

Figure 1

Hints:

This method is not recursive

Determine the depth, call the findDepth( ) method

Call the loadOverToArray( ) method and follow the array order when printing

Create the spacing strings as you see it best (can be tricky)

A display not sharply accurate is acceptable, but the linked structure must be recognizable.

It is accepted if full binary trees are considered for display only. 11. BST_Builder( ). Given the depth N as a parameter, this method recursively builds a full binary search tree of depth N such that the nodes store the integers 1, 2, 3, , 2 N+1 1

Hints.

The method must return the root of the tree it built

The method shall be double recursive, a recursive call must apply (with decreased depth) to both the left link and right link of the current node

depth is naturally the control parameter and the base case is depth = = 0

it may be helpful to add another sliding parameter to the method, the top number stored in the root of the current sub-tree 12. Add an application class Test to the project, test and document all the methods in both classes BinaryNode and TreeFactory.

Here are all the three classes, but I am not sure about some methods, so can any one help me with it :

BinaryNode classes:

public class BinaryNode {

private Object data;

BinaryNode left;

BinaryNode right;

public BinaryNode(Object initialData, BinaryNode initialLeft, BinaryNode initialRight){

this.data= initialData;

this.left= initialLeft;

this.right= initialRight;

}

public String toString(int count){

String field1 = "";

String field2 = "";

String field3 = "";

if(data == null)

field1 = "";

else

field1 = "data: " + data.toString();

if(left == null)

field2 = " ";

else

field2 = " left:"+ left.toString(count + 1);

if(right == null)

field3= " ";

else

field3= " right"+ right.toString(count + 1);

return field1 +" "+ field2+ " "+ field3;

}

public void print( int depth ) {

int i;

for ( i = 1; i

System.out.print( " " );

System.out.println( data );

if ( left != null )

left.print( depth+1 );

else if ( right != null ) {

for ( i = 1; i

System.out.print( " ");

System.out.println( "--" );

}

if ( right != null )

right.print( depth+1 );

else if ( left != null ) {

for ( i = 1; i

System.out.print( " ");

System.out.println( "--" );

}

}

}

TreeFactory class:

public class TreeFactory {

public static long powerOfTwo(int n){

if(n>= 0){

powerOfTwo(2^n);

}

return n;

}

public static int findDepth(BinaryNode root){

if(root== null)

return 0;

int leftD= findDepth(root.left);

int rightD= findDepth(root.right);

int v= leftD > rightD ? leftD+1 : rightD+1;

return v;

}

public static void loadOverToArray(BinaryNode root, int[] index){

int[] array={8,4,12,2,6,10,14,1,3,5,7,9,11,13,15};

}

}

Test class:

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

root root

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!