请各位帮我看看我这出什么问题了。为什么死循环。。

尼古拉斯Johnson 2014-01-16 11:56:48
public void insert(Person p) {
TreeNode newNode = new TreeNode(p);
if(root == null) {
root = newNode;
} else {
TreeNode current = root;
TreeNode node = null;
while(current != null) {
node = current;
if(current.getData().getiData() > p.getiData()) {
current = root.getLeftChild();
} else {
current = root.getRightChild();
}
}
if(node.getData().getiData() > p.getiData()) {
node.setLeftChild(newNode);
} else {
node.setRightChild(newNode);
}
}
}
...全文
120 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

  if(root == null) {

   } else {
     TreeNode current = root;
     while(current != null) {

     }
  }
  • 打赏
  • 举报
回复
引用 4 楼 MrsFeng 的回复:
current != null,难道不是一直成立吗? 难道你又把current设置为null的地方?
啊 我明白啦。。脑袋短路了昨天想半宿都没想出来。。。。 current = root.getLeftChild(); 这句话应该是current.getLeftChild() 谢谢啦!!!
tony4geek 2014-01-16
  • 打赏
  • 举报
回复
deubg 看下。2叉树,递归出现问题了。
大大点点 2014-01-16
  • 打赏
  • 举报
回复
给楼主个建议,打断点调试,很快就能找到哪里无线循环了!
  • 打赏
  • 举报
回复
引用 4 楼 MrsFeng 的回复:
current != null,难道不是一直成立吗? 难道你又把current设置为null的地方?
就是这样。。。。。
你好毒你好毒 2014-01-16
  • 打赏
  • 举报
回复
current != null,难道不是一直成立吗? 难道你又把current设置为null的地方?
  • 打赏
  • 举报
回复
引用 楼主 heanqing7 的回复:
public void insert(Person p) { TreeNode newNode = new TreeNode(p); if(root == null) { root = newNode; } else { TreeNode current = root; TreeNode node = null; while(current != null) { node = current; if(current.getData().getiData() > p.getiData()) { current = root.getLeftChild(); } else { current = root.getRightChild(); } } if(node.getData().getiData() > p.getiData()) { node.setLeftChild(newNode); } else { node.setRightChild(newNode); } } }
加根和根的左节点根的右节点好使。加子节点的子节点就无限循环了。。
  • 打赏
  • 举报
回复
引用 1 楼 wxyong3 的回复:

  if(root == null) {

   } else {
     TreeNode current = root;
     while(current != null) {

     }
  }
这个。。。没看出来有问题啊。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧