如何修复 RedBlackTree 实现中的删除问题?

2024-01-03

这是我正在使用的 RedBlackTree 的实现(来自 Mark Allen Weiss,数据结构

public class RedBlackTree<AnyKey extends Comparable<? super AnyKey>,
            AnyValue extends Comparable<? super AnyValue>> 
         implements MyTreeMap<AnyKey, AnyValue>{
    private static final int BLACK = 1; 
    private static final int RED   = 0;

    
     // The psuedo(bogus) root, has a key value of negative infinity and a right link to the real root.
    private RedBlackNode<AnyKey, AnyValue> header;
    // Used in place of  a null link. Will always be colored black.
    private RedBlackNode<AnyKey, AnyValue> nullNode; 
    private RedBlackNode<AnyKey, AnyValue> current;
    private RedBlackNode<AnyKey, AnyValue> parent;     
    private RedBlackNode<AnyKey, AnyValue> grand;        
    private RedBlackNode<AnyKey, AnyValue> great;

    public RedBlackTree( ){
        nullNode = new RedBlackNode<AnyKey, AnyValue>( null, null );
        nullNode.left = nullNode.right = nullNode;
        header      = new RedBlackNode<AnyKey, AnyValue>( null, null );
        header.left = header.right = nullNode;
    }

    private final int compare( AnyKey theKey, RedBlackNode<AnyKey, AnyValue> t ){
        if( t == header )
            return 1;
        else
            return theKey.compareTo( t.key );    
    }

    private void insert( AnyKey theKey, AnyValue theValue ){
        current = parent = grand = header;
        nullNode.key = theKey;
        nullNode.value = theValue;
        while( compare( theKey, current ) != 0 ){
            great = grand; grand = parent; parent = current;
            current = compare( theKey, current ) < 0 ?
                    current.left : current.right;
            // Check if two red children; fix if so
            if( current.left.color == RED && current.right.color == RED )
                handleReorient( theKey);
        }
        // Insertion fails if already present
        if( current != nullNode )
            throw new IllegalArgumentException( theKey.toString( ) );
        current = new RedBlackNode<AnyKey, AnyValue>( theKey, theValue,
                nullNode, nullNode );
        // Attach to parent
        if( compare( theKey, parent ) < 0 )
            parent.left = current;
        else
            parent.right = current;
        handleReorient( theKey );
    }

    /**
     * Remove from the tree.
     * @param x the item to remove.
     * @throws UnsupportedOperationException if called.
     */
    public void remove( AnyKey x ){

    }

    /**
     * Internal routine that is called during an insertion
     * if a node has two red children. Performs flip and rotations.
     * @param item the item being inserted.
     */
    private void handleReorient( AnyKey key ){
        // Do the color flip
        current.color = RED;
        current.left.color = BLACK;
        current.right.color = BLACK;
        if( parent.color == RED ){   // Have to rotate
            grand.color = RED;
            if( ( compare( key, grand ) < 0 ) !=
                    ( compare( key, parent ) < 0 ) )
                parent = rotate( key, grand );  // Start dbl rotate
            current = rotate( key, great );
            current.color = BLACK;
        }
        header.right.color = BLACK; // Make root black
    }

    /**
     * Internal routine that performs a single or double rotation.
     * Because the result is attached to the parent, there are four cases.
     * Called by handleReorient.
     * @param item the item in handleReorient.
     * @param parent the parent of the root of the rotated subtree.
     * @return the root of the rotated subtree.
     */
    private RedBlackNode<AnyKey, AnyValue> rotate( AnyKey item, RedBlackNode<AnyKey, AnyValue> parent ){
        if( compare( item, parent ) < 0 )
            return parent.left = compare( item, parent.left ) < 0 ?
                    rotateWithLeftChild( parent.left )  :  // LL
                        rotateWithRightChild( parent.left ) ;  // LR
                    else
                        return parent.right = compare( item, parent.right ) < 0 ?
                                rotateWithLeftChild( parent.right ) :  // RL
                                    rotateWithRightChild( parent.right );  // RR
    }

    /**
     * Rotate binary tree node with left child.
     * @param k2 the node to rotate with.
     */
    private static <AnyKey, AnyValue> RedBlackNode<AnyKey, AnyValue> rotateWithLeftChild( RedBlackNode<AnyKey, AnyValue> k2 ){
        RedBlackNode<AnyKey, AnyValue> k1 = k2.left;
        k2.left = k1.right;
        k1.right = k2;
        return k1;
    }
    @Override
public void put(AnyKey x, AnyValue y) {
    RedBlackNode<AnyKey, AnyValue> bN = find(x);
    if(bN == null) {
        insert(x, y);
    } else {
        bN.value = y;
    }
}
    /**
     * Rotate binary tree node with right child.
     * @param k1 the node to rotate with.
     */
    private static <AnyKey, AnyValue> RedBlackNode<AnyKey, AnyValue> rotateWithRightChild( RedBlackNode<AnyKey, AnyValue> k1 ){
        RedBlackNode<AnyKey, AnyValue> k2 = k1.right;
        k1.right = k2.left;
        k2.left = k1;
        return k2;
    }
    
    /**
     * Node in the red black tree.
     * @author Letian Sun
     * @param <AnyKey> can be any object type
     * @param <AnyValue> can be any object type 
     */
    private static class RedBlackNode<AnyKey, AnyValue>{
         AnyKey key;
         AnyValue value;   
         RedBlackNode<AnyKey, AnyValue> left;  
         RedBlackNode<AnyKey, AnyValue> right;     
         
         int color;   
         
        RedBlackNode( AnyKey theKey, AnyValue theValue ){
            this( theKey, theValue,  null, null );
        }        
        RedBlackNode( AnyKey theKey, AnyValue theValue, RedBlackNode<AnyKey, AnyValue> lt
                , RedBlackNode<AnyKey, AnyValue> rt ) {
            key  = theKey;
            value = theValue;
            left     = lt;
            right    = rt;
            color    = RedBlackTree.BLACK;
        }
       public String toString() {
         return "element:" + key.toString() 
                 + " times:" + value.toString();
     }
  }
    

我正在尝试编写作者没有编写的删除方法。 这是我到目前为止所拥有的

@Override
public void remove( AnyKey x ){
    header.right = remove( x, header.right );
}

private RedBlackNode<AnyKey, AnyValue> remove( AnyKey x, 
        RedBlackNode<AnyKey, AnyValue> t ){
    if( t == nullNode )
        return t;   // Item not found; do nothing
    int compareResult = x.compareTo( t.key );
    if( compareResult < 0 )
        t.left = remove( x, t.left );
    else if( compareResult > 0 )
        t.right = remove( x, t.right );
    else if( t.left != nullNode && t.right != nullNode ){ // Two children
        t.key = findMin( t.right ).key;
        t.right = remove( t.key, t.right );
    }
    else
        t = ( t.left != nullNode ) ? t.left : t.right;
    return rotate(t.key, t );
}

private RedBlackNode<AnyKey, AnyValue> findMin(RedBlackNode<AnyKey, AnyValue> t ){
    if(t != null && t.left != null) {
        t = findMin(t.left);
    }
    return t;
}
        

有人发现我的方法有任何问题吗?所有的逻辑对我来说都是有意义的。 当我运行 JUnit 测试用例时,

RedBlackTree testTree = new RedBlackTree<Integer,Integer>();
for(int c=0;c<1000;c++) {
    testTree.put(c, c);
}
int attempt = 60;
testTree.remove(attempt);
assertEquals(attempt - 1,testTree.get(attempt - 1).intValue());

测试失败


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复 RedBlackTree 实现中的删除问题? 的相关文章

随机推荐

  • Newtonsoft.Json SerializeObject 不带转义反斜杠

    给出代码 dynamic foo new ExpandoObject foo Bar something string json Newtonsoft Json JsonConvert SerializeObject foo 输出如下 Ba
  • 在 JavaScript 中缩放到没有画布的光标

    我有一个 img 通过调整鼠标滚轮滚动来缩放transform scale 我希望缩放像 Google 地图一样 您可以缩放到鼠标光标所在的位置 而不是图像的中心 不过 我不想使用画布 只是为了学习体验 这也是我发现的其他问题并没有真正帮助
  • WSO2 Identity Server Service Pack 的来源

    WSO2 Identity Server 5 0 0 的 Service Pack 1 的源是否公开可用 我在哪里可以找到 SVN 存储库中的源代码 Service Pack 没有任何源代码 服务包是通过聚合为 WSO2 中的产品提供的补丁
  • Rails 中的 Kerberos 身份验证

    是否可以使用 kerberos 对 Rails 下的用户进行身份验证 是否有任何现有插件 最好是扩展 authlogic 的功能 来执行此操作 我希望其他人能够向我们展示一种纯粹的 Rails 方法来做到这一点 但在那之前 让事情顺利进行的
  • 开发cocos-lua游戏时,Android中string.find中文字符失败,PC上成功

    我尝试使用string find 中国 中 我开发cocos lua游戏时 在PC上成功 但在Android上失败 在安卓上 string find return nil 首先 我认为它们的编码可能不同 所以我尝试打印出它们的字节 Andr
  • 使用 `*((*(&array + 1)) - 1)` 获取自动数组的最后一个元素是否安全?

    假设我想获取大小未知的自动数组的最后一个元素 我知道我可以利用sizeof运算符来获取数组的大小并相应地获取最后一个元素 正在使用 array 1 1 safe Like char array SOME SIZE printf Last e
  • 从android中的地址获取纬度和经度

    我尝试从地址获取纬度和经度 问题是 当我只给出城市名称时 它会给出正确的纬度和经度 而当我给出完整的地址 如州 城市名称 街道号码 时 它会给出正确的纬度和经度没有给我正确的纬度和经度 感谢您的配合回复 我的代码是 String addre
  • Ant 任务检查数据库(连接)是否存在?

    ANT 是否有可能在不导致构建失败的情况下检查数据库 连接 是否存在 例如
  • 按 utc 日期而不是服务器日期滚动文件

    这是我的 log4net xml 文件
  • pyqt:如何从 QVBoxLayout 中删除元素?

    我想要一个多颜色选择小部件 我这样做的方式是有一个 按钮和一个最初为空的 vbox 当按下 时 它会向包含 按钮和 3 个旋转框的 vbox 添加一个 QHBoxLayout 当按下 按钮时 我希望该行消失 并且所有内容都恢复到添加该行之前
  • DLib:train_shape_predictor_ex.cpp

    我正在尝试通过执行来训练 Dlib 的形状预测器train dlib shape predictor ex cpp http dlib net train shape predictor ex cpp html on 海伦数据集 http
  • ng-content 选择绑定变量

    我正在尝试使用 Angular 2 创建一个表单生成器 一个非常基本的示例如下 this fields name Name type text name Age type number 但我也想支持自定义元素 例如 this fields
  • ResourceDictionary 源绑定到模块(用于本地化)

    我有一个 XAML 窗口 其中有一组绑定到对象的字符串 如下所示
  • Sonarqube:查看涵盖源代码的单元测试

    我们在 Bamboo 中有一个 CI 设置 它运行 Junit 测试并使用 Jacoco 计算单元测试覆盖率 然后我们运行Sonar插件进行源代码分析 一切都运行良好 我们可以看到 SonarCube 服务器上的分析 包括覆盖范围 但我们希
  • Symfony 框架的最佳论坛插件解决方案是什么?

    我正在寻找一个好的解决方案整合论坛进入 symfony 应用程序 像 phpBB 这样的东西会很棒 我见过 phpBB 插件与 symfony 集成 但这不足以满足我的目的 而且 在我看来 映射数据库表是一种蹩脚的方法 如果有人知道 Sym
  • 如何在微服务/容器/云环境中管理机密?

    微服务和云是一回事 每个人都在谈论和写作 就我个人而言 我对这个主题思考了很多 如何利用它从中受益 可能面临哪些挑战 这如何加速日常开发 以及如何管理一切 几天来困扰我的一个问题是 如何在微服务 云环境中管理机密 想象一下一家拥有 150
  • 如何在 Chrome 上下载文件而不自动将文件重命名为“下载”?

    我使用 javascript 生成文件并下载它 看来 根据 chrome 版本的不同 下载文件名可以自动重命名为 download 有办法避免吗 这是我的代码 var link document createElement a link s
  • UISearchBar 使用 Storyboard 实现

    我对 iOS 开发非常陌生 但也很兴奋 我构建了一个应用程序 它使用故事板并使用 plist 文件的内容填充 UITableView 到目前为止 我设法让一切运行良好 但现在我想添加一个搜索栏 就像联系人应用程序中的搜索栏一样 本质上这就是
  • 如何访问Singleton类的静态方法?

    我对单例类有一些困惑 以下是我的一些观点 单例类可以有静态方法吗 如果是的话我们如何调用该方法 静态类和单例类之间的主要区别是什么 我创建了我的单例类 如下所示 public class Singleton private static S
  • 如何修复 RedBlackTree 实现中的删除问题?

    这是我正在使用的 RedBlackTree 的实现 来自 Mark Allen Weiss 数据结构 public class RedBlackTree