如何修复 RBT 树中的 RBT 属性

How to fix RBT properties in a RBT tree

提问人:Dan Stephens 提问时间:11/12/2023 最后编辑:President James K. PolkDan Stephens 更新时间:11/12/2023 访问量:49

问:

如果有人能帮助我开始如何修复 RBT 属性,我将不胜感激。我有一个 insertNode 方法,它根据 BST 属性插入一个节点,然后修复任何 RBT 问题。我想知道如何通过实施案例 1 和案例 2 来开始修复。其中情况 1 是节点 X 是根,根必须是黑色的,所以我们将 x 染成黑色。案例 2 是 X.uncle 是红色的,Color parent 和 uncle 是黑色的,grandparent 是红色的。但是,我遇到的问题是,在我的节点类中,我没有任何父节点或getParent()方法,我无法更改它,因此我需要一种不同的方式来处理父级/祖父级。

 private Node nodeInsertData(Node r, int x) {
        // BST Insertion
        if (r == null) return new Node(x, Node.Color.RED); 
            // Insert the new node accordingly to the basic rules of BST.
        else if (x < r.getData()) {
            r.setLeft(nodeInsertData(r.getLeft(), x)); 
        } else if (x > r.getData()) {
            r.setRight(nodeInsertData(r.getRight(), x));
        }
        // RBT Fixes

        return r; 
    }

有关如何开始实施修复的任何帮助将不胜感激。

java 数据结构 binary-search-tree 红黑树

评论


答: 暂无答案