The tree that I'm using, has infinite children
I need some samples with that !!
Thanks again !!
----- Andreas HÃ¥kansson wrote: ----
Hello
Since you do not provide any information on what kind of tre
you are working with, I will provide some information using
basic binarytree where each node can have two childen, the lef
and right child. The node could be designed like this (note tha
I make the childen public, of course you should use propertie
for this). The node includes an int variable to represent a valu
in the node, you would replace this variable to suite the kind o
information you need to store
public class BinaryNod
public BinaryNode left
public BinaryNode right
public int value
Basicly what you need to do to stop the recusion is to define
condition where the recursive method does a return instead o
continuting to call itself again
I also do not know if the travers order is of intresst to you, so
will breifly describe the three methods of traversing a binarytre
structure
(1) Pre-order : Check the value of the current node and the
continue on to the left child and then the right child
(2) In-order : Check the value of the left child, then the curren
child, and last the right child
(3) Post-order: Check the left child then the right child, and las
check the current child
So what we will do is make a pseudo-code definition of th
BinaryTree class to illustrat what we will be doing. We have a publi
method Find which will look for the node with a given value, and the
return it. This method will be the recusion-starter and we will use
private method to do the actuall recusion
public class BinaryTre
private BinaryNode root
public BinaryTree(
public BinaryNode Find(int v
if( this.root != null
this.Find(v, root
private BinaryNode Find(int v, BinaryNode n
// Implement the recursive search using a give
// travers method
Now we need to implement the private recusive method using th
traversation order we desire. I will show you how it would look, usin
all of the three orders. The condition we will use to stop the recusion i
when we find a node (any node) with a given value
(1) Pre-Order Traversio
private BinaryNode Find(int v, BinaryNode n
if( n.value == value
return this
if( n.left != null
this.Find(v, n.left)
if( n.right != null
this.Find(v, n.right)
(2) In-Order Traversio
private BinaryNode Find(int v, BinaryNode n
if( n.left != null
this.Find(v, n.left)
if( n.value == value
return this
if( n.right != null
this.Find(v, n.right)
(3) Post-Order Traversio
private BinaryNode Find(int v, BinaryNode n
if( n.left != null
this.Find(v, n.left)
if( n.right != null
this.Find(v, n.right)
if( n.value == value
return this
Please note that in this simple example, the order of traversio
has litle effect, unless you perhaps always see to it that you hav
a balanced, sorted, BinaryTree. I illustrated all three travers order
since you provided spars information on what you stor, in what kin
of tree etc. But the basic idea of defining one or many conditions t
stop AND return from the recusive method is the same even if you
use a non-binarytree structure.
Hope this helps,
//Andreas HÃ¥kansson
MadWildShark said:
Hello friends.
I have a tree structure that I want to cross, looking for a specific node.
I have tried to develope a recursive function, but when it has to leave,
and return the result, it does not stop the execution, and it follows until
the end of the tree.