rb trees

Upload: damir666

Post on 04-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Rb Trees

    1/3

    RED-BLACK TREES

    Time complexity

    in big O notation

    Average Worst case

    Space O(n) O(n)

    Search O(log n) O(log n)

    Insert O(log n) O(log n)Delete O(log n) O(log n)

    1. Every node is either red or black.

    2. The root is always black.

    3. If a node is red, its children must be black (although the converse isnt necessarily true).

    4. Every path from the root to a leaf, or to a null child, must contain the same number of black nodes.The number of black nodes on a path (black height). Must be the same for all paths from the root to a leaf .

    ASSUME NO DUPLICATES BECAUSE:

    Search process becomes more complicated if all items with the same key must be found.

    IF COLOR RULES VIOLATED:1. change the colors of nodes.

    2. You can perform rotations.

    COLOR FLIP:

    -changes a black node with two red children to a red node with two black children.

    ROTATION:-one node is designated the top node.

    A right rotation moves the top node into the position of its right child, and the top nodes left child into itsposition. A left rotation moves the top node into the position of its left child, and the top nodes right child into its

    position.

    INSERTION:

    After a new node is inserted, red-red conflicts are checked again. If a violation is found, appropriate rotations are

    carried out to make the tree red-black correct.

    /*** Print the tree contents in sorted order.*/

    publicvoid printTree( ){ if( isEmpty( ) ) System.out.println( "Empty tree" );else printTree( header.right );}

    /*** Internal method to print a subtree in sorted order.* @param t the node that roots the subtree.*/privatevoid printTree( RedBlackNode t ){if( t != nullNode ){ printTree( t.left );

    System.out.println( t.element );printTree( t.right ); } }

    http://en.wikipedia.org/wiki/Time_complexityhttp://en.wikipedia.org/wiki/Big_O_notationhttp://en.wikipedia.org/wiki/Big_O_notationhttp://en.wikipedia.org/wiki/Time_complexity
  • 7/30/2019 Rb Trees

    2/3

    publicclass RedBlackTree

  • 7/30/2019 Rb Trees

    3/3

    }

    // Used in insert routine and its helpersprivate RedBlackNode current;private RedBlackNode parent;private RedBlackNode grand;private RedBlackNode great;/*** Internal routine that is called during an insertion* if a node has two red children. Performs flip and rotations.

    * @param item the item being inserted.*/privatevoid handleReorient( AnyType item ){// Do the color flipcurrent.color = RED;current.left.color = BLACK;current.right.color = BLACK;if( parent.color == RED )// Have to rotate{grand.color = RED;if( ( compare( item, grand ) < 0 ) != ( compare( item, parent ) < 0 ) )

    parent = rotate( item, grand ); // Start dbl rotatecurrent = rotate( item, great );current.color = BLACK;}header.right.color = BLACK; // Make root black}

    /*** Insert into the tree.* @param item the item to insert.*/publicvoid insert( AnyType item ){

    current = parent = grand = header;nullNode.element = item;while( compare( item, current ) != 0 ){great = grand; grand = parent; parent = current;current = compare( item, current ) < 0 ? current.left : current.right;

    // Check if two red children; fix if soif( current.left.color == RED && current.right.color == RED ) handleReorient( item ); }// Insertion fails if already presentif( current != nullNode ) return;current = new RedBlackNode( item, nullNode, nullNode );// Attach to parent

    if( compare( item, parent ) < 0 ) parent.left = current;else parent.right = current; handleReorient( item );}