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 4 of 4

Thread: Traversing a tree

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Talking Traversing a Tree (Beginners)

    In my book, I'm given BinaryTree t and Node v as my parameter. So, I can traverse the tree by doing like:


    static boolean add(BinaryTree t, Node v )
     return add(t, t.left(v)) + add(t, t.right(v))




    static boolean method (BinaryTree t)

    I was wondering how do I traverse the tree? While comparing the values of it's sibling?
    I'm just given a "BinaryTree t" as a parameter. I'm not sure how to approach this.
    Thank you.
    Last edited by IAmHere; June 18th, 2011 at 10:42 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Traversing a tree

    I was wondering how do I traverse the tree?
    Depends what you wish to accomplish, as there are many ways to traverse a tree. Given we don't know the definition of the BinaryTree class, its hard to guide, however tree traversal typically involves starting at the root node, selecting a child node and recursively doing traversing its children until your goal is met.

  3. The Following User Says Thank You to copeg For This Useful Post:

    IAmHere (June 18th, 2011)

  4. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Traversing a tree

    I'm not sure how to write a recursive method with a tree as a parameter.
    I'm guessing you have to call its subtree every time.

  5. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Traversing a tree

    I figured it out.

    static boolean method (BinaryTree t)
    {
    ...
     return method2(t, t.root());
    }
     
     
    static boolean method2(BinaryTree t, Node v)
    {
    ...//So, now I have a binary and node parameter. I can traverse the tree it. lol
    }

Similar Threads

  1. Traversing a min heap to get sorted
    By derek in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 20th, 2010, 04:17 PM
  2. 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
  3. t:tree 2 an
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: January 27th, 2010, 12:56 PM
  4. B+ Tree
    By mikesir87 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 20th, 2009, 10:52 AM
  5. traversing multiple jTabbedPane?
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: October 2nd, 2009, 07:26 PM