Java 二叉搜索树查找最大节点并将其删除(泛型类)
正如标题所述,我目前正在尝试查找BST的max节点,并希望将其删除。我有从我的算法书中查找max节点和删除node ready的方法,但我不知道如何在main方法中使用它们。我有一个方法,可以通过输入一个数字来插入节点,例如8,它将按级别顺序打印一棵树: 4, 2, 6, 1, 3, 5, 7 其中4是根。我希望能够找到最后一个节点并将其删除。到目前为止,我掌握了以下方法:Java 二叉搜索树查找最大节点并将其删除(泛型类),java,algorithm,generics,binary-search-tree,Java,Algorithm,Generics,Binary Search Tree,正如标题所述,我目前正在尝试查找BST的max节点,并希望将其删除。我有从我的算法书中查找max节点和删除node ready的方法,但我不知道如何在main方法中使用它们。我有一个方法,可以通过输入一个数字来插入节点,例如8,它将按级别顺序打印一棵树: 4, 2, 6, 1, 3, 5, 7 其中4是根。我希望能够找到最后一个节点并将其删除。到目前为止,我掌握了以下方法: public BinaryNode remove(AnyType x, BinaryNode<AnyType>
public BinaryNode remove(AnyType x, BinaryNode<AnyType> t)
{
if(t == null)
return t;
int compareResult = x.compareTo(t.element);
if(compareResult < 0 )
t.left = remove(x, t.left);
else if(compareResult > 0)
t.right = remove(x, t.right);
else if(t.left != null && t.right != null)
{
t.element = (AnyType) findMax(t.right).element;
t.right = remove(t.element, t.right);
}
else
t = (t.left != null) ? t.left : t.right;
return t;
}
public BinaryNode<AnyType> remove(AnyType x)
{
return root = remove(x, root);
}
public BinaryNode<AnyType> findMax(BinaryNode<AnyType> t)
{
if(t != null)
while(t.right != null)
t = t.right;
return t;
}
公共二进制节点删除(AnyType x,BinaryNode t)
{
如果(t==null)
返回t;
int compareResult=x.compareTo(t.element);
如果(比较结果<0)
t、 左=移除(x,t.左);
否则如果(比较结果>0)
t、 右=移除(x,t.右);
else if(t.left!=null和&t.right!=null)
{
t、 元素=(AnyType)findMax(t.right).element;
t、 右=移除(t元素,t右);
}
其他的
t=(t.left!=null)?t.left:t.right;
返回t;
}
公共二进制节点删除(任意类型x)
{
返回根=移除(x,根);
}
公共二进制节点findMax(二进制节点t)
{
如果(t!=null)
while(t.right!=null)
t=t右;
返回t;
}
我的主要方法如下所示:
public static void main(String[] args)
{
CompleteBinarySearchTree<Integer> bst = new CompleteBinarySearchTree<>();
BinaryNode<Integer> tree = bst.createBinarySearchTree(bst.insertNodes(8));
bst.printLevelOrderedBST(tree);
}
publicstaticvoidmain(字符串[]args)
{
CompleteBinarySearchTree bst=新的CompleteBinarySearchTree();
BinaryNode树=bst.createBinarySearchTree(bst.insertNodes(8));
bst.printLevelOrderedBST(树);
}
我希望能够自由插入任何节点,并且树仍然能够删除max节点。我不知道如何在findMax()上调用remove()方法。我当然可以调用remove(7,tree),它将删除7,但这不是我想要的。非常感谢您的帮助。删除max节点的关键是您必须跟踪它的父节点,这样您就可以更新父节点的
右
指针(将其设置为null
)。您还必须处理这样的情况:您传递的节点没有正确的子节点,其中节点本身是最大的节点
下面的代码显示了基本思想。未经测试,但应接近
// removes the largest node in the binary tree with root at t.
// returns the new root.
public BinaryNode<AnyType> removeMax(BinaryNode<AnyType> t)
{
if (t == null) return null;
if (t.right == null) {
// the node passed in is the largest.
return t.left;
}
// traverse the right branch to the last node,
// keeping track of the previous node so you can correctly set its
// right pointer to null.
BinaryNode<AnyType> parent = t;
BinaryNode<AnyType> child = parent.right;
while (child.right != null) {
parent = child;
child = child.right;
}
parent.right = null;
return t;
}
//删除二叉树中根位于t的最大节点。
//返回新的根目录。
公共二进制节点removeMax(二进制节点t)
{
如果(t==null)返回null;
如果(t.right==null){
//传入的节点是最大的。
返回t.left;
}
//将右分支遍历到最后一个节点,
//跟踪上一个节点,以便正确设置其
//指向null的右指针。
二进制节点父节点=t;
BinaryNode子节点=parent.right;
while(child.right!=null){
父母=子女;
child=child.right;
}
parent.right=null;
返回t;
}
如果(t.right==null){return t.left;},您能解释一下为什么这是正确的吗?它不应该返回t
,因为根据BST属性,t
的值将大于其左边的值吗?@kiner\u shah其目的是删除最大值节点。因此,在本例中,传入的节点是最大的。Returningt.left
删除该节点并返回新的根。@JimMischel,我想我现在明白了!