1. 程式人生 > >一棵沒實現刪除的紅黑樹

一棵沒實現刪除的紅黑樹

除了刪除節點之外,把紅黑樹基本上看完了,順手用java實現了一棵紅黑樹
import javax.swing.*;

/**
 * Created by eminem on 16-11-15.
 */
public class brTree {
    private Node root;
    private static final boolean RED = true;
    private static final boolean BLACK = false;

    private class Node {
        String key;
        int value;
        Node left, right;
        int N;
        boolean color;

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


    }


    private boolean isRed(Node x) {

        //空連結那麼他就是black
        if (x == null) return false;

        return x.color == RED;
    }


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


    Node rotateLfet(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 = size(h.left) + size(h.right) + 1;
        return x;
    }

    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 = size(h.left) + size(h.right) + 1;
        return x;
    }


    private void flipColors(Node shit) {
        shit.color = RED;
        shit.left.color = BLACK;
        shit.right.color = BLACK;
    }


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

    private Node put(Node shit, String key, int value) {
        if (shit == null) {
            return new Node(key, value, 1, RED);
        }
        int cmp = key.compareTo(shit.key);
        if (cmp < 0) shit.left = put(shit.left, key, value);
        else if (cmp > 0) shit.right = put(shit.right, key, value);
        else shit.value = value;

        if (isRed(shit.right) && !isRed(shit.left)) shit = rotateLfet(shit);
        if (isRed(shit.left) && isRed(shit.left.left)) shit = rotateRight(shit);
        if (isRed(shit.left) && isRed(shit.right)) flipColors(shit);

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


    public int get(String key) {
        return get(root, key);
    }

    private Integer get(Node x, String key) {
        if (x == null) return null;
        int cmp = key.compareTo(x.key);
        if (cmp > 0) {
            return get(x.right, key);
        } else if (cmp < 0) {
            return get(x.left, key);
        } else {
            return x.value;
        }
    }


    public String min() {
        return min(root).key;
    }

    private Node min(Node x) {
        if (x.left == null) return x;
        return min(x.left);
    }

    public String max() {
        return max(root).key;
    }

    private Node max(Node x) {
        if (x.right == null) return x;
        return max(x.right);
    }


    public String floor(String key) {
        Node x = floor(root, key);
        if (x == null) return null;
        return x.key;
    }

    private Node floor(Node x, String key) {
        if (x == null) return null;
        int cmp = key.compareTo(x.key);
        if (cmp == 0) return x;
        if (cmp < 0) return floor(x.left, key);
        Node t = floor(x.right, key);
        if (t != null) return t;
        else return x;
    }

    //找到排名為k的鍵
    public String select(int k) {
        return select(root, k).key;
    }

    private Node select(Node x, int k) {
        if (x == null) return null;
        int t = size(x.left);
        if (t > k) return select(x.left, k);
        else if (t < k) return select(x.right, k - t - 1);
        else return x;
    }

    public int rank(String key) {
        return rank(root, key);
    }

    private Integer rank(Node x, String key) {
        //始終沒命中,已到樹的末尾
        if (x == null) return 0;
        int cmp = key.compareTo(x.key);
        if (cmp < 0) return rank(x.left, key);
        else if (cmp > 0) return 1 + size(x.left) + rank(x.right ,key);
        else return size(x.left);
    }
}
//md這些演算法的發明者也是吊,怎麼想出來的啊這東西
測試類如下:
/**
 * Created by eminem on 16-11-17.
 */
public class brTestt {
    public static void main(String[] args) {
        brTree fuck=new brTree();
        fuck.put("a",1);
        fuck.put("b",2);
        fuck.put("c",3);
        fuck.put("d",4);
        fuck.put("e",5);
        fuck.put("f",6);
        fuck.put("g",7);
        fuck.put("h",8);
        System.out.println(fuck.select(7));
        System.out.println(fuck.get("a"));
        System.out.println(fuck.floor("o"));
        
    }
}