本文共 6322 字,大约阅读时间需要 21 分钟。
class BinarySortTree
public void add(Node node) { if (root == null) { root = node; } else { root.add(node); } }
class Node
public void add(Node node){ if (node == null){ return; } if(node.value < this.value){ if (this.left == null){ this.left = node; }else{ this.left.add(node); } }else{ if (this.right == null){ this.right = node; }else{ this.right.add(node); } } }
总结与分析:
删除叶子节点
删除有一棵子树的结点
删除的目标节点TargetNode右两个子树
public Node search(int value){ if (this == null){ return null; } if (value == this.value){ return this; }else if(value < this.value){ //如果查找的值小于当前节点的值,那就向左进行递归 //还有一个问题:不知道但前指针存不存在,有可能为零 if (this.left == null){ return null; } return this.left.search(value); }else{ //如果当前的值大于当前节点的值,那就向右进行递归 //问题:不知道右子节点存不存在 if (this.right == null){ return null; } return this.right.search(value); } } //查找待删除结点的父节点
查找待删除结点的父节点
/** * * @param value 需要查找的节点值 * @return 返回的是要删除的节点的父节点,如果没有就返回null */public Node searchParent(int value){ //如果当前结点就是需要返回的父节点,说明子节点就是需要查找的结点 if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)){ return this; }else if(this.left != null && value < this.value){ return this.left.searchParent(value); //如果返回的不是当前的结点,那就进行说明返回的是左子结点,或者是右子节点, //故而应当是左子节点调用,或者是右子节点调用 }else if(this.right != null && value >= this.value){ return this.right.searchParent(value); }else { return null; }}
删除target Node的左子树的最大节点,或者是右子树的最小节点
/** * 找到当前子树的最小值,同时还要删除该结点 * @param node node传入的结点(为左二叉排序树的根节点) * @return 返回以Node为根节点的二叉排序树的最小结点Z * 个人认为:返回最小节点,然后在删除 */ public Node delRightTreeMin(Node node){ Node target = node; while (target.left != null){ target = target.left; } Node temp = target; //退出循环的target指向最小节点 delNode(target.value); return target; //找到了最小的值,那就返回该节点 }
class BestSearchTree
//查找要删除节点 public Node search(int value){ if (root == null){ return null; }else{ return root.search(value); } } //查找要删除节点的父节点 public Node searchParent(int value){ if(root == null){ return null; }else{ return root.searchParent(value); } }
第二部分 根据情况进行分类删除
public void delNode(int value){ if (root == null){ return ; }else{ //1. 需求先去找到待删除的节点 targetNode Node targetNode = search(value); //2. 如果没有找到要删除的结点 if (targetNode == null){ return; } //情况一,没有父节点,根节点就是要找的点 if ((root.left == null && root.right == null) && root == targetNode){ root = null; return; } if (parent.left != null && parent.left == targetNode){ parent.left = null; }else if(parent.right != null && parent.right == targetNode){ parent.right = null; } }else if(targetNode.left != null && targetNode.right != null){ Node minNode = delRightTreeMin(targetNode.right); if (parent.left != null && parent.left == targetNode){ parent.left = minNode; }else if(parent.right != null && parent.right == targetNode){ parent.right = minNode; } //在父节点的层面上进行删除, minNode.left = targetNode.left; minNode.right = targetNode.right; //在子节点层面上的操作 }else{ //情况四:删除只有一棵子树的特点 if (targetNode.right != null){ //仅有左子树,右子树为空 if ( parent.left == targetNode){ parent.left = targetNode.right; } if ( parent.right == targetNode){ parent.right = targetNode.right; } }else{ if ( parent.left == targetNode){ parent.left = targetNode.left; } if (parent.right == targetNode){ parent.right = targetNode.left; } } } } }
转载地址:http://hqgpb.baihongyu.com/