Question:
JAVA: Binary Search Tree help!! 10 pts best answer !!!?
anonymous
2012-04-18 16:17:10 UTC
//class1:
public class BSTNode1 {
public Object data;
public BSTNode1 left, right;
public Object key;


public BSTNode1 (Object d, BSTNode1 l, BSTNode1 r, Object key)
{
data=d;
left=l;
right=r;
this.key=key;

}

public BSTNode1(Object d){

data=d;
left = null;
right =null;
}

//left right set null


}





//class2:


import java.util.Comparator;





public class BST3{
private BSTNode3 root;

private Comparator comp;//how to compare nodes
public BST3 (BSTNode3 ro, Comparator co){
root=ro;
comp=co;

}
public BSTNode3 insert(BSTNode3 node, Object newData){
node=root;
//if(node ==null){
//node=new BSTNode1(newData);
//return node;

while(node !=null){
//if(newDataif(comp.compare(node.data, newData)<0){
if(node.left==null){
node.left=new BSTNode3(newData);


return node.left;
//System.out.println(newData +"inserted left of: " root.data);
}

else insert(node.left, newData);


}
//else{
else{
if (node.right==null){

node.right=new BSTNode3(newData);
//System.out.println(newData + "inserted right of: "root.data);
return node.right;
}
else insert(node.right, newData);



}
}
//return node=new BSTNode3(newData);
return node;

}

/*
public void insert2(BSTNode1 node,int n){
if(node != null){
if(n < node.data){
if(node.left == null){
node.left = new BSTNode1(n);
System.out.println(n+" inserted left of "+node.data);
return;
}
else insert2(node.left,n);
}
else{
if(node.right == null){
node.right = new BSTNode1(n);
System.out.println(n+" inserted right of "+node.data);
return;
}
else insert2(node.right,n);
}
}
}
*/
public void setComparator(Comparator c)
{
if(root==null)
comp=c;
}
//only allow if tree empty

public void clear()
{
root=null;
}//empty tree


public static void printInOrder(BSTNode3 node){
if(node != null){

printInOrder(node.left);
System.out.println(node.data);
printInOrder(node.right);
}
}

public static void printPreOrder(BSTNode3 node){
if(node != null){
System.out.println(node.data);
printPreOrder(node.left);

printPreOrder(node.right);
}
}
//static boolean found=false;
public BSTNode3 find(BSTNode3 ro, Object key){
root=ro;
while(ro!=null){
if(comp.compare(ro.key, key)==0)
//found=true;
return ro;

//else
if(comp.compare(key, ro.key) >0)
return find(ro.left, key);
if(comp.compare(key, ro.key)>0)
return find(ro.right, key);
}
return root;
}
//return false;

/*
static boolean found = false;

public static boolean find(BSTNode1 node, int n){
if(node != null){
//System.out.println(n+" n node.data "+node.data);
if(n == node.data){
found = true;
return true;
}
else if(n < node.data) find(node.left,n);
else if( n > node.data) find(node.right,n);
}
return false;
}
*/
public static void main(String [] args){

BST3 tree = new BST3(new BSTNode3("HIiiiiiii"), new StringComparator3());

/*tree.insert(tree.root, "hello");
tree.insert(tree.root, "hi");
tree.insert(tree.root, "yo");
tree.insert(tree.root, "where");
tree.insert(tree.root, "yoes");*/

//tree.insert(tree.root, "hello");

tree.insert(tree.root, "hil");


tree.insert(tree.root, "hi");
/*
tree.insert(tree.root, "where");
tree.insert(tree.root, "yoes");
*/

System.out.println("-------------…");
BST3.printInOrder(tree.root);
//System.out.println("-------------…
//tree.printPreOrder(tree.root);



//tree.find(tree.root, "hello");
//System.out.println(found);
//found = false;
//tree.find(tree.root, "yos");
//System.out.println(found);*/


//tree.find(tree.root, "hello");
//tree.find(tree.root, "yos");


}
}



class3:

import java.util.Comparator;

public class StringComparator3 implements Comparator{
public int compare(Object s1, Object s2){

return ((String)s1).compareTo((String)s2);
}
}



I keep getting the following message:

Exception in thread "main" java.lang.StackOverflowError
at aPackage.StringComparator3.compare(Strin…
at aPackage.BST3.insert(BST3.java:26)
at aPackage.BST3.insert(BST3.java:35)
at aPackage.BST3.insert(BST3.java:35)
at aPackage.BST3.insert(BST3.java:35)
at aPackage.BST3.insert(BST3.java:35)


Can anyone help me or give me some hints please?
Three answers:
Ratchetr
2012-04-18 16:33:28 UTC
Like Walter said, the stack trace tells you what is going wrong...you are using recursion but you have the termination condition wrong, you just turtle all the way down (until you run out of stack space).



This jumped out at me:



The insert function takes a BSTNode3 node parameter.



But what is the *very first* thing insert does? This:

node=root;



So, you pass insert a node, but you immediately ignore that value and replace it with root. That sounds like a good recipe for infinite recursion to me. I don't know if just commenting out that 1 line will fix everything, but it's a start. Whatever you do...DON'T PUT IT BACK IN if something else goes wrong.
James Bond
2012-04-19 00:48:29 UTC
Why dont you mark the lines where you are getting error.

I am sure I have supplied a version of working code recently which successfully inserted nodes into BST.
anonymous
2012-04-18 23:23:19 UTC
You must not be checking it properly. Your error shows that you are going in recursion too deep which shouldn't be happening. I may look at it if you paste your code here: http://pastebin.com/ I do not want to read this code in YA because the formatting is not right. Make sure you select Java as the language.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...