Help with binary trees please
I have created 3 classes, binaryTreeTraversal, this has four traversal methods which also has a method to print out to the screen.
I have binaryTreeNode that has my leftChild, rightChild and Object element.
I also have an arrayQueue that stores the nodes etc..
What I am trying to do to test it is to automatically input data, (e.g A+B) and print it in each traversal.
Code:
public class TestBed
{
public void TestBed()
{
binaryTreeTraversal traversal = new binaryTreeTraversal();
BinaryTreeNode t = new BinaryTreeNode();
t.leftChild = new BinaryTreeNode();
t.rightChild = new BinaryTreeNode();
t.rightChild.element = new BinaryTreeNode();
t.leftChild.element = new BinaryTreeNode();
Object element = "A+B"; //Don't think this is right, but thats what I want to print out
traversal.inOrder(t); //Calls inOrder and print out
traversal.levelOrder(t); //Calls levelOrder and prints out
traversal.postOrder(t); //Calls postOrder and prints out
traversal.preOrder(t); //Calls preOrder and prints out
}
}
The output I get is :
BinaryTreeNode@375ef3ab
null
BinaryTreeNode@3acf7bf3
null
BinaryTreeNode@375ef3ab
BinaryTreeNode@3acf7bf3
BinaryTreeNode@375ef3ab
BinaryTreeNode@3acf7bf3
null
null
BinaryTreeNode@375ef3ab
BinaryTreeNode@3acf7bf3
I hope this makes sense and thank you to anybody who can help.
Re: Help with binary trees please
Your BinaryTreeNode class doesn't implement a toString() method, that's why you get the funny output (generated by Object.toString()). I don't understand your test code, e.g. can an element part of a node be another binary tree? According to your code it can, according to my book it can't.
kind regards,
Jos
Re: Help with binary trees please
Thanks very much :)
When you say an element part of a node do you mean :
t.rightChild.element = new BinaryTreeNode();
t.leftChild.element = new BinaryTreeNode();
Re: Help with binary trees please
Quote:
Originally Posted by
sim18
Thanks very much :)
When you say an element part of a node do you mean :
t.rightChild.element = new BinaryTreeNode();
t.leftChild.element = new BinaryTreeNode();
Yep, that's the part I don't understand; I understand what leftChild and rightChild mean so I guessed that the 'element' part was the data part of a node ...
kind regards,
Jos