红黑树-Java实现

该实现是基于Robert Sedgewick Kevin Wayne的红宝典《算法》3.3.2节:红黑二叉查找树

public class RedBlackTree<Key extends Comparable<Key>, Value> {
    private Node root;
    private static final boolean BLACK = true;
    private static final boolean RED = false;
    private class Node {
        Key key;
        Value value;
        Node left, right;
        int N;
        boolean color;

        public Node(Key key, Value value, int n, boolean color) {
            this.key = key;
            this.value = value;
            this.N = n;
            this.color = color;
        }
    }

    private boolean isRed(Node n) {
        if (n == null) return false;
        return n.color == RED;
    }

    public int size(Node x) {
        if (x == null)
            return 0;
        else
            return x.N;
    }

    public void flipColors(Node x) {
        x.color = RED;
        x.left.color = BLACK;
        x.right.color = BLACK;
    }

    public void put(Key key, Value val) {
        root = put(root, key, val);
        root.color = BLACK;
    }
    public Node put(Node h, Key key, Value val) {
        if (h == null) {
            return new Node(key, val, 1, RED);
        }

        int cmp = key.compareTo(h.key);
        if (cmp < 0) h.left = put(h.left, key, val);
        else if (cmp > 0) h.right = put(h.right, key, val);
        else h.value = val;

        if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h);
        if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h);
        if (isRed(h.left) && isRed(h.right)) flipColors(h);

        h.N = size(h.left) + size(h.right) + 1;
        return h;
    }

    protected Node rotateLeft(Node h) {
        Node x = h.right;
        h.right = x.left;
        x.left = h;
        x.color = h.color;
        h.color = RED;
        x.N = h.N;
        h.N = 1 + size(h.left)
                + size(h.right);
        return x;
    }

    protected Node rotateRight(Node h) {
        Node x = h.left;
        h.left = x.right;
        x.right = h;
        x.color = h.color;
        h.color = RED;
        x.N = h.N;
        h.N = 1 + size(h.left)
                + size(h.right);
        return x;
    }

    public void iterator(Node node) {
        if (node == null)
            System.out.println("tree is null");

        if (node.left != null)
            iterator(node.left);

        if (node.color)
            System.out.print(node.key + ":BLACK   ");
        else
            System.out.print(node.key + ":RED   ");

        if (node.right != null)
            iterator(node.right);
    }

    public static  void main(String[] args) {
        RedBlackTree<String, String> redBlackTree = new RedBlackTree<>();

        System.out.println(redBlackTree.size(redBlackTree.root));
        redBlackTree.put("S", "s");
        redBlackTree.put("E", "e");
        redBlackTree.put("A", "a");
        redBlackTree.put("R", "r");
        redBlackTree.put("C", "c");
        redBlackTree.put("H", "h");
        redBlackTree.put("X", "x");
        redBlackTree.put("M", "m");
        redBlackTree.put("P", "p");
        redBlackTree.put("L", "l");
        System.out.println(redBlackTree.size(redBlackTree.root));
        redBlackTree.iterator(redBlackTree.root);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值