【问题标题】:RedBlackTree Mark Allen Weis Remove(x) Top Bottom ApproachRedBlackTree Mark Allen Weis 移除(x)顶部底部方法
【发布时间】:2012-06-04 07:46:14
【问题描述】:

来自 Data Structures and Problem Solving Using Java 的原始 Mark Allen Weis RedBlackTree 实现找到 here

我似乎无法理解从树中删除节点的问题。在阅读了wikipedia resource 之后,我注意到“is_leaf()”函数.. 这个和 Mark Weis 实现的问题是没有办法分辨哪个节点是叶子,哪个不是叶子

void delete_one_child(struct node *n)
{
    /*
     * Precondition: n has at most one non-null child.
     */
    struct node *child = is_leaf(n->right) ? n->left : n->right;

    replace_node(n, child);
    if (n->color == BLACK) {
            if (child->color == RED)
                    child->color = BLACK;
            else
                    delete_case1(child);
    }
    free(n);
}

Is_Leaf java 实现

public boolean isLeaf(){
if(left == null && right == null){
return false;
}
return true;
}

控制台输出

value=1 color=1 leaf=true left=null right=14
value=2 color=1 leaf=true left=null right=5
value=5 color=0 leaf=true left=null right=null
value=7 color=0 leaf=true left=2 right=11
value=8 color=0 leaf=true left=null right=null
value=11 color=1 leaf=true left=8 right=null
value=14 color=1 leaf=true left=7 right=15
value=15 color=1 leaf=true left=null right=null

树格式(来自控制台)

└── (1) 1
    └── (1) 14
        ├── (0) 7
        │   ├── (1) 2
        │   │   └── (0) 5
        │   └── (1) 11
        │       └── (0) 8
        └── (1) 15

规则:

  1. 根是黑色的
  2. 每个红色节点都有一个黑色父节点
  3. 红色节点的所有子节点都是黑色的 – 红色节点不能有红色子节点
  4. 从根到叶的每条路径都包含相同的编号 黑色节点数

所以我的问题是如何实现从 this 实现中删除 Red and Back Trees?

【问题讨论】:

    标签: java red-black-tree


    【解决方案1】:

    我认为这是正确的 isLeaf() 代码:

    public boolean isLeaf(RedBlackNode<AnyType> t ){
        if(t.left.element == null && t.right.element == null){
            return true;
        }
        return false;
    }
    

    【讨论】:

    • 没有。叶子的标准是它的左右节点 pointers 为空,即它们不引用它们下面的其他节点。如果 left 或 right 为 null,您的代码将抛出异常,因为它试图访问 null 元素的属性。
    • @matthewdavidson 在查看 insert() 代码时肯定看起来不正确。在插入代码中,当它创建一个新的“叶”节点时,它使用以下行:“current = new RedBlackNode(item, nullNode, nullNode);”它创建了一个具有左右非空指针的新节点。这就是为什么你必须检查这些指针的元素字段。
    • 我明白你在说什么,但叶子不是节点。这就是我要说的。 “当前”是一个节点,而不是叶子。 'left' 和 'right' 是叶子。一个节点位于头部和叶子之间,它的左右指针指向一个节点、一个叶子或什么都没有。一片叶子在末端,指针指向无。查看代码在上述测试中的行为方式。叶子的左右指针总是指向null,并且它们的元素不为null。
    • @Justin 你能看看我的 RedBlackTree 删除方法吗? stackoverflow.com/questions/28705454/…
    【解决方案2】:

    你问如何实现isLeaf()?如果是这样,您应该将 isLeaf 传递给您要检查的节点。

    public boolean isLeaf(RedBlackNode<AnyType> t ){
    if(t.left == null && t.right == null){
    return false;
    }
    return true;
    }
    

    否则,算法本身看起来是正确的,唯一真正的工作是将 C 翻译成 Java,这很容易。

    【讨论】:

    • 我在你的插入中也注意到了这一点: if( current != nullNode ) throw new DuplicateItemException( item.toString( ) ); current = new RedBlackNode( item, nullNode, nullNode );我认为您可能想要检查 current.element != nullNode.element 因为 nullNode 的值已设置,我认为这可能是您似乎插入重复项的原因。
    【解决方案3】:

    您必须检查nullnode 而不是null

    public boolean isLeaf() {
        if(left == nullnode && right == nullnode) {
            return false;
        }
        return true;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多