二叉搜索树实现 - 搜索函数找不到节点 (Java)
Binary Search Tree Implementation - Search Function can't find Node (Java)
我正在 Java
中实现 二叉搜索树 。但是,我的 search
函数在删除函数中似乎没有正常工作。它总是在树中找不到 Nodes
,无论它们是否真的在树中。我认为我的逻辑是正确的, 比较节点 左右移动取决于要搜索的 Node
是更大还是更小,但我可能会遇到一些问题return
值。如果添加节点 class 或程序测试有帮助,我可以这样做。有什么建议吗?
public class BinarySearchTree {
private boolean empty = true;
private int size = 0;
private Node root;
public BinarySearchTree(Node root)
{
this.root = root;
}
public Node[] search(int value)
{
Node parent = null;
Node currentNode = root;
Node[] returnList = new Node[2];
while ( currentNode != null )
{
if (currentNode.getValue() == value)
{
returnList[0] = parent;
returnList[1] = currentNode;
return returnList;
}
else if (value < currentNode.getValue())
{
parent = currentNode;
currentNode = currentNode.getLeft();
}
else if (value > currentNode.getValue())
{
parent = currentNode;
currentNode = currentNode.getRight();
}
}
return returnList;
}
public void add(int value)
{
Node comparingNode = root;
while (true)
{
if (comparingNode.getValue() == value)
{
System.out.println("Tried to add duplicate value of " + value);
break;
}
if (comparingNode.getLeft() == null && comparingNode.getRight() == null)
{
if (value > comparingNode.getValue())
{
comparingNode.setRight(new Node(value));
}
if (value < comparingNode.getValue())
{
comparingNode.setLeft(new Node(value));
}
break;
}
else
{
if (value < comparingNode.getValue())
{
if (comparingNode.getLeft() == null)
{
comparingNode.setLeft(new Node(value));
break;
}
else
{
comparingNode = comparingNode.getLeft();
}
}
if (value > comparingNode.getValue())
{
if (comparingNode.getRight() == null)
{
comparingNode.setRight(new Node(value));
break;
}
else
{
comparingNode = comparingNode.getRight();
}
}
}
}
}
public void remove(int value)
{
Node[] nodesFound = search( value );
Node parent = nodesFound[0];
Node child = nodesFound[1];
boolean fLeft = ( parent.getLeft() == child );
// child node has no children.
if (fLeft)
{
parent.setLeft(null);
}
else
{
parent.setRight(null);
}
if( child.getLeft() != null && child.getRight() == null )
{
// child node has only left child.
if( fLeft )
{
parent.setLeft(child.getLeft());
}
else
{
parent.setRight(child.getLeft());
}
}
else if ( child.getRight() != null && child.getLeft() == null )
{
// child node has only right child.
if( fLeft )
{
parent.setLeft(child.getRight());
}
else
{
parent.setRight(child.getRight());
}
}
else
{
// child node has both children.
if( child.getRight().getLeft() == null )
{
child.getRight().setLeft( child.getLeft() );
parent.setRight( child.getRight() );
}
else
{
Node[] returnList = findLeftMost2Children(child.getRight());
Node leftMostParent = returnList[0];
Node leftMostChild = returnList[1];
leftMostParent.setLeft(null);
leftMostChild.setLeft(child.getLeft());
leftMostChild.setRight(child.getRight());
parent.setRight(leftMostChild);
}
}
}
public Node getRoot()
{
return root;
}
public void outputTreeInOrder( Node root )
{
if( root == null )
return;
// Output the left tree.
if( root.getLeft() != null )
outputTreeInOrder( root.getLeft() );
// Output the current node.
System.out.print( root.getValue() + " " );
// Output the right tree.
if( root.getRight() != null )
outputTreeInOrder( root.getRight() );
}
private Node[] findLeftMost2Children( Node root )
{
Node parent = null;
Node current = root;
while (current.getLeft() != null)
{
parent = current;
current = current.getLeft();
}
Node[] returnList = new Node[2];
returnList[0] = parent;
returnList[1] = current;
return returnList;
}
}
我根据问题中的代码进行了更正。我意识到我必须设置正确的指针来在删除时移动节点,而不仅仅是更改值。您必须实际移动 node
和新的 pointers
及其 parent
.
我正在 Java
中实现 二叉搜索树 。但是,我的 search
函数在删除函数中似乎没有正常工作。它总是在树中找不到 Nodes
,无论它们是否真的在树中。我认为我的逻辑是正确的, 比较节点 左右移动取决于要搜索的 Node
是更大还是更小,但我可能会遇到一些问题return
值。如果添加节点 class 或程序测试有帮助,我可以这样做。有什么建议吗?
public class BinarySearchTree {
private boolean empty = true;
private int size = 0;
private Node root;
public BinarySearchTree(Node root)
{
this.root = root;
}
public Node[] search(int value)
{
Node parent = null;
Node currentNode = root;
Node[] returnList = new Node[2];
while ( currentNode != null )
{
if (currentNode.getValue() == value)
{
returnList[0] = parent;
returnList[1] = currentNode;
return returnList;
}
else if (value < currentNode.getValue())
{
parent = currentNode;
currentNode = currentNode.getLeft();
}
else if (value > currentNode.getValue())
{
parent = currentNode;
currentNode = currentNode.getRight();
}
}
return returnList;
}
public void add(int value)
{
Node comparingNode = root;
while (true)
{
if (comparingNode.getValue() == value)
{
System.out.println("Tried to add duplicate value of " + value);
break;
}
if (comparingNode.getLeft() == null && comparingNode.getRight() == null)
{
if (value > comparingNode.getValue())
{
comparingNode.setRight(new Node(value));
}
if (value < comparingNode.getValue())
{
comparingNode.setLeft(new Node(value));
}
break;
}
else
{
if (value < comparingNode.getValue())
{
if (comparingNode.getLeft() == null)
{
comparingNode.setLeft(new Node(value));
break;
}
else
{
comparingNode = comparingNode.getLeft();
}
}
if (value > comparingNode.getValue())
{
if (comparingNode.getRight() == null)
{
comparingNode.setRight(new Node(value));
break;
}
else
{
comparingNode = comparingNode.getRight();
}
}
}
}
}
public void remove(int value)
{
Node[] nodesFound = search( value );
Node parent = nodesFound[0];
Node child = nodesFound[1];
boolean fLeft = ( parent.getLeft() == child );
// child node has no children.
if (fLeft)
{
parent.setLeft(null);
}
else
{
parent.setRight(null);
}
if( child.getLeft() != null && child.getRight() == null )
{
// child node has only left child.
if( fLeft )
{
parent.setLeft(child.getLeft());
}
else
{
parent.setRight(child.getLeft());
}
}
else if ( child.getRight() != null && child.getLeft() == null )
{
// child node has only right child.
if( fLeft )
{
parent.setLeft(child.getRight());
}
else
{
parent.setRight(child.getRight());
}
}
else
{
// child node has both children.
if( child.getRight().getLeft() == null )
{
child.getRight().setLeft( child.getLeft() );
parent.setRight( child.getRight() );
}
else
{
Node[] returnList = findLeftMost2Children(child.getRight());
Node leftMostParent = returnList[0];
Node leftMostChild = returnList[1];
leftMostParent.setLeft(null);
leftMostChild.setLeft(child.getLeft());
leftMostChild.setRight(child.getRight());
parent.setRight(leftMostChild);
}
}
}
public Node getRoot()
{
return root;
}
public void outputTreeInOrder( Node root )
{
if( root == null )
return;
// Output the left tree.
if( root.getLeft() != null )
outputTreeInOrder( root.getLeft() );
// Output the current node.
System.out.print( root.getValue() + " " );
// Output the right tree.
if( root.getRight() != null )
outputTreeInOrder( root.getRight() );
}
private Node[] findLeftMost2Children( Node root )
{
Node parent = null;
Node current = root;
while (current.getLeft() != null)
{
parent = current;
current = current.getLeft();
}
Node[] returnList = new Node[2];
returnList[0] = parent;
returnList[1] = current;
return returnList;
}
}
我根据问题中的代码进行了更正。我意识到我必须设置正确的指针来在删除时移动节点,而不仅仅是更改值。您必须实际移动 node
和新的 pointers
及其 parent
.