TheJVM_1970
12-04-2005, 06:37 PM
Hi, how do i create a Binary Tree using nodes and stacks? I've created a stack interface, and a full and empty exception class but i'm now creating the TreeStack class to create the tree. It uses 3 complete levels, therfore 7 nodes and 7 stacks. Each stack limit is going to be set to maximum 10. I've created the methods and the arrays i think i'll need but i'm not sure how to create the tree itself. Here's the code i have done:
Stack.java:
public interface Stack {
// accessor methods
public int size(); //# return the number of elements stored in the stack
public boolean isEmpty(); //# test whether the stack is empty
public Object top() //# return the top elemet
throws StackEmptyException; //# thrown if called on an empty stack
// update methods
public void push (Object element); //# insert an element onto the stack
public Object pop() //# return and remove the top element of the stack
throws StackEmptyException; //# thrown if called on an empty stack
}
StackEmptyException.java:
public class StackEmptyException extends RuntimeException {
public StackEmptyException(String err) {
super(err);
}
}
StackFullException.java:
public class StackFullException extends RuntimeException {
public StackFullException(String err) {
super(err);
}
}
TreeStack.java
public class TreeStack{
// Create Arrays
// Max nodes needed is 7 (3 complete levels)
BinaryTreeNodes[] bn = new BinaryTreeNodes[7];
// Max stacks needed is 7 (3 complete levels)
BinaryTreeStack[] bs = new BinaryTreeStack[7];
// At each node a stack is created as required
// i.e. the data structure is a tree of stacks.
// Each stack only need to have a maximum capacity of 10.
// Create Binary Tree
BinaryTree[] b = new BinaryTree();
public void input(){
// Get user input
public void printLevelOrder(){
}
public void printPopLevelOrder(){
}
public void printPopInorder(){
}
public void printPopPrerder(){
}
public void printPopRoot(){
}
When attempting to run the program i want it to run by entering input like this:
java TreeStack
1 3 2 printLevelOrder printPopRoot
CONTROL-D (i.e. end of file)
Any help is much appreciated or if anyone knows where i can read up on this topic.
Thanks.
Stack.java:
public interface Stack {
// accessor methods
public int size(); //# return the number of elements stored in the stack
public boolean isEmpty(); //# test whether the stack is empty
public Object top() //# return the top elemet
throws StackEmptyException; //# thrown if called on an empty stack
// update methods
public void push (Object element); //# insert an element onto the stack
public Object pop() //# return and remove the top element of the stack
throws StackEmptyException; //# thrown if called on an empty stack
}
StackEmptyException.java:
public class StackEmptyException extends RuntimeException {
public StackEmptyException(String err) {
super(err);
}
}
StackFullException.java:
public class StackFullException extends RuntimeException {
public StackFullException(String err) {
super(err);
}
}
TreeStack.java
public class TreeStack{
// Create Arrays
// Max nodes needed is 7 (3 complete levels)
BinaryTreeNodes[] bn = new BinaryTreeNodes[7];
// Max stacks needed is 7 (3 complete levels)
BinaryTreeStack[] bs = new BinaryTreeStack[7];
// At each node a stack is created as required
// i.e. the data structure is a tree of stacks.
// Each stack only need to have a maximum capacity of 10.
// Create Binary Tree
BinaryTree[] b = new BinaryTree();
public void input(){
// Get user input
public void printLevelOrder(){
}
public void printPopLevelOrder(){
}
public void printPopInorder(){
}
public void printPopPrerder(){
}
public void printPopRoot(){
}
When attempting to run the program i want it to run by entering input like this:
java TreeStack
1 3 2 printLevelOrder printPopRoot
CONTROL-D (i.e. end of file)
Any help is much appreciated or if anyone knows where i can read up on this topic.
Thanks.