为什么打印不会停止而没有错误?
我尝试将布尔插入(k)的结果打印在for循环中,但是在第一次插入后,打印停止,这表明第二个插入并不完全成功。
内部方法插入(k),调用方法“检索(k)”,以检查是否已经插入k。
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("---------");
}
结果是
-------------------
true
dd
true,,,22
100 insertion done
---------
true
dd
在删除插入()方法中的“检索(k)”调用之后,打印良好,所以我假设方法“检索(k)”存在问题,并且由于没有错误 + CPU使用率更高,它可能是无限循环,问题是,我看不到。
这是方法“检索(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);
}
编辑:添加的插入方法
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
insert()
方法。当第二次调用时,root
是非编号,因此执行进入Else分支。在那里,它设置current.left = p
带有current == p
,因此p
现在是其自己的p.left
代码>。当retrieve()
方法到达该节点时,它设置了p = p.left
什么都没有改变,从而导致无限循环。您使用
当前
节点的方法不起作用。在insert()
中,您每次都必须从根部搜索新节点的插入位置,类似于您在retive()
中所做的工作。只要继续下去直到到达叶子。然后将新节点插入其中。代码看起来像这样:
The issue is with the
insert()
method. When it is called for the second time,root
is non-null, so execution gets into the else branch. There, it setscurrent.left = p
withcurrent == p
, sop
is now its ownp.left
. When theretrieve()
method arrives at that node, it setsp = p.left
which changes nothing, causing the infinite loop.Your approach using a
current
node does not work. Ininsert()
, you have to search the insertion position of the new node from the root every time, similar to what you do inretrieve()
. Just keep going down until you reach a leaf. Then insert the new node there.The code could look like this: