# Traversing a tree

• June 18th, 2011, 06:07 PM
IAmHere
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:

Code :

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

Code :

`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.
• June 18th, 2011, 07:12 PM
copeg
Re: Traversing a tree
Quote:

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.
• June 18th, 2011, 09:39 PM
IAmHere
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.
• June 19th, 2011, 09:47 AM
IAmHere
Re: Traversing a tree
I figured it out.

Code :

```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 }```