最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Java實現(xiàn)紅黑樹

 更新時間:2021年09月15日 09:44:18   作者:胡不慌  
紅黑樹是眾多“平衡的”搜索樹模式中的一種,在最壞情況下,它相關操作的時間復雜度為O(log n),接下倆小編將子啊下文詳細介紹Java是如何實現(xiàn)紅黑樹的

1、紅黑樹的屬性

紅黑樹是一種二分查找樹,與普通的二分查找樹不同的一點是,紅黑樹的每個節(jié)點都有一個顏色(color)屬性。該屬性的值要么是紅色,要么是黑色。

通過限制從根到葉子的任何簡單路徑上的節(jié)點顏色,紅黑樹確保沒有比任何其他路徑長兩倍的路徑,從而使樹近似平衡。

假設紅黑樹節(jié)點的屬性有鍵(key)、顏色(color)、左子節(jié)點(left)、右子節(jié)點(right),父節(jié)點(parent)。

一棵紅黑樹必須滿足下面有下面這些特性( 紅黑樹特性 ):

  • 樹中的每個節(jié)點要么是紅色,要么是黑色;
  • 根節(jié)點是黑色;
  • 每個葉子節(jié)點(null)是黑色;
  • 如果某節(jié)點是紅色的,它的兩個子節(jié)點都是黑色;
  • 對于每個節(jié)點到后面任一葉子節(jié)點(null)的所有路徑,都有相同數(shù)量的黑色節(jié)點。

為了在紅黑樹代碼中處理邊界條件方便,我們用一個哨兵變量代替null。對于一個紅黑樹tree,哨兵變量RedBlackTree.NULL(下文代碼中)是一個和其它節(jié)點有同樣屬性的節(jié)點,它的顏色(color)屬性是黑色,其它屬性可以任意取值。

我們使用哨兵變量是因為我們可以把一個節(jié)點node的子節(jié)點null當成一個普通節(jié)點。

在這里,我們使用哨兵變量RedBlackTree.NULL代替樹中所有的null(所有的葉子節(jié)點及根節(jié)點的父節(jié)點)。

我們把從一個節(jié)點n(不包括)到任一葉子節(jié)點路徑上的黑色節(jié)點的個數(shù)稱為 黑色高度 ,用bh(n)表示。一棵紅黑樹的黑色高度是其根節(jié)點的黑色高度。

關于紅黑樹的搜索,求最小值,求最大值,求前驅,求后繼這些操作的代碼與二分查找樹的這些操作的代碼基本一致。可以在用java實現(xiàn)二分查找樹查看。

結合上文給出下面的代碼。

用一個枚舉類Color表示顏色:

public enum Color {
    Black("黑色"), Red("紅色");

    private String color;

    private Color(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return color;
    }
}

類Node表示節(jié)點:

public class Node {
    public int key;
    public Color color;
    public Node left;
    public Node right;
    public Node parent;

    public Node() {
    }

    public Node(Color color) {
        this.color = color;
    }

    public Node(int key) {
        this.key = key;
        this.color = Color.Red;
    }

    public int height() {
        return Math.max(left != RedBlackTree.NULL ? left.height() : 0, right != RedBlackTree.NULL ? right.height() : 0) + 1;
    }

    public Node minimum() {
        Node pointer = this;
        while (pointer.left != RedBlackTree.NULL)
            pointer = pointer.left;
        return pointer;
    }

    @Override
    public String toString() {
        String position = "null";
        if (this.parent != RedBlackTree.NULL)
            position = this.parent.left == this ? "left" : "right";
        return "[key: " + key + ", color: " + color + ", parent: " + parent.key + ", position: " + position + "]";
    }
}

類RedTreeNode表示紅黑樹:

public class RedBlackTree {

    // 表示哨兵變量
    public final static Node NULL = new Node(Color.Black);

    public Node root;

    public RedBlackTree() {
        this.root = NULL;
    }

}

2、旋轉

紅黑樹的插入和刪除操作,能改變紅黑樹的結構,可能會使它不再有前面所說的某些特性性。為了維持這些特性,我們需要改變樹中某些節(jié)點的顏色和位置。

我們可以通過旋轉改變節(jié)點的結構。主要有 左旋轉 右旋轉 兩種方式。具體如下圖所示。

左旋轉:把一個節(jié)點n的右子節(jié)點right變?yōu)樗母腹?jié)點,n變?yōu)閞ight的左子節(jié)點,所以right不能為null。這時n的右指針空了出來,right的左子樹被n擠掉,所以right原來的左子樹稱為n的右子樹。

右旋轉:把一個節(jié)點n的左子節(jié)點left變?yōu)樗母腹?jié)點,n變?yōu)閘eft的右子節(jié)點,所以left不能為null。這時n的左指針被空了出來,left的右子樹被n擠掉,所以left原來的右子樹被稱為n的左子樹。

可在RedTreeNode類中,加上如下實現(xiàn)代碼:

public void leftRotate(Node node) {
        Node rightNode = node.right;

        node.right = rightNode.left;
        if (rightNode.left != RedBlackTree.NULL)
            rightNode.left.parent = node;

        rightNode.parent = node.parent;
        if (node.parent == RedBlackTree.NULL)
            this.root = rightNode;
        else if (node.parent.left == node)
            node.parent.left = rightNode;
        else
            node.parent.right = rightNode;

        rightNode.left = node;
        node.parent = rightNode;
    }

    public void rightRotate(Node node) {
        Node leftNode = node.left;

        node.left = leftNode.right;
        if (leftNode.right != RedBlackTree.NULL)
            leftNode.right.parent = node;

        leftNode.parent = node.parent;
        if (node.parent == RedBlackTree.NULL) {
            this.root = leftNode;
        } else if (node.parent.left == node) {
            node.parent.left = leftNode;
        } else {
            node.parent.right = leftNode;
        }

        leftNode.right = node;
        node.parent = leftNode;
    }

3、插入

紅黑樹的插入代碼與二分查找樹的插入代碼非常相似。只不過紅黑樹的插入操作會改變紅黑樹的結構,使其不在有該有的特性。

在這里,新插入的節(jié)點默認是紅色。

所以在插入節(jié)點之后,要有維護紅黑樹特性的代碼。

public void insert(Node node) {
        Node parentPointer = RedBlackTree.NULL;
        Node pointer = this.root;

        while (this.root != RedBlackTree.NULL) {
            parentPointer = pointer;
            pointer = node.key < pointer.key ? pointer.left : pointer.right;
        }

        node.parent = parentPointer;
        if(parentPointer == RedBlackTree.NULL) {
            this.root = node;
        }else if(node.key < parentPointer.key) {
            parentPointer.left = node;
        }else {
            parentPointer.right = node;
        }

        node.left = RedBlackTree.NULL;
        node.right = RedBlackTree.NULL;
        node.color = Color.Red;
        // 維護紅黑樹屬性的方法
        this.insertFixUp(node);
    }

用上述方法插入一個新節(jié)點的時候,有兩類情況會違反紅黑樹的特性。

  • 當樹中沒有節(jié)點時,此時插入的節(jié)點稱為根節(jié)點,而此節(jié)點的顏色為紅色。
  • 當新插入的節(jié)點成為一個紅色節(jié)點的子節(jié)點時,此時存在一個紅色結點有紅色子節(jié)點的情況。

對于第一類情況,可以直接把根結點設置為黑色;而針對第二類情況,需要根據(jù)具體條件,做出相應的解決方案。

具體代碼如下:

public void insertFixUp(Node node) {
        // 當node不是根結點,且node的父節(jié)點顏色為紅色
        while (node.parent.color == Color.Red) {
            // 先判斷node的父節(jié)點是左子節(jié)點,還是右子節(jié)點,這不同的情況,解決方案也會不同
            if (node.parent == node.parent.parent.left) {
                Node uncleNode = node.parent.parent.right;
                if (uncleNode.color == Color.Red) {  // 如果叔叔節(jié)點是紅色,則父父一定是黑色
                    // 通過把父父節(jié)點變成紅色,父節(jié)點和兄弟節(jié)點變成黑色,然后在判斷父父節(jié)點的顏色是否合適
                    uncleNode.color = Color.Black;
                    node.parent.color = Color.Black;
                    node.parent.parent.color = Color.Red;
                    node = node.parent.parent;
                } else if (node == node.parent.right) {
                    node = node.parent;
                    this.leftRotate(node);
                } else {
                    node.parent.color = Color.Black;
                    node.parent.parent.color = Color.Red;
                    this.rightRotate(node.parent.parent);
                }
            } else {
                Node uncleNode = node.parent.parent.left;
                if (uncleNode.color == Color.Red) {
                    uncleNode.color = Color.Black;
                    node.parent.color = Color.Black;
                    node.parent.parent.color = Color.Red;
                    node = node.parent.parent;
                } else if (node == node.parent.left) {
                    node = node.parent;
                    this.rightRotate(node);
                } else {
                    node.parent.color = Color.Black;
                    node.parent.parent.color = Color.Red;
                    this.leftRotate(node.parent.parent);
                }
            }
        }
        // 如果之前樹中沒有節(jié)點,那么新加入的點就成了新結點,而新插入的結點都是紅色的,所以需要修改。
        this.root.color = Color.Black;
    }

下面的圖分別對應第二類情況中的六種及相應處理結果。

情況1:

情況2:

情況3:

情況4:

情況5:

情況6:

4、刪除

紅黑樹中節(jié)點的刪除會使一個結點代替另外一個節(jié)點。所以先要實現(xiàn)這樣的代碼:

public void transplant(Node n1, Node n2) {
        if(n1.parent == RedBlackTree.NULL){
            this.root = n2;
        }else if(n1.parent.left == n1) {
            n1.parent.left = n2;
        }else {
            n1.parent.right = n2;
        }
        n2.parent = n1.parent;
    }


紅黑樹的刪除節(jié)點代碼是基于二分查找樹的刪除節(jié)點代碼而寫的。

刪除結點代碼:

public void delete(Node node) {
        Node pointer1 = node;
        // 用于記錄被刪除的顏色,如果是紅色,可以不用管,但如果是黑色,可能會破壞紅黑樹的屬性
        Color pointerOriginColor = pointer1.color;
        // 用于記錄問題的出現(xiàn)點
        Node pointer2;
        if (node.left == RedBlackTree.NULL) {
            pointer2 = node.right;
            this.transplant(node, node.right);
        } else if (node.right == RedBlackTree.NULL) {
            pointer2 = node.left;
            this.transplant(node, node.left);
        } else {
            // 如要刪除的字節(jié)有兩個子節(jié)點,則找到其直接后繼(右子樹最小值),直接后繼節(jié)點沒有非空左子節(jié)點。
            pointer1 = node.right.minimum();
            // 記錄直接后繼的顏色和其右子節(jié)點
            pointerOriginColor = pointer1.color;
            pointer2 = pointer1.right;
            // 如果其直接后繼是node的右子節(jié)點,不用進行處理
            if (pointer1.parent == node) {
                pointer2.parent = pointer1;
            } else {
                // 否則,先把直接后繼從樹中提取出來,用來替換node
                this.transplant(pointer1, pointer1.right);
                pointer1.right = node.right;
                pointer1.right.parent = pointer1;
            }
            // 用node的直接后繼替換node,繼承node的顏色
            this.transplant(node, pointer1);
            pointer1.left = node.left;
            pointer1.left.parent = pointer1;
            pointer1.color = node.color;
        }
        if (pointerOriginColor == Color.Black) {
            this.deleteFixUp(pointer2);
        }
    }

當被刪除節(jié)點的顏色是黑色時需要調用方法維護紅黑樹的特性。

主要有兩類情況:

  • 當node是紅色時,直接變成黑色即可。
  • 當node是黑色時,需要調整紅黑樹結構。,
private void deleteFixUp(Node node) {
        // 如果node不是根節(jié)點,且是黑色
        while (node != this.root && node.color == Color.Black) {
            // 如果node是其父節(jié)點的左子節(jié)點
            if (node == node.parent.left) {
                // 記錄node的兄弟節(jié)點
                Node pointer1 = node.parent.right;
                // 如果他兄弟節(jié)點是紅色
                if (pointer1.color == Color.Red) {
                    pointer1.color = Color.Black;
                    node.parent.color = Color.Red;
                    leftRotate(node.parent);
                    pointer1 = node.parent.right;
                }
                if (pointer1.left.color == Color.Black && pointer1.right.color == Color.Black) {
                    pointer1.color = Color.Red;
                    node = node.parent;
                } else if (pointer1.right.color == Color.Black) {
                    pointer1.left.color = Color.Black;
                    pointer1.color = Color.Red;
                    rightRotate(pointer1);
                    pointer1 = node.parent.right;
                } else {
                    pointer1.color = node.parent.color;
                    node.parent.color = Color.Black;
                    pointer1.right.color = Color.Black;
                    leftRotate(node.parent);
                    node = this.root;
                }
            } else {
                // 記錄node的兄弟節(jié)點
                Node pointer1 = node.parent.left;
                // 如果他兄弟節(jié)點是紅色
                if (pointer1.color == Color.Red) {
                    pointer1.color = Color.Black;
                    node.parent.color = Color.Red;
                    rightRotate(node.parent);
                    pointer1 = node.parent.left;
                }
                if (pointer1.right.color == Color.Black && pointer1.left.color == Color.Black) {
                    pointer1.color = Color.Red;
                    node = node.parent;
                } else if (pointer1.left.color == Color.Black) {
                    pointer1.right.color = Color.Black;
                    pointer1.color = Color.Red;
                    leftRotate(pointer1);
                    pointer1 = node.parent.left;
                } else {
                    pointer1.color = node.parent.color;
                    node.parent.color = Color.Black;
                    pointer1.left.color = Color.Black;
                    rightRotate(node.parent);
                    node = this.root;
                }
            }

        }
        node.color = Color.Black;
    }

對第二類情況,有下面8種:

情況1:

情況2:

情況3:

情況4:

情況5:

情況6:

情況7:

情況8:

5、所有代碼

public enum Color {
    Black("黑色"), Red("紅色");

    private String color;

    private Color(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return color;
    }
}
public class Node {
    public int key;
    public Color color;
    public Node left;
    public Node right;
    public Node parent;

    public Node() {
    }

    public Node(Color color) {
        this.color = color;
    }

    public Node(int key) {
        this.key = key;
        this.color = Color.Red;
    }

    /**
     * 求在樹中節(jié)點的高度
     * 
     * @return
     */
    public int height() {
        return Math.max(left != RedBlackTree.NULL ? left.height() : 0, right != RedBlackTree.NULL ? right.height() : 0) + 1;
    }

    /**
     * 在以該節(jié)點為根節(jié)點的樹中,求最小節(jié)點
     * 
     * @return
     */
    public Node minimum() {
        Node pointer = this;
        while (pointer.left != RedBlackTree.NULL)
            pointer = pointer.left;
        return pointer;
    }

    @Override
    public String toString() {
        String position = "null";
        if (this.parent != RedBlackTree.NULL)
            position = this.parent.left == this ? "left" : "right";
        return "[key: " + key + ", color: " + color + ", parent: " + parent.key + ", position: " + position + "]";
    }
}
import java.util.LinkedList;
import java.util.Queue;

public class RedBlackTree {

    public final static Node NULL = new Node(Color.Black);

    public Node root;

    public RedBlackTree() {
        this.root = NULL;
    }

    /**
     * 左旋轉
     * 
     * @param node
     */
    public void leftRotate(Node node) {
        Node rightNode = node.right;

        node.right = rightNode.left;
        if (rightNode.left != RedBlackTree.NULL)
            rightNode.left.parent = node;

        rightNode.parent = node.parent;
        if (node.parent == RedBlackTree.NULL)
            this.root = rightNode;
        else if (node.parent.left == node)
            node.parent.left = rightNode;
        else
            node.parent.right = rightNode;

        rightNode.left = node;
        node.parent = rightNode;
    }

    /**
     * 右旋轉
     * 
     * @param node
     */
    public void rightRotate(Node node) {
        Node leftNode = node.left;

        node.left = leftNode.right;
        if (leftNode.right != RedBlackTree.NULL)
            leftNode.right.parent = node;

        leftNode.parent = node.parent;
        if (node.parent == RedBlackTree.NULL) {
            this.root = leftNode;
        } else if (node.parent.left == node) {
            node.parent.left = leftNode;
        } else {
            node.parent.right = leftNode;
        }

        leftNode.right = node;
        node.parent = leftNode;
    }

    public void insert(Node node) {
        Node parentPointer = RedBlackTree.NULL;
        Node pointer = this.root;

        while (pointer != RedBlackTree.NULL) {
            parentPointer = pointer;
            pointer = node.key < pointer.key ? pointer.left : pointer.right;
        }

        node.parent = parentPointer;
        if (parentPointer == RedBlackTree.NULL) {
            this.root = node;
        } else if (node.key < parentPointer.key) {
            parentPointer.left = node;
        } else {
            parentPointer.right = node;
        }

        node.left = RedBlackTree.NULL;
        node.right = RedBlackTree.NULL;
        node.color = Color.Red;
        this.insertFixUp(node);
    }

    private void insertFixUp(Node node) {
        // 當node不是根結點,且node的父節(jié)點顏色為紅色
        while (node.parent.color == Color.Red) {
            // 先判斷node的父節(jié)點是左子節(jié)點,還是右子節(jié)點,這不同的情況,解決方案也會不同
            if (node.parent == node.parent.parent.left) {
                Node uncleNode = node.parent.parent.right;
                if (uncleNode.color == Color.Red) { // 如果叔叔節(jié)點是紅色,則父父一定是黑色
                    // 通過把父父節(jié)點變成紅色,父節(jié)點和兄弟節(jié)點變成黑色,然后在判斷父父節(jié)點的顏色是否合適
                    uncleNode.color = Color.Black;
                    node.parent.color = Color.Black;
                    node.parent.parent.color = Color.Red;
                    node = node.parent.parent;
                } else if (node == node.parent.right) { // node是其父節(jié)點的右子節(jié)點,且叔叔節(jié)點是黑色
                    // 對node的父節(jié)點進行左旋轉
                    node = node.parent;
                    this.leftRotate(node);
                } else { // node如果叔叔節(jié)點是黑色,node是其父節(jié)點的左子節(jié)點,父父節(jié)點是黑色
                    // 把父節(jié)點變成黑色,父父節(jié)點變成紅色,對父父節(jié)點進行右旋轉
                    node.parent.color = Color.Black;
                    node.parent.parent.color = Color.Red;
                    this.rightRotate(node.parent.parent);
                }
            } else {
                Node uncleNode = node.parent.parent.left;
                if (uncleNode.color == Color.Red) {
                    uncleNode.color = Color.Black;
                    node.parent.color = Color.Black;
                    node.parent.parent.color = Color.Red;
                    node = node.parent.parent;
                } else if (node == node.parent.left) {
                    node = node.parent;
                    this.rightRotate(node);
                } else {
                    node.parent.color = Color.Black;
                    node.parent.parent.color = Color.Red;
                    this.leftRotate(node.parent.parent);
                }
            }
        }
        // 如果之前樹中沒有節(jié)點,那么新加入的點就成了新結點,而新插入的結點都是紅色的,所以需要修改。
        this.root.color = Color.Black;
    }

    /**
     * n2替代n1
     * 
     * @param n1
     * @param n2
     */
    private void transplant(Node n1, Node n2) {

        if (n1.parent == RedBlackTree.NULL) { // 如果n1是根節(jié)點
            this.root = n2;
        } else if (n1.parent.left == n1) { // 如果n1是其父節(jié)點的左子節(jié)點
            n1.parent.left = n2;
        } else { // 如果n1是其父節(jié)點的右子節(jié)點
            n1.parent.right = n2;
        }
        n2.parent = n1.parent;
    }

    /**
     * 刪除節(jié)點node
     * 
     * @param node
     */
    public void delete(Node node) {
        Node pointer1 = node;
        // 用于記錄被刪除的顏色,如果是紅色,可以不用管,但如果是黑色,可能會破壞紅黑樹的屬性
        Color pointerOriginColor = pointer1.color;
        // 用于記錄問題的出現(xiàn)點
        Node pointer2;
        if (node.left == RedBlackTree.NULL) {
            pointer2 = node.right;
            this.transplant(node, node.right);
        } else if (node.right == RedBlackTree.NULL) {
            pointer2 = node.left;
            this.transplant(node, node.left);
        } else {
            // 如要刪除的字節(jié)有兩個子節(jié)點,則找到其直接后繼(右子樹最小值),直接后繼節(jié)點沒有非空左子節(jié)點。
            pointer1 = node.right.minimum();
            // 記錄直接后繼的顏色和其右子節(jié)點
            pointerOriginColor = pointer1.color;
            pointer2 = pointer1.right;
            // 如果其直接后繼是node的右子節(jié)點,不用進行處理
            if (pointer1.parent == node) {
                pointer2.parent = pointer1;
            } else {
                // 否則,先把直接后繼從樹中提取出來,用來替換node
                this.transplant(pointer1, pointer1.right);
                pointer1.right = node.right;
                pointer1.right.parent = pointer1;
            }
            // 用node的直接后繼替換node,繼承node的顏色
            this.transplant(node, pointer1);
            pointer1.left = node.left;
            pointer1.left.parent = pointer1;
            pointer1.color = node.color;
        }
        if (pointerOriginColor == Color.Black) {
            this.deleteFixUp(pointer2);
        }
    }

    /**
     * The procedure RB-DELETE-FIXUP restores properties 1, 2, and 4
     * 
     * @param node
     */
    private void deleteFixUp(Node node) {
        // 如果node不是根節(jié)點,且是黑色
        while (node != this.root && node.color == Color.Black) {
            // 如果node是其父節(jié)點的左子節(jié)點
            if (node == node.parent.left) {
                // 記錄node的兄弟節(jié)點
                Node pointer1 = node.parent.right;
                // 如果node兄弟節(jié)點是紅色
                if (pointer1.color == Color.Red) {
                    pointer1.color = Color.Black;
                    node.parent.color = Color.Red;
                    leftRotate(node.parent);
                    pointer1 = node.parent.right;
                }
                if (pointer1.left.color == Color.Black && pointer1.right.color == Color.Black) {
                    pointer1.color = Color.Red;
                    node = node.parent;
                } else if (pointer1.right.color == Color.Black) {
                    pointer1.left.color = Color.Black;
                    pointer1.color = Color.Red;
                    rightRotate(pointer1);
                    pointer1 = node.parent.right;
                } else {
                    pointer1.color = node.parent.color;
                    node.parent.color = Color.Black;
                    pointer1.right.color = Color.Black;
                    leftRotate(node.parent);
                    node = this.root;
                }
            } else {
                // 記錄node的兄弟節(jié)點
                Node pointer1 = node.parent.left;
                // 如果他兄弟節(jié)點是紅色
                if (pointer1.color == Color.Red) {
                    pointer1.color = Color.Black;
                    node.parent.color = Color.Red;
                    rightRotate(node.parent);
                    pointer1 = node.parent.left;
                }
                if (pointer1.right.color == Color.Black && pointer1.left.color == Color.Black) {
                    pointer1.color = Color.Red;
                    node = node.parent;
                } else if (pointer1.left.color == Color.Black) {
                    pointer1.right.color = Color.Black;
                    pointer1.color = Color.Red;
                    leftRotate(pointer1);
                    pointer1 = node.parent.left;
                } else {
                    pointer1.color = node.parent.color;
                    node.parent.color = Color.Black;
                    pointer1.left.color = Color.Black;
                    rightRotate(node.parent);
                    node = this.root;
                }
            }

        }
        node.color = Color.Black;
    }

    private void innerWalk(Node node) {
        if (node != NULL) {
            innerWalk(node.left);
            System.out.println(node);
            innerWalk(node.right);
        }
    }

    /**
     * 中序遍歷
     */
    public void innerWalk() {
        this.innerWalk(this.root);
    }

    /**
     * 層次遍歷
     */
    public void print() {
        Queue<Node> queue = new LinkedList<>();
        queue.add(this.root);
        while (!queue.isEmpty()) {
            Node temp = queue.poll();
            System.out.println(temp);
            if (temp.left != NULL)
                queue.add(temp.left);
            if (temp.right != NULL)
                queue.add(temp.right);
        }
    }

    // 查找
    public Node search(int key) {
        Node pointer = this.root;
        while (pointer != NULL && pointer.key != key) {
            pointer = pointer.key < key ? pointer.right : pointer.left;
        }
        return pointer;
    }

}

6、演示

演示代碼:

public class Test01 {
  public static void main(String[] args) {
    int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
    RedBlackTree redBlackTree = new RedBlackTree();
    for (int i = 0; i < arr.length; i++) {
      redBlackTree.insert(new Node(arr[i]));
    }
    System.out.println("樹的高度: " + redBlackTree.root.height());
    System.out.println("左子樹的高度: " + redBlackTree.root.left.height());
    System.out.println("右子樹的高度: " + redBlackTree.root.right.height());
    System.out.println("層次遍歷");
    redBlackTree.print();
    // 要刪除節(jié)點
    Node node = redBlackTree.search(4);
    redBlackTree.delete(node);
    System.out.println("樹的高度: " + redBlackTree.root.height());
    System.out.println("左子樹的高度: " + redBlackTree.root.left.height());
    System.out.println("右子樹的高度: " + redBlackTree.root.right.height());
    System.out.println("層次遍歷");
    redBlackTree.print();
  }
}

到此這篇關于利用Java實現(xiàn)紅黑樹的文章就介紹到這了,更多相關Java實現(xiàn)紅黑樹內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java圖書管理系統(tǒng),課程設計必用(源碼+文檔)

    Java圖書管理系統(tǒng),課程設計必用(源碼+文檔)

    本系統(tǒng)采用Java,MySQL 作為系統(tǒng)數(shù)據(jù)庫,重點開發(fā)并實現(xiàn)了系統(tǒng)各個核心功能模塊,包括采編模塊、典藏模塊、基礎信息模塊、流通模塊、期刊模塊、查詢模塊、評論模塊、系統(tǒng)統(tǒng)計模塊以及幫助功能模塊
    2021-06-06
  • SpringCloud Gateway動態(tài)轉發(fā)后端服務實現(xiàn)過程講解

    SpringCloud Gateway動態(tài)轉發(fā)后端服務實現(xiàn)過程講解

    這篇文章主要介紹了SpringCloud Gateway動態(tài)轉發(fā)后端服務實現(xiàn)過程,簡單的路由轉發(fā)可以通過SpringCloudGateway的配置文件實現(xiàn),在一些業(yè)務場景種,會需要動態(tài)替換路由配置中的后端服務地址,單純靠配置文件無法滿足這種需求
    2023-03-03
  • 詳解Spring如何解析占位符

    詳解Spring如何解析占位符

    Spring一直支持將屬性定義到外部的屬性的文件中,并使用占占位符的形式為使用"${}"包裝的屬性名稱,為了使用屬性占位符,我們必須配置一個PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer實例,本文將介紹如何解析占位符
    2021-06-06
  • Elasticsearch Join字段類型簡單快速上手教程

    Elasticsearch Join字段類型簡單快速上手教程

    這篇文章主要為大家介紹了Elasticsearch Join字段類型簡單快速上手教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Java與Python之間使用jython工具類實現(xiàn)數(shù)據(jù)交互

    Java與Python之間使用jython工具類實現(xiàn)數(shù)據(jù)交互

    今天小編就為大家分享一篇關于Java與Python之間使用jython工具類實現(xiàn)數(shù)據(jù)交互,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 詳解Java集合中的基本數(shù)據(jù)結構

    詳解Java集合中的基本數(shù)據(jù)結構

    總有小伙伴讓我總結一下Java集合中的基本數(shù)據(jù)結構的相關知識,今天特地整理了本篇文章,文中有非常詳細的介紹,需要的朋友可以參考下
    2021-06-06
  • IDEA項目使用SpringBoot+MyBatis-Plus的方法

    IDEA項目使用SpringBoot+MyBatis-Plus的方法

    這篇文章主要介紹了IDEA項目使用SpringBoot+MyBatis-Plus的方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • shiro與spring?security用自定義異常處理401錯誤

    shiro與spring?security用自定義異常處理401錯誤

    這篇文章主要介紹了shiro與spring?security用自定義異常處理401錯誤,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 自定義JmsListenerContainerFactory時,containerFactory字段解讀

    自定義JmsListenerContainerFactory時,containerFactory字段解讀

    這篇文章主要介紹了自定義JmsListenerContainerFactory時,containerFactory字段解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • SpringBoot上傳文件到本服務器 目錄與jar包同級問題

    SpringBoot上傳文件到本服務器 目錄與jar包同級問題

    這篇文章主要介紹了SpringBoot上傳文件到本服務器 目錄與jar包同級問題,需要的朋友可以參考下
    2018-11-11

最新評論

通辽市| 黄龙县| 镇康县| 盐池县| 汉源县| 湖北省| 大田县| 辉南县| 盐山县| 阿克陶县| 特克斯县| 马尔康县| 凭祥市| 吉林省| 北辰区| 来宾市| 马鞍山市| 夏津县| 屏边| 内乡县| 米易县| 佛山市| 壤塘县| 宁晋县| 化州市| 来凤县| 乌苏市| 定边县| 会泽县| 乌恰县| 鄯善县| 手机| 谷城县| 巧家县| 连山| 朝阳市| 彭水| 密云县| 裕民县| 库车县| 会同县|