Stackoverflow error in recursive method (URGENT...
Hi all,
My code has the problem of stackoverflow when executing a recursive loop
my loop is as follows
initially curr is null and node is getting values like 5,7,4,3,2,6.
i must store the values returned from recursion procedure in a tree named as next.
after inserting the nodes the recursion method must return the starting root .
initially root =null;
Call by:
---------------
BinaryTree next=new BinaryTree();
next.root=next.Recursive(n,next.root);
Function:
-----------
private BinaryNode Recursive(BinaryNode node,BinaryNode curr)
{
if(curr == null )
{
curr = node;
}
else
{
if ((node.key).compareTo(curr.key )<0)
{
if(curr.leftChild==null)
{
curr.leftChild=node;
return curr;
}
else
curr.leftChild=Recursive(node, curr.leftChild);
}
else if ((node.key).compareTo(curr.key )>0)
{
if(curr.rightChild==null)
{
curr.rightChild=node;
return curr;
}
else
curr.rightChild=Recursive(node, curr.rightChild);
}
}
return curr;
}
I thought problem was solved by increasing the internal stack size.but it still exists.....
please suggest some way for this recursion problem...
thanks in advance...
|