NoRecursiveBST

简书 賈小強
转载请注明原创出处,谢谢!

package com.lab1.test3;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class RedBlackBST<Key extends Comparable<Key>, Value> {
    private Node root;
    private static final boolean RED = true;
    private static final boolean BLACK = false;

    private class Node {
        Key key;
        Value value;
        Node left, right;
        boolean color;
        int size;

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

    }

    private void deleteMin() {
        root = deleteMin(root);
    }

    private Node deleteMin(Node x) {
        if (x.left == null) {
            return x.right;
        }
        x.left = deleteMin(x.left);
        x.size = size(x.left) + size(x.right) + 1;
        return x;
    }

    private Key min() {
        return min(root).key;
    }

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

    private int height() {
        return height(root);
    }

    private int height(Node x) {
        if (x == null) {
            return -1;
        }
        return Math.max(height(x.left), height(x.right)) + 1;
    }

    private void delete(Key key) {
        root = delete(root, key);
    }

    private Node delete(Node x, Key key) {
        if (x == null) {
            return null;
        }
        int cmp = key.compareTo(x.key);
        if (cmp < 0) {
            x.left = delete(x.left, key);
        } else if (cmp > 0) {
            x.right = delete(x.right, key);
        } else {
            if (x.left == null) {
                return x.right;
            }
            if (x.right == null) {
                return x.left;
            }
            Node t = x;
            x = min(t.right);
            x.right = deleteMin(t.right);
            x.left = t.left;
        }
        x.size = size(x.left) + size(x.right) + 1;
        return x;
    }

    private Iterable<Key> keys() {
        Queue<Key> queue = new LinkedList<>();
        Stack<Node> stack = new Stack<>();
        Node x = root;
        while (x != null || !stack.isEmpty()) {
            if (x != null) {
                stack.push(x);
                x = x.left;
            } else {
                x = stack.pop();
                queue.add(x.key);
                x = x.right;
            }
        }
        return queue;
    }

    private Value get(Key key) {
        return get(root, key);
    }

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

    private boolean contains(Key key) {
        return get(key) != null;
    }

    private boolean isEmpty() {
        return size() == 0;
    }

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

    private void put(Key key, Value value) {
        root = put(root, key, value);
    }

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

        if (!isRed(h.left) && isRed(h.right)) {
            h=rotateLeft(h);
        }
        if(isRed(h.left)&&isRed(h.left.left)){
            h=rotateRight(h);
        }
        if(isRed(h.left)&&isRed(h.right)){
            flipColors(h);
        }
        h.size = size(h.left) + size(h.right) + 1;
        return h;
    }

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

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

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

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

    private int size(Node x) {
        if (x == null) {
            return 0;
        }
        return x.size;
    }

    public static void main(String[] args) {
        RedBlackBST<String, Integer> st = new RedBlackBST<>();
        String test = "S E A R C H E X A M P L E";
        String[] keys = test.split(" ");
        for (int i = 0; i < keys.length; i++) {
            st.put(keys[i], i);
        }
        System.out.println("height      = " + st.height());
        System.out.println("size        = " + st.size());
        System.out.println("isEmpty     = " + st.isEmpty());
        System.out.println("min         = " + st.min());
        st.deleteMin();
        System.out.println("min         = " + st.min());
        System.out.println("contains    = " + st.contains("S"));
        st.delete("S");
        System.out.println("contains    = " + st.contains("S"));
        for (String key : st.keys()) {
            System.out.println(key + " " + st.get(key));
        }
    }
}

输出

height      = 3
size        = 10
isEmpty     = false
min         = A
min         = C
contains    = true
contains    = false
C 4
E 12
H 5
L 11
M 9
P 10
R 3
X 7

Happy learning !!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,593评论 25 708
  • 简书 賈小強转载请注明原创出处,谢谢! Servlet是一种允许响应请求的Java类。虽然Servlet可以响应任...
    賈小強阅读 10,643评论 1 44
  • ●传统记录VS视觉记录 先来看两组对比图片 请对比以上2组图片,思考: 1.你们更喜欢A组还是更喜欢B组?2.闭上...
    诗云Monica阅读 878评论 3 9
  • 一般来说当生活出现低谷时,常常意味着你的生活和心绪出现了混乱。最近因为复习考试,我的生活出现了一次低谷,通过观察和...
    欢喜台阅读 457评论 0 0
  • [cp]我希望你把我当作孩子捧在手心里 ­ 给我宠爱 ­ 我希望你带着宠溺的眼神 揉乱我的碎发 ­ 陪我一跳一跳的...
    栾城安阅读 416评论 0 0