zoukankan      html  css  js  c++  java
  • 红黑树实现

    package algs4;
    
    /**
     * Created by blank on 2015-10-15 上午10:08.
     */
    public class RedBlackTree {
    
        private Node root;
    
        public void put(Key key, Value value) {
            root = put(root, key, value);
            root.color = BLACK;
        }
    
        private Node put(Node h, Key key, Value value) {
            if (h == null) {
                return new Node(key, value, 1, RED);
            }
            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.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;
        }
    
        void flipColors(Node h) {
            h.color = RED;
            h.left.color = BLACK;
            h.right.color = BLACK;
        }
    
        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;
        }
    
        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;
        }
    
        int size(Node x) {
            return x.N;
        }
    
        static class Key implements Comparable<Key> {
            int val;
    
            @Override
            public int compareTo(Key o) {
                return val - o.val;
            }
        }
    
        private boolean isRed(Node x) {
            return x != null && x.color;
        }
    
        static class Value {
            String val;
        }
    
        public static final boolean RED = true;
    
        public static final boolean BLACK = false;
        
        static 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;
                N = n;
                this.color = color;
            }
        }
    }
  • 相关阅读:
    Spring AOP capabilities and goals
    java Design Patterns
    CDI Features
    connector for python实验
    spring ref &history&design philosophy
    LDAP & Implementation
    RESTful Levels & HATEOAS
    运维管理利器系列--ipmitool
    CentOS8.2同步阿里云Zabbix镜像到本地,本地搭建Zabbix仓库
    CentOS8.2同步阿里云Ceph镜像到本地,本地搭建ceph仓库
  • 原文地址:https://www.cnblogs.com/aboutblank/p/4881828.html
Copyright © 2011-2022 走看看