本文共 1418 字,大约阅读时间需要 4 分钟。
BinaryTree类: public void delete(int no) { if (isEmpty()) { System.out.println("树为空"); return; } //判定是否仅仅只有根节点,是由根节点就退出程序 if (root.getHeroNo() == no) { root = null; return; } this.root.delete(no); }
HeroNode类:
public void delete(int no) { //判断左右子节点是否为空 if (this.left != null && this.left.getHeroNo() == no) { //判定当前结点的左子节点是否为待删除结点,是的话删除推出 this.left = null; return; } if (this.right != null && this.right.getHeroNo() == no) { this.right = null; return; } //左右子节点都不是那就分别进行左右递归循环 if (this.left != null) { this.left.delete(no); //这里不能加return,因为可能没有找到 } if (this.right != null) { this.right.delete(no); } return; //达到最后一层,全是叶子结点,已经在上一轮遍历过了,就意味着完全退出 }
转载地址:http://iqgpb.baihongyu.com/