【发布时间】:2022-11-11 05:24:56
【问题描述】:
我正在尝试构建二叉搜索树,但在创建自己的删除节点的方法时遇到问题。
我知道当一个节点有两个孩子(一个孩子或没有孩子)时如何删除它,我在我的代码中已经考虑到了这一点。但是当我真的想删除它并将其设置为 null 时,什么也没有发生。节点仍然存在,并且当节点有两个孩子时,最小的数字不会替换它。 (一种处理删除具有两个孩子的节点的技术)。问题出在哪里?
下面是代码:
public static void main(String[] args) {
BST root=new BST(10);
root.insert(5);
root.insert(15);
root.insert(2);
root.insert(5);
BST node=root.insert(13);
root.insert(22);
root.insert(1);
root.insert(14);
BST smallestNode=root.insert(12);
System.out.println(node.left.value);
System.out.println(root.contains(4));
System.out.println(root.remove(12));
System.out.println(root.contains(12));
}
public static class BST {
public int value;
public BST left;
public BST right;
public BST(int root) {
this.value = root;
}
public BST insert(int value) {
BST currentNode=this;
while(true){
if(value>= currentNode.value){
if(currentNode.right ==null){
currentNode.right=new BST(value);
currentNode=currentNode.right;
break;
}
currentNode= currentNode.right;
}else{
if(currentNode.left==null){
currentNode.left=new BST(value);
currentNode=currentNode.left;
break;
}
currentNode= currentNode.left;
}
}
return currentNode;
}
public boolean contains(int value) {
BST currentNode=this;
while(true){
if(value>= currentNode.value){
if(currentNode.right ==null){
return false;
}
currentNode= currentNode.right;
if(currentNode.value==value){
return true;
}
}else{
if(currentNode.left==null){
return false;
}
currentNode= currentNode.left;
if(currentNode.value==value){
return true;
}
}
}
}
public BST remove(int value) {
BST currentNode=this;
if(currentNode.right==null &¤tNode.left==null){
return this;
}
while(true){
if(value>= currentNode.value){
if(currentNode.right==null){
return this;
}
currentNode= currentNode.right;
if(currentNode.value==value){
if(currentNode.left!=null&¤tNode.right==null){
currentNode=currentNode.left;
return currentNode;
}else if(currentNode.right!=null&¤tNode.left==null){
currentNode=currentNode.right;
return currentNode;
}else if(currentNode.right==null&¤tNode.left==null){
currentNode=null;
return currentNode;
}else{
currentNode.value=smallestValueSearchService(this);
currentNode.right=this.right;
currentNode.left=this.left;
return currentNode;
}
}
}else{
if(currentNode.left==null){
return this;
}
currentNode= currentNode.left;
if(currentNode.value==value){
if(currentNode.left!=null&¤tNode.right==null){
currentNode=currentNode.left;
return currentNode;
}else if(currentNode.right!=null&¤tNode.left==null){
currentNode=currentNode.right;
return currentNode;
}else if(currentNode.right==null&¤tNode.left==null){
currentNode=null;
return currentNode;
}else{
currentNode.value=smallestValueSearchService(this);
currentNode.right=this.right;
currentNode.left=this.left;
return currentNode;
}
}
}
}
}
}
//?: conditional
public static int smallestValueSearchService(BST binaryTree){
if(binaryTree.left==null){
int temp=binaryTree.value;
binaryTree=null;
return temp;
}else{
return smallestValueSearchService(binaryTree.left);
}
}
- 这是我应该实现的目标以及我的代码产生的结果的直观说明:
- 这是二叉树:
【问题讨论】:
-
请提供一个可运行的示例来说明您的问题。阅读How to Ask。
标签: java binary-tree