# need help with binary tree

• July 9th, 2010, 03:13 AM
vash0047
need help with binary tree
here is the question

Specify and design, and implement a class for binary trees where the nodes are stored in an array list, similar to the way that a complete binary tree is usually stored. However, these binary tree do not need to be complete. The class should have methods to create the first node and to move cursor around the tree. After the first node, new nodes may be added only as children of the cursor.

thanks
• July 9th, 2010, 06:36 AM
Norm
Re: need help with binary tree
Quote:

here is the question
Sorry, I don't see the question. All I see is the definition of an assignment. Questions end in questionMarks(?).

• July 9th, 2010, 09:40 AM
vash0047
Re: need help with binary tree
truthfully this is my assignment but i juz dont know how to store binary nodes in an array list, could anyone help me
• July 9th, 2010, 10:30 AM
Norm
Re: need help with binary tree
Create an ArrayList and use its add() method to store a node in it.
• July 12th, 2010, 08:15 AM
vash0047
Re: need help with binary tree
well i dont have to use add() but i got the answer to the statement i given above but any one plz check that i got it right??

/**
*
* Charlie, Nop, Bum+
*/
import java.util.ArrayList;

public class MyBinaryTree<T extends Comparable<? super T>> {

private ArrayList<T> array;
private int cursor;

public MyBinaryTree() {
array = new ArrayList<T>();
cursor = 1;
}

public void createFirstNode(T x) {
array.set(1, x);
}

public void goToParent() {
if (cursor != 1) {
cursor = cursor / 2;
}
}

public void goToFirstChild() {
cursor = cursor * 2;
}

public void goToSecondChild() {
cursor = (cursor * 2) + 1;
}

array.set(cursor * 2, x);
}

array.set((cursor * 2) + 1, x);
}

public void remove() {
array.set(cursor, null);
}

public void makeEmpty() {
array = new ArrayList<T>();
cursor = 1;
}

public boolean isEmpty() {
return array.get(1) == null;
}

}
• July 12th, 2010, 08:23 AM
Norm
Re: need help with binary tree
Quote:

check that i got it right??
Does the program give the correct results when you run it?