红黑树-Java实现

来源:互联网 发布:世界国家城市数据库 编辑:程序博客网 时间:2024/06/06 05:27

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

github : https://github.com/renxue/algorithm-for-java/blob/master/src/com/fengchengpeng/btree/RedBlackTree.java

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);    }