C2java 2nd insert a red/black tree

Source: Internet
Author: User

The red/black tree serves as the underlying storage structure of Set and Map in C ++ STL and java Collection, which is very important. As a special tree in the CS tree, understanding it is almost a beginner. In the words of Mr. Wang zhaizhai, Qian Xiaotang: (Guo Lao) the wall of the husband is high, and the king is both in and out.

/* RBTree. java -- Red Black Treedoc: 0. comparison between the red and black trees and AVL trees: Insert a maximum of 2 rotations (12 inserts), and the maximum tree height is 2 * log (n) 1. the red/black tree is a binary representation of 2-3-4 trees. 2. 2-3-4 trees map to the features of the Red-black tree: the root node is black, two consecutive red nodes are not allowed, and one node of the 2-4-4 tree corresponds to a black node in the red-black tree. 3. 2-3-4 Tree Insertion Method: Split 4-nodes along the road; always Insert at the leaf node. Someone has been asking on the internet how the following R1-R4 insertion rules come from, in fact, is derived from 2 and 3. 2-3-4 Tree insertion is easy to draw on paper, and it is easy to switch nodes to the red-black tree. However, to directly draw the red-black tree, you must be very skillful in rotation. 4. the right-hand side of X actually refers to raising the position of X to a layer. The parent node of X falls down by a layer, that is, "turn right and spiral up ". For example, after six blocks and four closed steps, the waist is spiraling. For the permissions of classes and members, my approach is: 1. the default value of "extern" is not added to all files in C. The default value of "package-private" is not added to all files in java. That is, there is no limit on access within the same directory. 2. If the function in the corresponding C file is static in front of the function, add private to the method in java. 3. Add public to emphasize third-party use. Official reference: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.htmlauthor: ludi 2014.03 */class RBNode
 
  
> {Final static int BLACK = 0, RED = 1; RBNode
  
   
Parent, left, right; int color; T nodeValue; RBNode (T item, RBNode
   
    
Left, RBNode
    
     
Right, RBNode
     
      
Parent, int color) {nodeValue = item; this. left = left; this. right = right; this. parent = parent; this. color = color ;}} public class RBTree
      
        > {RBNode
       
         Root; RBNode
        
          NIL = null; public RBTree () {/* constructor: Construct NIL so that it can be rotated like other nodes */if (NIL = null) NIL = new RBNode
         
           (Null, null, RBNode. BLACK); root = NIL;} public void deleteTree (RBNode
          
            T) {/* destructor */if (t! = NIL) {deleteTree (t. left); deleteTree (t. right); t = null;} private void rotate (RBNode
           
             Cursor, int type) {/* type = 0: Left-hand */RBNode
            
              P = middle. parent, g = bytes. parent. parent; if (0 = type) {p. right = middle. left; ignore. left = p; middle. parent = g; p. parent = cursor; if (p. right! = NIL) p. right. parent = p;} else {p. left = middle. right; enabled. right = p; enabled. parent = g; p. parent = cursor; if (p. left! = NIL) p. left. parent = p;} if (p = root) root = kernel; else if (p = g. right) g. right = elastic; elseg. left = left;} private void split4Node (RBNode
             
               X) {/* increase 4-nodes and perform necessary rotation * // * flip color */x. color = RBNode. RED; x. left. color = RBNode. BLACK; x. right. color = RBNode. BLACK; RBNode
              
                P = x. parent; if (p. color = RBNode. RED) {/* if the parent node is RED, rotate */RBNode
               
                 G = x. parent. parent; g. color = RBNode. RED; if (p = g. left & x = p. right) {/* x is the internal grandson of g */rotate (x, 0); x. color = RBNode. BLACK; p = x;/* prepare the right hand */} else if (p = g. right & amp; x = p. left) {rotate (x, 1); x. color = RBNode. BLACK; p = x;} else {p. color = RBNode. BLACK;} rotate (p, p = g. left? 1: 0) ;}} public boolean add (T item) {/* Insert */RBNode
                
                  Curr = root, parent = NIL, newNode; while (curr! = NIL) {/* Find the insertion point */if (curr. nodeValue. equals (item) return false;/* R1 splits 4-nodes along the road */if (curr. left. color = RBNode. RED & curr. right. color = RBNode. RED) split4Node (curr); parent = curr; if (item. compareTo (curr. nodeValue) <0) curr = curr. left; elsecurr = curr. right;}/* R2 new node is inserted as a red node */newNode = new RBNode
                 
                   (Item, NIL, NIL, parent, RBNode. RED); if (parent = NIL) {root = newNode;} else {if (item. compareTo (parent. nodeValue) <0) parent. left = newNode; elseparent. right = newNode;/* if (parent. color = RBNode. RED) split4Node (newNode);} root. color = RBNode. BLACK;/* R4 the root node is BLACK */return true;}/* The following is a non-core method, which is only used to practice Binary Trees */void visitInOrder (RBNode
                  
                    T) {if (t = NIL) return; visitInOrder (t. left); System. out. print (t. nodeValue + ""); visitInOrder (t. right);} int height (RBNode
                   
                     T) {int hl, hr; if (t = NIL) return-1; hl = height (t. left); hr = height (t. right); hl = 1 + (hl> hr? Hl: hr); System. out. print (t. nodeValue + ":" + hl + ""); return hl ;}} class Test {public static void main (String [] arg) {RBTree
                    
                      Tree = new RBTree
                     
                       (); // Int [] arr = {40, 20, 10, 35, 50, 25, 30}; int [] arr =, 55,11}; for (int x: arr) {tree. add (x);} System. out. println ("tree height:"); tree. height (tree. root); System. out. println (); System. out. println ("visitInOrder:"); tree. visitInOrder (tree. root); System. out. println (); tree. deleteTree (tree. root); tree = null;}/* ludi @ ubun :~ /Java $ javac-encoding UTF-8 RBTree. java & java Testtree height: visitInOrder: 2 4 8 10 11 12 15 25 35 55 ludi @ ubun :~ /Java $ */
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

As the deletion operation is complex, you need to refer to the instructions later and paste them on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.