Results 1 to 4 of 4
  1. #1
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

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

    Java 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default 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
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default 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();

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default Re: Help with binary trees please

    Quote Originally Posted by sim18 View Post
    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
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Algorithms to compare None-Binary Trees
    By memo.baghdady in forum New To Java
    Replies: 0
    Last Post: 07-24-2012, 01:56 PM
  2. Binary Search Trees
    By kraigballa in forum New To Java
    Replies: 2
    Last Post: 04-10-2012, 04:31 PM
  3. HELP!! Binary trees
    By Get_tanked in forum New To Java
    Replies: 4
    Last Post: 03-24-2011, 07:09 PM
  4. Binary trees
    By girgishf in forum Advanced Java
    Replies: 15
    Last Post: 11-20-2010, 05:29 PM
  5. Tutorial on Binary Search Trees
    By JordashTalon in forum New To Java
    Replies: 3
    Last Post: 03-18-2009, 04:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •