The red-black tree data structure 0043

---------------------- ------------------------- red-black tree ----

Red-black tree is still a binary search tree, and AVL, are added some restrictions on the basis of the binary search tree: the five specific restrictions are as follows:

1) Each node is either red or black

2) the root is black

3) each leaf node (the last empty node called a leaf node) is black

4) If a node is red, then both its children are black nodes

5) from any node to a leaf node, black node through the same

 

 

 

2-3 tree is an absolute balance of the tree: from the root node to a leaf node through an arbitrary number of nodes is the same, through the integration of newly added nodes and father nodes must be the first fusion (red-black tree is this principle , so the red-black tree newly added node must be red, that default constructor red) - split - a fusion to guarantee balanced.

 

Red node: on behalf of his father, and it is fused together , on behalf 2-3 tree node 3

 

 

 

 

 

 

 

Red-black tree "black balance" binary tree: i.e., red-black tree of Article 5 constraints, any node to the leaf node through the black is the same. Strictly speaking meaning, not a balanced binary tree children, the height difference that is about sub-tree is likely to be greater than 1. Red-black tree maximum height 2logn, so the time complexity is O (logn) of

 

Compared with the red-black tree AVL tree:

Find: red-black tree slightly slower than AVL tree

Add and delete: red-black tree faster than AVL tree

 

So if the stored data frequently add and delete: select the red-black tree

If the data stored base does not change, only for query: select AVL tree

 

RBTree code to achieve the following (not implemented deletion method):

package rbTree;

import java.util.ArrayList;

public class RBTree<K extends Comparable<K>, V> {

    private static final boolean RED = true;
    private static final boolean BLACK = false;

    private class Node{
        public K key;
        public V value;
        public Node left, right;
        public boolean color;

        public Node(K key, V value){
            this.key = key;
            this.value = value;
            left = null;
            right = null;
            color = RED;
        }
    }

    private Node root;
    private int size;

    public RBTree(){
        root = null;
        size = 0;
    }

    public int getSize(){
        return size;
    }

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

    // 判断节点node的颜色
   
private boolean isRed(Node node){
        if(node == null)
            return BLACK;
        return node.color;
    }

    //   node                     x
    //  /   \    
左旋转         /  \
    // T1   x   --------->   node   T3
    //     / \              /   \
    //    T2 T3            T1   T2
   
private Node leftRotate(Node node){

        Node x = node.right;

        // 左旋转
       
node.right = x.left;
        x.left = node;

        x.color = node.color;
        node.color = RED;

        return x;
    }

    //     node                   x
    //    /   \    
右旋转       /  \
    //   x    T2   ------->   y   node
    //  / \                       /  \
    // y  T1                     T1  T2
   
private Node rightRotate(Node node){

        Node x = node.left;

        // 右旋转
       
node.left = x.right;
        x.right = node;

        x.color = node.color;
        node.color = RED;

        return x;
    }

    // 颜色翻转
   
private void flipColors(Node node){

        node.color = RED;
        node.left.color = BLACK;
        node.right.color = BLACK;
    }

    // 向红黑树中添加新的元素(key, value)
   
public void add(K key, V value){
        root = add(root, key, value);
        root.color = BLACK; // 最终根节点为黑色节点
   
}

    // 向以node为根的红黑树中插入元素(key, value),递归算法
   
// 返回插入新节点后红黑树的根
   
private Node add(Node node, K key, V value){

        if(node == null){
            size ++;
            return new Node(key, value); // 默认插入红色节点
       
}

        if(key.compareTo(node.key) < 0)
            node.left = add(node.left, key, value);
        else if(key.compareTo(node.key) > 0)
            node.right = add(node.right, key, value);
        else // key.compareTo(node.key) == 0
           
node.value = value;

        if (isRed(node.right) && !isRed(node.left))
            node = leftRotate(node);

        if (isRed(node.left) && isRed(node.left.left))
            node = rightRotate(node);

        if (isRed(node.left) && isRed(node.right))
            flipColors(node);

        return node;
    }

    // 返回以node为根节点的二分搜索树中,key所在的节点
   
private Node getNode(Node node, K key){

        if(node == null)
            return null;

        if(key.equals(node.key))
            return node;
        else if(key.compareTo(node.key) < 0)
            return getNode(node.left, key);
        else // if(key.compareTo(node.key) > 0)
            
return getNode(node.right, key);
    }

    public boolean contains(K key){
        return getNode(root, key) != null;
    }

    public V get(K key){

        Node node = getNode(root, key);
        return node == null ? null : node.value;
    }

    public void set(K key, V newValue){
        Node node = getNode(root, key);
        if(node == null)
            throw new IllegalArgumentException(key + " doesn't exist!");

        node.value = newValue;
    }

    // 返回以node为根的二分搜索树的最小值所在的节点
   
private Node minimum(Node node){
        if(node.left == null)
            return node;
        return minimum(node.left);
    }
}

 

 

总结:

1)  二分搜索树适合处理完全随机的数据;不适用于处理近乎有序的数据,这样会退化为链表

2)  AVL与红黑树相比,AVL更适合处理查询数据  

3)  红黑树牺牲了平衡性,即有可能是不平衡的,但一定是“绝对黑平衡”的,2logn的高度,统计性能更优(即更适合增删改查的综合性操作)

Guess you like

Origin www.cnblogs.com/xiao1572662/p/12129395.html