网站单页在线制作软件,微信小程序开发哪家好,永嘉网站建设几,网站开发前后端一、细节思考和分类我们删除二叉树的节点时候#xff0c;要保证删除以后的数据继续保持有序状态#xff0c;那么就会分为三种情况a.删除叶子节点#xff1b;b.删除只有一个子节点的节点#xff1b;c.删除有两个子节点的节点。二、实现思路和代码实现1.删除叶子节点实现思路…一、细节思考和分类我们删除二叉树的节点时候要保证删除以后的数据继续保持有序状态那么就会分为三种情况a.删除叶子节点b.删除只有一个子节点的节点c.删除有两个子节点的节点。二、实现思路和代码实现1.删除叶子节点实现思路①找到要删除的节点targetNode②找到targetNode的父节点parentNode判断是否存在③确定当前targetNode是parentNode的左子树和右子树④根据以上情况进行删除左子节点parentNode.lChildnull;右子节点: parentNode.rChildnull;代码实现://确定targetNode是parentNode的左子树还是右子树 if(parentNode.lChild!nullparentNode.lChildtarget){ parentNode.lChildnull; } else if(parentNode.rChild!nullparentNode.rChildtarget){ parentNode.rChildnull; }判断是targetNode是parentNode的左子树还是右子树,是哪边的树就让哪边指空.2.删除有两个子节点的节点实现思路:①找到要删除的节点targetNode②找到targetNode的父节点parentNode判断是否存在③去找targetNode的右子树的最小值;④将targetNode的右子树最小值替换掉targetNode的值;⑤删除targetNode右子树的最小值.代码实现:if(targetNode!nulltargetNode.rChild!null){ int minfindRightTreeMin(targetNode.rChild); targetNode.datamin; }3.删除只有一个子节点的节点实现思路:①找到要删除的节点targetNode②找到targetNode的父节点parentNode判断是否存在③确定当前targetNode是parentNode的左子树和右子树④判断targetNode的子节点是其左子树还是右子树:分为四种情况:如果targetNode是parentNode左子树Ⅰ targetNode有左子节点parentNode.lChildtargetNode.lChild;Ⅱ targetNode有右子节点parentNode.lChildtargetNode.rChild;如果targetNode是parentNode右子树Ⅲ targetNode有左子节点parentNode.rChildtargetNode.lChild;Ⅳ targetNode有右子节点parentNode.rChildtargetNode.rChild;代码实现:if(parentNode.lChild!null parentNode.lChild.data target){ //确定targetNode是parentNode的左子树 if(targetNode.lChild ! null){ //targetNode有左子结点 parentNode.lChild targetNode.lChild; }else { //targetNode有右子结点 parentNode.lChild targetNode.rChild; } }else if(parentNode.rChild!null parentNode.rChild.data target){ //确定targetNode是parentNode的右子树 if(targetNode.lChild ! null){ //targetNode有左子结点 parentNode.rChild targetNode.lChild; }else { //targetNode有右子结点 parentNode.rChild targetNode.rChild; }4.需要注意的点此外我们要注意一个点万一要删除的点仅有一个节点我们就要if(root.lChild null root.rChild null){ root null; return; }5.需要添加的额外方法分别是①找到要删除的节点// 找到要删除的节点 TreeNode findTarget(TreeNode root,Integer target){ if(root null){ return null; } //去找这个值 if(root.data target){ return root; }else if(target root.data){ //判断是否有左子树 if(root.lChild null){ return null; } return findTarget(root.lChild,target); }else { //判断是否有右子树 if (root.rChild null) { return null; } return findTarget(root.rChild, target); } }②找要删除节点的父节点//去找要删除节点的父节点 TreeNode findParent(TreeNode root,Integer target){ if(root null){ return null; } if((root.lChild!null) (root.lChild.data target) || (root.rChild!null) (root.rChild.data target)){ return root; }else { if(root.lChild!null target root.data){ return findParent(root.lChild,target); }else if(root.rChild!null target root.data){ return findParent(root.rChild,target); }else { return null; } } }③找右子树的最小值public int findRightTreeMin(TreeNode node){ while (node.lChild !null){ node node.lChild; } int min node.data; delete(root, min); return min; }三、完整代码的运行和运行结果完整代码TreeNode findTarget(TreeNode root,Integer target){ if(root null){ return null; } //去找这个值 if(root.data target){ return root; }else if(target root.data){ //判断是否有左子树 if(root.lChild null){ return null; } return findTarget(root.lChild,target); }else { //判断是否有右子树 if (root.rChild null) { return null; } return findTarget(root.rChild, target); } } //去找要删除节点的父节点 TreeNode findParent(TreeNode root,Integer target){ if(root null){ return null; } if((root.lChild!null) (root.lChild.data target) || (root.rChild!null) (root.rChild.data target)){ return root; }else { if(root.lChild!null target root.data){ return findParent(root.lChild,target); }else if(root.rChild!null target root.data){ return findParent(root.rChild,target); }else { return null; } } } /** * 去找右子树的最小值 * param node * return */ public int findRightTreeMin(TreeNode node){ while (node.lChild !null){ node node.lChild; } int min node.data; delete(root, min); return min; } public void delete(TreeNode root,Integer target){ if(root null){ return; } //2.万一要删的节点只有一个节点 if(root.lChild null root.rChild null){ root null; return; } //1.去找被删除的节点 TreeNode targetNode findTarget(root,target); if(targetNode null){ //找不到 return; } //3.找到父节点 TreeNode parentNode findParent(root,target); //分情况进行删除 if(targetNode.lChild null targetNode.rChild null){ //叶子节点 //确定targetNode是parentNode的左子树还是右子树 if(parentNode.lChild ! null parentNode.lChild.data target){ parentNode.lChild null; }else if(parentNode.rChild ! null parentNode.rChild.data target){ parentNode.rChild null; } }else if(targetNode.lChild ! null targetNode.rChild ! null){ //有两个子节点的节点 int min findRightTreeMin(targetNode.rChild); targetNode.data min; }else { //targetNode 只有一个子节点的节点 //确定targetNode是parentNode的左子树还是右子树 if(parentNode.lChild!null parentNode.lChild.data target){ //确定targetNode是parentNode的左子树 if(targetNode.lChild ! null){ //targetNode有左子结点 parentNode.lChild targetNode.lChild; }else { //targetNode有右子结点 parentNode.lChild targetNode.rChild; } }else if(parentNode.rChild!null parentNode.rChild.data target){ //确定targetNode是parentNode的右子树 if(targetNode.lChild ! null){ //targetNode有左子结点 parentNode.rChild targetNode.lChild; }else { //targetNode有右子结点 parentNode.rChild targetNode.rChild; } } } }测试类构建相应的树然后指出删除节点运行结果