Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: Help with this binary tree

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this binary tree

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this binary tree

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this binary tree

    Thank you
    It works now, next time I will be sure to read the error message properly

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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:

    BinaryTreeNode n1 = new BinaryTreeNode("A");

    I have done nothing different from what I can tell,

    Thanks for any help

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this binary tree

    I don't understand what you mean sorry?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this binary tree

    This will compile and run the test

    public class Testing
    {
     
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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:
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this binary tree

    Sorry, code should work now

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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:

     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

    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

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this binary tree

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this binary tree

    1. Traverse left sub tree
    2. Traverse right sub tree
    3. Visit root

    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?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this binary tree

    What is the ^ operator?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this binary tree

    it is just 'to the power of'

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this binary tree

    then (C^D^E)
    would be: (C^(D^E))
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this binary tree

    Does that mean I will have to include the brackets in me code?

    Example:

    BinaryTreeNode n1 = new BinaryTreeNode("A");

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    sim18 (November 29th, 2012)

  25. #24
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Binary Tree Traversal help please
    By sim18 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2012, 08:01 AM
  2. need help with binary tree
    By vash0047 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 08:23 AM
  3. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  4. Problem with binary tree
    By Exoskeletor in forum Object Oriented Programming
    Replies: 2
    Last Post: January 8th, 2010, 01:03 PM
  5. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM