I try to print the result of boolean insert(K) in a for loop but after the first insertion the printing stops, that indicates the second insertion is not fully successful.
and inside method insert(K), the method "retrieves(K)" is called, to check if K has been already inserted.
for (int i = 100; i > 0; i--) {
System.out.println(m.insert(i +1, 22));
System.out.println("dd");
System.out.println(m.retrieve(i+1).first + ",,,"+m.retrieve(i+1).second);
System.out.println(i + " insertion done");
System.out.println("---------");
}
and the result is
-------------------
true
dd
true,,,22
100 insertion done
---------
true
dd
After removing the "retrieves(K)" call in the insert() method, the print runs just fine, so i am assuming there is an issues with the method "retrieves(K)", and since there is no error + cpu usage is higher, it might be an infinite loop, the problem is, i don't see it.
here is the method "retrieves(K)"
public Pair<Boolean, T> retrieve(K k) {
Pair<Boolean, T> ff = new Pair<Boolean, T>(false, null);
BSTMapNode<K, T> p = root;
if(root==null) {
return new Pair<Boolean,T>(false,null);
}
else
while (p != null) {
if (k.compareTo(p.key) == 0) {
ff.first=true;
ff.second=p.data;
return new Pair<Boolean,T>(true,p.data);
} else if (k.compareTo(p.key) < 0) {
p = p.left;
} else
p = p.right;
}
return new Pair<Boolean,T>(false,null);
}
EDIT: added insert method
public boolean insert(K k, T e) {
BSTMapNode<K, T> p = current;
BSTMapNode<K, T> q = current;
// ISSUE HERE
if (retrieve(k).first == true) {
current = q;
return false;
//
}
BSTMapNode<K, T> tmp = new BSTMapNode<K, T>(k, e);
if (root == null) {
root = current = tmp;
return true;
} else {
if (k.compareTo(current.key) < 0)
current.left = p;
else
current.right = p;
current = p;
return true;
}