亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

java紅黑樹實現代碼怎么寫

小億
83
2024-03-13 15:01:04
欄目: 編程語言

以下是一個簡單的紅黑樹實現代碼示例:

class Node {
    int data;
    Node left, right, parent;
    boolean color;  // true表示紅色,false表示黑色
    
    public Node(int data) {
        this.data = data;
        this.color = true;  // 新插入的節點默認為紅色
        this.left = this.right = this.parent = null;
    }
}

public class RedBlackTree {
    private Node root;
    
    // 紅黑樹左旋轉
    private void leftRotate(Node x) {
        Node y = x.right;
        x.right = y.left;
        if (y.left != null) {
            y.left.parent = x;
        }
        y.parent = x.parent;
        if (x.parent == null) {
            root = y;
        } else if (x == x.parent.left) {
            x.parent.left = y;
        } else {
            x.parent.right = y;
        }
        y.left = x;
        x.parent = y;
    }
    
    // 紅黑樹右旋轉
    private void rightRotate(Node x) {
        Node y = x.left;
        x.left = y.right;
        if (y.right != null) {
            y.right.parent = x;
        }
        y.parent = x.parent;
        if (x.parent == null) {
            root = y;
        } else if (x == x.parent.right) {
            x.parent.right = y;
        } else {
            x.parent.left = y;
        }
        y.right = x;
        x.parent = y;
    }
    
    // 紅黑樹插入
    public void insert(int data) {
        Node newNode = new Node(data);
        Node parent = null;
        Node current = root;
        
        while (current != null) {
            parent = current;
            if (data < current.data) {
                current = current.left;
            } else {
                current = current.right;
            }
        }
        
        newNode.parent = parent;
        if (parent == null) {
            root = newNode;
        } else if (data < parent.data) {
            parent.left = newNode;
        } else {
            parent.right = newNode;
        }
        
        insertFixUp(newNode);
    }
    
    // 紅黑樹插入修正
    private void insertFixUp(Node x) {
        while (x != root && x.parent.color == true) {
            if (x.parent == x.parent.parent.left) {
                Node y = x.parent.parent.right;
                if (y != null && y.color == true) {
                    x.parent.color = false;
                    y.color = false;
                    x.parent.parent.color = true;
                    x = x.parent.parent;
                } else {
                    if (x == x.parent.right) {
                        x = x.parent;
                        leftRotate(x);
                    }
                    x.parent.color = false;
                    x.parent.parent.color = true;
                    rightRotate(x.parent.parent);
                }
            } else {
                Node y = x.parent.parent.left;
                if (y != null && y.color == true) {
                    x.parent.color = false;
                    y.color = false;
                    x.parent.parent.color = true;
                    x = x.parent.parent;
                } else {
                    if (x == x.parent.left) {
                        x = x.parent;
                        rightRotate(x);
                    }
                    x.parent.color = false;
                    x.parent.parent.color = true;
                    leftRotate(x.parent.parent);
                }
            }
        }
        root.color = false;
    }
    
    // 中序遍歷打印紅黑樹
    public void inorderTraversal(Node node) {
        if (node != null) {
            inorderTraversal(node.left);
            System.out.print(node.data + " ");
            inorderTraversal(node.right);
        }
    }
    
    public static void main(String[] args) {
        RedBlackTree rbTree = new RedBlackTree();
        
        rbTree.insert(7);
        rbTree.insert(3);
        rbTree.insert(18);
        rbTree.insert(10);
        rbTree.insert(22);
        rbTree.insert(8);
        rbTree.insert(11);
        
        rbTree.inorderTraversal(rbTree.root);
    }
}

以上是一個簡單的紅黑樹實現,包含了插入和修正操作。您可以根據需要進行進一步擴展和優化。

0
大连市| 阳山县| 桑日县| 玛曲县| 乐安县| 龙井市| 大足县| 洛阳市| 德保县| 华宁县| 江陵县| 孟州市| 库车县| 稻城县| 涟水县| 洛扎县| 青阳县| 锦屏县| 赤城县| 丹江口市| 庐江县| 台中市| 邳州市| 临高县| 临漳县| 东明县| 周宁县| 湘乡市| 安远县| 平和县| 海伦市| 涿州市| 中卫市| 广南县| 珠海市| 鹤壁市| 静乐县| 怀柔区| 罗平县| 富顺县| 谷城县|