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
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(?).
Do you have any specific questions? Go ahead and ask them.
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
Re: need help with binary tree
Create an ArrayList and use its add() method to store a node in it.
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;
}
public void addFirstChildNode(T x) {
array.set(cursor * 2, x);
}
public void addSecondChildNode(T 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;
}
}
Re: need help with binary tree
Quote:
check that i got it right??
Does the program give the correct results when you run it?