java.lang.StackOverflowError

Hi,

I'm trying to create a Binary search tree, for a dictionary which comes from an external file, that the user defines.

It works fine with a small dictionary file, but once the file has more then 4200 items or so, it causes an error, and gives me the message:

Exception in thread "main" java.lang.StackOverflowError

at java.lang.String.compareTo(Unknown Source)

It is coming from this portion of the code:

protected TreeNode insertItem(TreeNode tNode, KeyedItem newItem){

TreeNode newSubtree;

//position of insertion found

//insert after leaf

//create a new node

if(tNode ==null){

tNode =new TreeNode(newItem, null,null);

return tNode;

}//end if

KeyedItem nodeItem = (KeyedItem)tNode.getItem();

//search the left subtree

if (newItem.getKey().compareTo(nodeItem.getKey()) < 0){

newSubtree = insertItem(tNode.getLeft(), newItem);

tNode.setLeft(newSubtree);

return tNode;

}

//search the right subtree

else{

newSubtree = insertItem(tNode.getRight(), newItem);

tNode.setRight(newSubtree);

return tNode;

}//end if

}//end insertItem

and particularly with these lines:

[

if (newItem.getKey().compareTo(nodeItem.getKey()) < 0){

+

newSubtree = insertItem(tNode.getRight(), newItem);

does anybody know how to fix this problem please let me know?

[2314 byte] By [wane1234a] at [2007-11-26 18:44:30]
# 1
StackOverflowError is usually thrown when the recursion is too deep for your current memory capacity to handle it. Simple binary search on a sorted dictionary array would be better than using the recursive structure of the tree.
hiwaa at 2007-7-9 6:18:25 > top of Java-index,Java Essentials,New To Java...