All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.st.bst.lxx.LXXRedBlackBST Maven / Gradle / Ivy

There is a newer version: 2.0
Show newest version
package shz.st.bst.lxx;

import shz.queue.LLinkedQueue;
import shz.st.bst.RedBlackBST;

/**
 * 健为K类型的红黑树
 */
public abstract class LXXRedBlackBST> extends RedBlackBST {
    protected static abstract class Node> extends RedBlackBST.Node {
        protected K key;

        protected Node(K key, boolean red) {
            super(red);
            this.key = key;
        }
    }

    protected LXXRedBlackBST(Node root) {
        super(root);
    }

    @SuppressWarnings("unchecked")
    protected final K key(RedBlackBST.Node h) {
        return ((Node) h).key;
    }

    /**
     * 返回不大于hi的节点数量
     */
    public final int sizeLe(K hi) {
        if (hi == null) throw new NullPointerException();
        return sizeLe(root(), hi);
    }

    protected final int sizeLe(Node h, K hi) {
        int res = 0;
        while (h != null) {
            if (hi.compareTo(h.key) < 0) h = h.left();
            else {
                res += 1 + size(h.left());
                h = h.right();
            }
        }
        return res;
    }

    /**
     * 返回不小于lo的节点数量
     */
    public final int sizeGe(K lo) {
        if (lo == null) throw new NullPointerException();
        return sizeGe(root(), lo);
    }

    protected final int sizeGe(Node h, K lo) {
        int res = 0;
        while (h != null) {
            if (lo.compareTo(h.key) > 0) h = h.right();
            else {
                res += 1 + size(h.right());
                h = h.left();
            }
        }
        return res;
    }

    /**
     * 返回在区间[lo,hi]的节点数量
     */
    public final int size(K lo, K hi) {
        if (lo == null || hi == null) throw new NullPointerException();
        if (lo.compareTo(hi) > 0) throw new IllegalArgumentException();
        return size(root(), lo, hi);
    }

    protected final int size(Node h, K lo, K hi) {
        int res = 0;
        while (h != null) {
            int cmp = lo.compareTo(h.key);
            if (cmp > 0) h = h.right();
            else if (cmp == 0) {
                res += 1 + sizeLe(h.right(), hi);
                break;
            } else if (hi.compareTo(h.key) < 0) h = h.left();
            else {
                res += sizeGe(h.left(), lo) + 1 + sizeLe(h.right(), hi);
                break;
            }
        }
        return res;
    }

    public final K floor(K key) {
        if (key == null) throw new NullPointerException();
        Node h = floor(root(), key);
        return h == null ? null : h.key;
    }

    protected final Node floor(Node h, K key) {
        Node res = null;
        while (h != null) {
            int cmp = key.compareTo(h.key);
            if (cmp == 0) return h;
            if (cmp < 0) h = h.left();
            else {
                res = h;
                h = h.right();
            }
        }
        return res;
    }

    public final K ceil(K key) {
        if (key == null) throw new NullPointerException();
        Node h = ceil(root(), key);
        return h == null ? null : h.key;
    }

    protected final Node ceil(Node h, K key) {
        Node res = null;
        while (h != null) {
            int cmp = key.compareTo(h.key);
            if (cmp == 0) return h;
            if (cmp > 0) h = h.right();
            else {
                res = h;
                h = h.left();
            }
        }
        return res;
    }

    /**
     * 查找所有键(中序遍历)
     */
    public final Iterable keys() {
        LLinkedQueue queue = LLinkedQueue.of();
        keys(root(), queue);
        return queue;
    }

    protected final void keys(Node h, LLinkedQueue queue) {
        if (h == null) return;
        keys(h.left(), queue);
        queue.offer(h.key);
        keys(h.right(), queue);
    }

    /**
     * 查找所有不大于hi的键
     */
    public final Iterable keysLe(K hi) {
        if (hi == null) throw new NullPointerException();
        LLinkedQueue queue = LLinkedQueue.of();
        keysLe(root(), queue, hi);
        return queue;
    }

    protected final void keysLe(Node h, LLinkedQueue queue, K hi) {
        if (h == null) return;
        if (hi.compareTo(h.key) < 0) keysLe(h.left(), queue, hi);
        else {
            keys(h.left(), queue);
            queue.offer(h.key);
            keysLe(h.right(), queue, hi);
        }
    }

    /**
     * 查找所有不小于lo的键
     */
    public final Iterable keysGe(K lo) {
        if (lo == null) throw new NullPointerException();
        LLinkedQueue queue = LLinkedQueue.of();
        keysGe(root(), queue, lo);
        return queue;
    }

    protected final void keysGe(Node h, LLinkedQueue queue, K lo) {
        if (h == null) return;
        if (lo.compareTo(h.key) > 0) keysGe(h.right(), queue, lo);
        else {
            keysGe(h.left(), queue, lo);
            queue.offer(h.key);
            keys(h.right(), queue);
        }
    }

    /**
     * 查找所有在区间[lo,hi]的键
     */
    public final Iterable keys(K lo, K hi) {
        if (lo == null || hi == null) throw new NullPointerException();
        if (lo.compareTo(hi) > 0) throw new IllegalArgumentException();
        LLinkedQueue queue = LLinkedQueue.of();
        keys(root(), queue, lo, hi);
        return queue;
    }

    protected final void keys(Node h, LLinkedQueue queue, K lo, K hi) {
        if (h == null) return;
        int cmp = lo.compareTo(h.key);
        if (cmp > 0) {
            keys(h.right(), queue, lo, hi);
        } else if (cmp == 0) {
            queue.offer(h.key);
            keysLe(h.right(), queue, hi);
        } else {
            if (hi.compareTo(h.key) >= 0) {
                keysGe(h.left(), queue, lo);
                queue.offer(h.key);
                keysLe(h.right(), queue, hi);
            } else keys(h.left(), queue, lo, hi);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy