首先,这是家庭作业,所以把它放在外面。
我应该用特定的方法实现二叉查找树:
void insert(字符串)、boolean remove(字符串)和boolean find(字符串)。
我已经能够成功地编程和测试插入,并找到方法,但我有困难与删除。
我的程序中发生的事情是,删除实际上并没有从树中删除任何东西,我相信这是因为它只引用当前节点的本地创建,但我可能错了。我认为我可以实现我需要测试的不同案例的逻辑(可能需要帮助删除具有两个子节点的案例,但我认为我在概念上得到了它)主要是试图理解我需要做什么不同的事情来正确引用树
current = null; // case
以下是我到目前为止得到的信息:
public boolean remove(String title)
{
return remove(root, title);
}
private boolean remove(BSTNode current, String title)
{
if (current == null)
{
return false;
}
if (current.data == title)
{
if (current.left_child !=null && current.right_child != null)
{
return true; // returning true since I haven't finished writing this case
}
else if (current.left_child == null && current.right_child == null)
{
current = null; // this should remove the node from tree but it doesn't
return true;
}
else if (current.left_child != null && current.right_child == null)
{
current = current.left_child; // don't think this is right
return true;
}
else if (current.right_child != null && current.left_child == null)
{
current = current.right_child; // not sure about this
return true;
}
}
root = current;
if (title.compareToIgnoreCase(current.data) == -1)
{
return remove(current.left_child, title);
}
else
{
return remove(current.right_child, title);
}
}
任何知识都是非常感激的。
节点由其父节点引用(根节点除外,该节点由您的BST本身引用)。为了从树中删除节点,您需要将父节点中的引用设置为null
。
你现在要做的是:
Before:
parent.left ---> node <--- current
After setting current = null:
parent.left ---> node current ---> null
也就是说,当前引用null
,但这不会更改父变量(仅更改该局部变量)。
在删除方法中,您还需要传递父级(或者在父级调用中处理两个子级,无论您更喜欢什么)。
顺便说一下:永远不要将字符串与==
进行比较,例如,请参见这个问题。
如何在不将父节点显式存储在每个节点中的情况下查找节点及其父节点:
我会说在循环中执行此操作比使用递归更简单,但两者都可以工作。在循环中:
BSTNode parent = null;
BSTNode current = root;
boolean found = false;
while (!found && current != null) {
int compare = title.compareToIgnoreCase(current.data);
if (compare == 0) {
found = true;
} else {
parent = current;
if (compare < 0) {
current = current.left;
} else {
current = current.right;
}
}
}
if (!found) {
// title was not found
} else if (parent == null) {
// found the root
} else {
// found another node
}
By递归很烦人,因为您需要一个同时返回节点及其父节点的方法。您需要一些简单的内部类来解决这个问题:
private class NodeAndParent {
public BSTNode node;
public BSTNode parent;
public NodeAndParent(BSTNode node, BSTNode parent) {
this.node = node;
this.parent = parent;
}
}
private boolean find(String title, NodeAndParent current) {
if (current.node == null) {
return false; // not found
} else {
int compare = title.compareToIgnoreCase(current.node.data);
if (compare == 0) {
return true; // found
} else {
current.parent = current.node;
if (compare < 0) {
current.node = current.node.left;
} else {
current.node = current.node.right;
}
}
}
}
private boolean remove(String title) {
NodeAndParent nodeAndParent = new NodeAndParent(root, null);
boolean found = find(title, nodeAndParent);
if (!found) {
// title was not found
} else if (nodeAndParent.parent == null) {
// found the root
} else {
// found another node
}
}
我正在尝试为我一直在研究的BST结构实现一个移除方法。以下是包含查找、插入和删除方法的代码: 我被告知可以使用insert方法来帮助我使用remove方法,但我只是不知道如何获取最小/最大的元素,然后用该值替换我正在删除的元素,然后递归地删除我获取替换值的节点,同时仍然保持O(logn)的复杂性。有人有什么想法或明显的漏洞我错过了,或任何其他有帮助的,因为我撞我的头在这个问题上? 编辑:我用答案的
在二元搜索树的情况下,为什么我们不能简单地在一个节点有两个子节点的情况下,将一个节点的前一个节点替换为后一个节点?
我试图为我的BST做一个递归加法。public add方法接受一个int参数,private方法接受相同的int和一个节点。这是我目前掌握的代码 我经常得到空指针,也尝试过调试,但我的逻辑中有一些我看不到的缺陷。
从二叉查找树中删除节点时,您可以将节点替换为左侧的最大子节点或右侧的最小子节点。 我很难理解以下实现执行删除操作的方式。 上面的代码包括以下步骤: < li >查找替换节点。 < li >让替换节点引用已删除节点的左右子节点。 < li >让已删除节点的左右子节点将替换节点作为父节点。 < li >让替换节点引用已删除节点的父节点作为自己的父节点。 < li >清理。 我有困难的部分特别是递归。据
我正在制作一个按字符串键排序的二叉搜索树。每个节点由一个与一个键相关联的无序信息链表组成。这棵树是按字母顺序排列的。 我已经完成了大部分的程序,但有麻烦的删除方法。 谢谢你。
我试图验证二叉查找树。给定二叉树的根,确定它是否是有效的二叉查找树(BST)。 有效的BST定义如下: 节点的左子树只包含键小于节点键的节点。节点的右子树只包含键大于节点键的节点。左子树和右子树也必须是二叉搜索树。 https://leetcode.com/problems/validate-binary-search-tree/ 我正在使用递归解决方案,但它未能通过以下测试用例: 输入:[2,1