-
Help with this binary tree
I have created a arrayQueue and binaryTreeTraversal class that has different traversal methods, both of these classes work :)
I have got my BinaryTreeNode class
Code :
public class BinaryTreeNode
{
}
}
I basically want to print out A+B in the traversal, but when i compile it says actual and formal argument lists differ in length.
Is this to do with my BinaryTreeNode class?
Thanks very much
-
Re: Help with this binary tree
Quote:
when i compile it says
Please copy the full text of the error message and paste it here. We need to see the full text to be able to know where and what the exact problem is.
-
Re: Help with this binary tree
"no suitable constructor found for BinaryTreeNode(java.lang.String,BinaryTreeNode,Bin aryTreeNode)
constructor BinaryTreeNode.BinaryTreeNode(BinaryTreeNode,Binar yTreeNode) is not applicable (actual and formal argument lists differ in length)
constructor BinaryTreeNode.BinaryTreeNode(java.lang.Object) is not applicable (actual and formal argument lists differ in length)"
Thanks
-
Re: Help with this binary tree
Quote:
no suitable constructor found for
The compiler can not find a constructor in the class that has a matching argument list.
Either change the new statement so its args match the existing constructor's args
Or add a new constructor to the class that has args that match how you want to use it.
-
Re: Help with this binary tree
Thank you :D
It works now, next time I will be sure to read the error message properly
-
Re: Help with this binary tree
Sorry to ask another question.
My tree works perfect now printing out, A+B or A-B / C in all four different traversals.
Here is my code:
Code :
BinaryTreeNode n1 = new BinaryTreeNode("A");
I have done nothing different from what I can tell,
Thanks for any help
-
Re: Help with this binary tree
The posted code does not compile without errors. Correct the errors if you want anyone to help you test the code.
-
Re: Help with this binary tree
I don't understand what you mean sorry?
-
Re: Help with this binary tree
Please post the current version of the code that compiles, executes and shows the problem.
What is posted here now, does not compile without errors.
-
Re: Help with this binary tree
Everything compiles fine and there are no errors.
The only error as I said before is that that output of the PostOrder traversal result is : AB*C/D^E^, but the postOrder should not be like that.
To insure I get the correct postOrder traversal from: (A*B) / (C^D^E), I was just wondering if I would have to put brackets to somehow tell the computer to do one bit first because if I dont I get the output : AB*C/D^E^ instead of AB*CDE^^/
Hope I am a lot clearer now, thanks
-
Re: Help with this binary tree
If I can't compile the code I can't test it. If you want help post code that compiles and executes and shows the problem.
-
Re: Help with this binary tree
This will compile and run the test
-
Re: Help with this binary tree
It doesn't execute. I get this:
Error: Main method not found in class TestingBTN, please define the main method as:
-
Re: Help with this binary tree
Sorry, code should work now
-
Re: Help with this binary tree
The code executes ok now. Do you have a design or algorithm for what the code is supposed to do?
I don't see any comments that describe the logic of what it is supposed to do.
Without the algorithm documented in the code, I can't tell if the code is correctly following the algorithm.
-
Re: Help with this binary tree
From the test class I am creating new binaryTreeNodes that make up: (A*B) / (C^D^E), from the Traversal class it will then go through the nodes and output each node onto the screen in the order they should be. Example:
Code :
public void inOrder(BinaryTreeNode t)
{
ArrayQueue q = new ArrayQueue();
if (t != null)
{
inOrder(t.leftChild);
visit(t);
inOrder(t.rightChild);
}
}
that will output: A*B / C^D^E, ArrayQueue class is just used to store the nodes.
My problem is that when outputting in postOrder, it does not output the correct sequence for this.
If I changed the code for A-B / C
Code :
BinaryTreeNode n1 = new BinaryTreeNode("A");
BinaryTreeNode n2 = new BinaryTreeNode("B");
BinaryTreeNode n3 = new BinaryTreeNode("-",n1,n2);
BinaryTreeNode n4 = new BinaryTreeNode("C");
BinaryTreeNode n5 = new BinaryTreeNode("/", n3, n4);
System.out.println("\nInOrder Traversal: ");
traversal.inOrder(n5);
System.out.println("\npostOrder Traversal: ");
traversal.postOrder(n5);
This would print perfectly in both inOrder and postOrder
-
Re: Help with this binary tree
Quote:
problem is that when outputting in postOrder, it does not output the correct sequence
What is the algorithm for transiting in post order? Does your code follow that algorithm?
-
Re: Help with this binary tree
1. Traverse left sub tree
2. Traverse right sub tree
3. Visit root
Code :
public void postOrder(BinaryTreeNode t)
{
ArrayQueue q = new ArrayQueue();
if (t != null)
{
postOrder(t.leftChild);
postOrder(t.rightChild);
visit(t);
}
}
I assumed that was correct?
-
Re: Help with this binary tree
-
Re: Help with this binary tree
it is just 'to the power of'
-
Re: Help with this binary tree
then (C^D^E)
would be: (C^(D^E))
-
Re: Help with this binary tree
Does that mean I will have to include the brackets in me code?
Example:
Code :
BinaryTreeNode n1 = new BinaryTreeNode("A");
-
Re: Help with this binary tree
It may mean that the way you load the binary tree doesn't preserve the correct order of operations.
I don't think you desired output: AB*CDE^^/
is right. Shouldn't it be: AB*CD^E^/
Have you worked through the tree manually to see what it takes to generate the output your want?
-
Re: Help with this binary tree
Yes I have worked through it manually, I have managed to sort it now, thanks very much for your help