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

# Thread: Traversing a tree

1. ## 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 )

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.

2. ## 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. ## 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. ## 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
}