📜  左斜红黑树(插入)

📅  最后修改于: 2021-05-24 21:43:39             🧑  作者: Mango

先决条件:红色–黑色树。

左倾的Red Black Tree或(LLRB)是Red black tree的一种变体,它比Red black tree本身更容易实现,并保证在O(logn)时间内进行所有搜索,删除和插入操作。

哪些节点是红色的,哪些节点是黑色的?
具有两个传入边缘的节点为红色。
具有单个进入边缘的节点的颜色为黑色。

LLRB的特点
1.根节点的颜色始终为黑色。
2.插入的每个新节点始终为红色。
3.节点的每个NULL子级在颜色上均被视为黑色。
例如:树中只有40个。

root
       |
       40 <-- as 40 is the root so it 
      /  \    is also Black in color. 
    NULL  NULL <-- Black in color.

4.不应有一个具有RIGHT RED子项和LEFT BLACK子项(或NULL子项,因为所有NULL均为BLACK)的节点(如果存在)左旋转节点,并交换当前节点及其子项的颜色,以保持规则2的一致性,即新节点的颜色必须为红色。

CASE 1.
   root                        root     
     |                          ||      
     40      LeftRotate(40)     50     
    /  \\        --->          /  \    
  NULL  50                   40   NULL    

                               root              
                                |               
         ColorSwap(50, 40)      50 
           --->               //  \              
                             40  NULL                     

5.不应有具有LEFT RED子代和LEFT RED孙子代的节点(如果存在)Right旋转节点并在该节点和RIGHT子代之间交换颜色,以遵循规则2。

CASE 2.
   root                         root      
    |                            ||        
    40        RightRotate(40)    20        
   //  \         --->          //   \      
  20    50                    10    40           
 //                                   \      
10                                     50

                          root
                           |
    ColorSwap(20, 40)      20
      --->                // \\
                         10   40
                               \
                               50
 

6.如果存在,则不应有一个节点具有LEFT RED子节点和RIGHT RED子节点。反转所有节点的颜色,即current_node,LEFT子节点和RIGHT子节点。

CASE 3.
   root                           root      
    |       !color(20, 10, 30)     ||       
    20            --->             20                
  //  \\                          /  \  
 10    30                        10  30 

                                     root
        As the root is always black    |                     
                --->                  20   
                                     /  \
                                   10   30   

我们为什么要遵循上述规则?因为通过遵循上述特征/规则,我们能够模拟所有红黑树的属性,而无需关心其复杂的实现。
例子:

Insert the following data into LEFT LEANING RED-BLACK
TREE and display the inorder traversal of tree.
Input : 10 20 30 40 50 25
Output : 10 20 30 40 50 25
        root
         |
         40
       //  \
      20    50
     /  \
   10    30
        //
       25 

方法 :
LLRB中的插入就像插入二叉搜索树中一样。区别在于,将节点插入树后,我们将把步骤追溯到根,并尝试对LLRB强制执行上述规则。
在进行上述旋转和交换颜色时,我们的根可能会变成红色,所以我们也可能会发生这种情况。我们必须确保我们的根始终保持黑色。
LLRB插入示例。

C
// C program to implement insert operation
// in Red Black Tree.
#include 
#include 
#include 
  
typedef struct node
{
    struct node *left, *right;
    int data;
  
    // red ==> true, black ==> false
    bool color; 
} node;
  
  
  
// utility function to create a node.
node* createNode(int data, bool color)
{
    node *myNode = (node *) malloc(sizeof(node));
    myNode -> left = myNode -> right = NULL;
    myNode -> data = data;
  
    // New Node which is created is
    // always red in color.
    myNode -> color = true;
    return myNode;
}
  
// utility function to rotate node anticlockwise.
node* rotateLeft(node* myNode)
{
    printf("left rotation!!\n");
    node *child = myNode -> right;
    node *childLeft = child -> left;
  
    child -> left = myNode;
    myNode -> right = childLeft;
  
    return child;
}
  
// utility function to rotate node clockwise.
node* rotateRight(node* myNode)
{
    printf("right rotation\n");
    node *child = myNode -> left;
    node *childRight =  child -> right;
  
    child -> right = myNode;
    myNode -> left = childRight;
      
    return child; 
}
  
// utility function to check whether
// node is red in color or not.
int isRed(node *myNode)
{
    if (myNode == NULL) 
       return 0;
    return (myNode -> color == true);
}
  
// utility function to swap color of two 
// nodes.
void swapColors(node *node1, node *node2)
{
    bool temp = node1 -> color;
    node1 -> color = node2 -> color;
    node2 -> color = temp;
}
  
// insertion into Left Leaning Red Black Tree.
node* insert(node* myNode, int data)
{
    // Normal insertion code for any Binary
    // Search tree. 
    if (myNode == NULL) 
        return createNode(data, false);    
  
    if (data < myNode -> data) 
        myNode -> left = insert(myNode -> left, data);
  
    else if (data > myNode -> data) 
        myNode -> right = insert(myNode -> right, data);
  
    else    
        return myNode;
  
      
    // case 1.
    // when right child is Red but left child is
    // Black or doesn't exist.
    if (isRed(myNode -> right) && !isRed(myNode -> left))
    {
        // left rotate the node to make it into
        // valid structure.
        myNode = rotateLeft(myNode);
  
        // swap the colors as the child node 
        // should always be red
        swapColors(myNode, myNode -> left);
          
    }
      
    // case 2
    // when left child as well as left grand child in Red
    if (isRed(myNode -> left) && isRed(myNode -> left -> left))
    {   
        // right rotate the current node to make 
        // it into a valid structure.
        myNode = rotateRight(myNode);
        swapColors(myNode, myNode -> right);
    }
      
      
    // case 3
    // when both left and right child are Red in color.
    if (isRed(myNode -> left) && isRed(myNode -> right))
    {
        // invert the color of node as well 
        // it's left and right child.
        myNode -> color = !myNode -> color;
  
        // change the color to black.
        myNode -> left -> color = false;
        myNode -> right -> color = false; 
    }
  
    return myNode;
}
  
// Inorder traversal
void inorder(node *node)
{
    if (node)
    {
        inorder(node -> left);
        printf("%d ", node -> data);
        inorder(node -> right); 
    }
}
  
// Driver function
int main()
{
    node *root = NULL;
    /* LLRB tree made after all insertions are made.
              
    1. Nodes which have double INCOMING edge means
       that they are RED in color.
    2. Nodes which have single INCOMING edge means
       that they are BLACK in color.
      
        root
         |
         40
       //  \
      20    50
     /  \
   10    30
        //
       25    */
  
    root = insert(root, 10);
    // to make sure that root remains
    // black is color
    root -> color = false;
      
    root = insert(root, 20);
    root -> color = false;
      
    root = insert(root, 30);
    root -> color = false;
     
    root = insert(root, 40);
    root -> color = false;
      
    root = insert(root, 50);
    root -> color = false; 
      
    root = insert(root, 25);
    root -> color = false; 
  
    // display the tree through inorder traversal.
    inorder(root);
      
    return 0;
}


Java
// Java program to implement insert operation
// in Red Black Tree.
  
class node
{
    node left, right;
    int data;
  
    // red ==> true, black ==> false
    boolean color; 
      
    node(int data)
    {
        this.data = data;
        left = null;
        right = null;
          
        // New Node which is created is
        // always red in color.
        color = true;
    }
}
  
public class LLRBTREE {
  
    private static node root = null;
      
    // utility function to rotate node anticlockwise.
    node rotateLeft(node myNode)
    {
        System.out.printf("left rotation!!\n");
        node child = myNode.right;
        node childLeft = child.left;
  
        child.left = myNode;
        myNode.right = childLeft;
  
        return child;
    }
  
    // utility function to rotate node clockwise.
    node rotateRight(node myNode)
    {
        System.out.printf("right rotation\n");
        node child = myNode.left;
        node childRight = child.right;
  
        child.right = myNode;
        myNode.left = childRight;
  
        return child; 
    }
  
    // utility function to check whether
    // node is red in color or not.
    boolean isRed(node myNode)
    {
        if (myNode == null) 
            return false;
        return (myNode.color == true);
    }
  
    // utility function to swap color of two 
    // nodes.
    void swapColors(node node1, node node2)
    {
        boolean temp = node1.color;
        node1.color = node2.color;
        node2.color = temp;
    }
  
    // insertion into Left Leaning Red Black Tree.
    node insert(node myNode, int data)
    {
        // Normal insertion code for any Binary
        // Search tree. 
        if (myNode == null) 
            return new node(data); 
  
        if (data < myNode.data) 
            myNode.left = insert(myNode.left, data);
          
        else if (data > myNode.data) 
            myNode.right = insert(myNode.right, data);
          
        else
            return myNode;
  
  
        // case 1.
        // when right child is Red but left child is
        // Black or doesn't exist.
        if (isRed(myNode.right) && !isRed(myNode.left))
        {
            // left rotate the node to make it into
            // valid structure.
            myNode = rotateLeft(myNode);
  
            // swap the colors as the child node 
            // should always be red
            swapColors(myNode, myNode.left);
  
        }
  
        // case 2
        // when left child as well as left grand child in Red
        if (isRed(myNode.left) && isRed(myNode.left.left))
        { 
            // right rotate the current node to make 
            // it into a valid structure.
            myNode = rotateRight(myNode);
            swapColors(myNode, myNode.right);
        }
  
  
        // case 3
        // when both left and right child are Red in color.
        if (isRed(myNode.left) && isRed(myNode.right))
        {
            // invert the color of node as well 
            // it's left and right child.
            myNode.color = !myNode.color;
  
            // change the color to black.
            myNode.left.color = false;
            myNode.right.color = false; 
        }
  
        return myNode;
    }
  
  
    // Inorder traversal
    void inorder(node node)
    {
        if (node != null)
        {
            inorder(node.left);
            System.out.print(node.data + " ");
            inorder(node.right); 
        }
    }
  
    public static void main(String[] args) {
    /* LLRB tree made after all insertions are made.
              
    1. Nodes which have double INCOMING edge means
        that they are RED in color.
    2. Nodes which have single INCOMING edge means
        that they are BLACK in color.
      
        root
        |
        40
        // \
    20 50
    / \
    10 30
        //
        25 */
      
    LLRBTREE node = new LLRBTREE();
      
    root = node.insert(root, 10);
    // to make sure that root remains
    // black is color
    root.color = false;
      
    root = node.insert(root, 20);
    root.color = false;
      
    root = node.insert(root, 30);
    root.color = false;
      
    root = node.insert(root, 40);
    root.color = false;
      
    root = node.insert(root, 50);
    root.color = false; 
      
    root = node.insert(root, 25);
    root.color = false; 
  
    // display the tree through inorder traversal.
    node.inorder(root);
  
    }
      
}
  
// This code is contributed by ARSHPREET_SINGH


C#
// C# program to implement insert 
// operation in Red Black Tree. 
using System;
  
class node 
{ 
    public node left, right; 
    public int data; 
  
    // red ==> true, black ==> false 
    public Boolean color; 
      
    public node(int data) 
    { 
        this.data = data; 
        left = null; 
        right = null; 
          
        // New Node which is created 
        // is always red in color. 
        color = true; 
    } 
} 
  
public class LLRBTREE 
{ 
  
private static node root = null; 
  
// utility function to rotate
// node anticlockwise. 
node rotateLeft(node myNode) 
{ 
    Console.Write("left rotation!!\n"); 
    node child = myNode.right; 
    node childLeft = child.left; 
  
    child.left = myNode; 
    myNode.right = childLeft; 
  
    return child; 
} 
  
// utility function to rotate
// node clockwise. 
node rotateRight(node myNode) 
{ 
    Console.Write("right rotation\n"); 
    node child = myNode.left; 
    node childRight = child.right; 
  
    child.right = myNode; 
    myNode.left = childRight; 
  
    return child; 
} 
  
// utility function to check whether 
// node is red in color or not. 
Boolean isRed(node myNode) 
{ 
    if (myNode == null) 
        return false; 
    return (myNode.color == true); 
} 
  
// utility function to swap 
// color of two nodes. 
void swapColors(node node1, node node2) 
{ 
    Boolean temp = node1.color; 
    node1.color = node2.color; 
    node2.color = temp; 
} 
  
// insertion into Left
// Leaning Red Black Tree. 
node insert(node myNode, int data) 
{ 
    // Normal insertion code for 
    // any Binary Search tree. 
    if (myNode == null) 
        return new node(data); 
  
    if (data < myNode.data) 
        myNode.left = insert(myNode.left, data); 
      
    else if (data > myNode.data) 
        myNode.right = insert(myNode.right, data); 
      
    else
        return myNode; 
  
  
    // case 1. 
    // when right child is Red
    // but left child is 
    // Black or doesn't exist. 
    if (isRed(myNode.right) && 
        !isRed(myNode.left)) 
    { 
        // left rotate the node to make 
        // it into valid structure. 
        myNode = rotateLeft(myNode); 
  
        // swap the colors as the child 
        // node should always be red 
        swapColors(myNode, myNode.left); 
  
    } 
  
    // case 2 
    // when left child as well as 
    // left grand child in Red 
    if (isRed(myNode.left) && 
        isRed(myNode.left.left)) 
    { 
        // right rotate the current node 
        // to make it into a valid structure. 
        myNode = rotateRight(myNode); 
        swapColors(myNode, myNode.right); 
    } 
  
  
    // case 3 
    // when both left and right 
    // child are Red in color. 
    if (isRed(myNode.left) && 
        isRed(myNode.right)) 
    { 
        // invert the color of node as well 
        // it's left and right child. 
        myNode.color = !myNode.color; 
  
        // change the color to black. 
        myNode.left.color = false; 
        myNode.right.color = false; 
    } 
  
    return myNode; 
} 
  
  
// Inorder traversal 
void inorder(node node) 
{ 
    if (node != null) 
    { 
        inorder(node.left); 
        Console.Write(node.data + " "); 
        inorder(node.right); 
    } 
} 
  
// Driver Code
static public void Main(String []args)
{ 
/* LLRB tree made after all 
   insertions are made. 
          
1. Nodes which have double INCOMING 
   edge means that they are RED in color. 
2. Nodes which have single INCOMING edge  
   means that they are BLACK in color. 
  
            root 
             | 
             40 
           //   \ 
          20    50 
        /    \ 
       10    30 
            // 
           25 
*/
  
LLRBTREE node = new LLRBTREE(); 
  
root = node.insert(root, 10); 
  
// to make sure that root  
// remains black is color 
root.color = false; 
  
root = node.insert(root, 20); 
root.color = false; 
  
root = node.insert(root, 30); 
root.color = false; 
  
root = node.insert(root, 40); 
root.color = false; 
  
root = node.insert(root, 50); 
root.color = false; 
  
root = node.insert(root, 25); 
root.color = false; 
  
// display the tree through 
// inorder traversal. 
node.inorder(root); 
} 
}
  
// This code is contributed 
// by Arnab Kundu


输出:

left rotation!!
left rotation!!
left rotation!!
10 20 25 30 40 50 

时间复杂度: O(log n)