算法 3.3节 红黑树和B树

  • 红黑树的查询等操作的实现均与二叉树一致,只有插入与删除不同
public class RBT {
    private Node root;
    private static boolean RED = true;
    private static boolean BLACK = false;

    private class Node {
        private int key;
        private int value;
        private Node left;
        private Node right;
        private int count;
        boolean color;

        public Node(int key, int value, int count, boolean color) {
            this.key = key;
            this.value = value;
            this.count = count;
            this.color = color;
        }

    }

    private boolean isRed(Node x) {
        if (x == null) return BLACK; //不要忘了空节点
        return x.color == RED;
    }

    private Node rotateLeft(Node x) {
        Node t = x.right;
        x.right = t.left;
        t.left = x;
        t.color = x.color; //别忘了改颜色
        x.color = RED;
        t.count = x.count; // t取代了原来的x,所以不用重新计数
        x.count = 1 + size(x.left) + size(x.right);
        return t;
    }

    private Node rotateRight(Node x) {
        Node t = x.left;
        x.left = t.right;
        t.right = x;
        t.color = x.color;
        x.color = RED;
        t.count = x.count;
        x.count = 1 + size(x.left) + size(x.right);
        return t;
    }

    private void flip(Node x) {
        x.color = RED;
        x.left.color = BLACK;
        x.right.color = BLACK;
        ;[;/4]
    }

    public RBT() {
        root.count = 1;
        root.left = null;
        root.right = null;
        root.color = false;
    }

    public int size() {
        return size(root);
    }

    private int size(Node x) {
        return x == null ? 0 : x.count;
    }

    public int get(int key) {
        Node x = get(root, key);
        return x == null ? -1 : x.value;
    }

    private Node get(Node x, int key) {
        if (x == null) return null;
        else if (x.key == key) return x;
        else if (x.key > key) return get(x.left, key);
        else if (x.key < key) return get(x.right, key);
    }

    public void insert(int key, int value) {
        root = insert(key, value, root);
        root.color = BLACK;
    }

    private Node insert(int key, int value, Node x) {
        if (x == null) return new Node(key, value, 1, RED);
        if (x.key > key) x.left = insert(key, value, x.left);
        if (x.key < key) x.right = insert(key, value, x.right);

        if (isRed(x.right) && !isRed(x.left)) x = rotateLeft(x);
        if (isRed(x.left) && isRed(x.left.left)) x = rotateRight(x);
        if (x.right.color && x.left.color) flip(x);

        x.count = 1 + size(x.left) + size(x.right);
        return x;
    }

    public void deleteMin() {

    }

    private Node deleteMin(Node x) {

    }

    public void delete(int key) {

    }

    private Node delete(Node x, int key) {

    }

    public boolean isBalanced() {
        int black = 0;
        Node x = root;
        while (x.left != null) {
            if (!isRed(x)) black++;
            x = x.left;
        }
        return boolean isBalanced (root, black);
    }

    private boolean isBalanced(Node x, int black) {
        if (x == null) return black == 0;
        if (!isRed(x)) black--;
        return isBalanced(x.left,black) && isBalanced(x.right, black);
    }


}

猜你喜欢

转载自blog.csdn.net/winter_wu_1998/article/details/79556294