Lab tomorrow

Drawing red-black trees

Bring two colors of pens

 

Red-Black Trees

Another way to balance binary search trees

Chapter 28.30 in Carrano/Savitch

What is a red-black tree

•      It is a binary search tree in which each node is either red or black

•      The root is always black

•      If a node is red, its children must be black

•      Every path from a node to NULL pointer must contain the same number of black nodes

–    The paths we are interested in are to a NULL pointer

–    This means the path from the root to items with no children (a leaf), or to items with one child

•      Note that the paths we are interested in are not necessarily to a leaf!

 

Red-black tree definition

•      A red-black tree is a BST that is empty, or in which the root node is colored black, and every other node is colored red or black, and the following properties are satisfied:

•     Red rule: If an node is red, it parents must be black

–    i.e. no two red nodes together

•     Path rule: The number of black nodes must be the same in all paths from the root to a node with no children or with one child.

–    Another way to say this is from the root node to a node with a NULL pointer

•      Look at some BST on the overhead and decide which are red-black trees

 

Inserting into a red-black tree

•      The new item is placed as a leaf in the tree

•      If the new node is colored black, it will violate the path rule

–   So all new insertions are red

•      If the parent is black we are done.

•      If the parent is red, we would violate the red rule

–   So the parent being red indicates the need to adjust the tree

–   This adjustment is done by color changes and tree rotations

 

Insertion considerations

•      To decide what needs to be done after insertions, we look at the local neighborhood.  This consists of the

•   curr_node the node being inserted

•   parent of node being inserted

•   grandparent of the node being inserted

•   uncle the sibling of parent

–   Since an adjustment may involve recoloring, it may just move the problem two steps up the tree

–   In this case it is dealt with recursively

 

Inserting nodes into a red-black tree

•         Find the place to insert into the tree, and color the new node red

–     If the parent is black, we are done

–     If it is red, an adjustment must be performed

•       Six cases dealing with parent and uncle

  New node        Uncle                  Parent

•       Either            red                 red left child

•       Right             black              red left child

•       Left               black              red left child

•       The other three cases are mirror images with the parent being a red right child

 

 

 

Case 1      

CurrNode— either left or right child    Parent – red left child    Uncle– red

What to do:

Recolor parent, uncle, grandparent

     may have to readjust if result makes two reds together higher in the tree

 

Case 2– ziz-zag case

CurrNode—right child   Parent – red left child   Uncle– black or none

There is a ziz-zag between the current node, the parent

What to do:

Rotate  parent and  CurrNode and reset CurrNode

It then becomes case 3

 

Case 3

CurrNode—left child   Parent – red left child   Uncle– black or none

What to do:

Change color of parent and grandparent

Rotate(parent, grandparent)

 

Insert into a red-black tree

53, 27, 75, 25, 70, 41, 38, 16, 59, 36

23, 15, 40, 8, 17, 21, 18, 16, 55

 

Efficiency of Red-Black trees

•      A red-black tree has just one extra bit of storage in each node, its color

•      Inserting a node runs in O(log n) time since the rotations take constant time

•      Deleting a node is only slightly more complex than inserting

–   It also runs in O(log n) time

 

Other trees to practice

•      25, 27, 16, 36, 38, 53, 41, 59, 75, 70

 

Summary

Case 1 uncle—red;   parent -- red left child;   CurNode -- either   L or R

Recolor parent, uncle, grandparent

 

Case 2  CurNode--right child;  parent--a red left child, uncle – black or none

–    Rotate (parent, CurNode) and reset CurNode

–    It then becomes case 3

 

Case 3 CurNode -- left child; parent--red left child;   uncle–black or none

–    Change color of parent and grandparent

–    Rotate(parent, grandparent)