7_redblack

7_redblack - Red–black trees  !

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Red–black trees  !  Define the red‐black tree properties  !  Describe and implement rotations  !  Implement red‐black tree insertion  !  Implement red‐black tree deletion  October 2004 John Edgar 2 !  !  Items can be inserted in  and removed from BSTs  in O(height) time   So what is the height of a  BST?  !  If the tree is balanced: O 43 24 12 37 61 balanced BST  height = O(logn)  61 12 43 37 24 (logn)  !  If the tree is very  unbalanced: O(n)  unbalanced BST  height = O(n)  October 2004 John Edgar 3 !  Define a balanced binary tree as one where  !  There is no path from the root to a leaf that is more than  !  Guaranteeing that a BST is balanced requires either  !  A more complex structure (2‐3 and 2‐3‐4 trees) or  !  More complex insertion and deletion algorithms (red‐ twice as long as any other such path  !  The height of such a tree is O(logn)  black trees)  October 2004 John Edgar 4 !  !  A red‐black tree is a balanced BST  Each node has an extra colour field which is  !  red or black  !  !  Nodes have an extra pointer to their parent Imagine that empty nodes are added so that every  real node has two children  !  They are imaginary nodes so are not allocated space  !  The imaginary nodes are always coloured black  ▪  Usually represented as a boolean – isBlack October 2004 John Edgar 5 1.  2.  !  Every node is either red or black  Every leaf is black  This refers to the imaginary leaves  ▪  i.e. every null child of a node is considered to be a black leaf  3.  4.  5.  If a node is red both its children must be black  Every path from a node to a leaf contains the same  number of black nodes  The root is black (mainly for convenience)  October 2004 John Edgar 6 !  The black height of a node, bh(v), is the number of  black nodes on a path from v to a leaf  !  Without counting v itself  !  Because of property 4 every path from a node to a leaf  !  The height of a node, h(v), is the number of nodes  on the longest path from v to a leaf  !  Without counting v itself  !  From property 3 a red node’s children must be black  ▪  So h(v) ≤ 2(bh(v))  contains the same number of black nodes  October 2004 John Edgar 7 !  It can be shown that a tree with the red‐black  structure is balanced  !  !  Assume that a tree has n internal nodes  A balanced tree has no path from the root to a leaf that is  more than twice as long as any other such path  !  An internal node is a non‐leaf node, and the leaf nodes are  imaginary nodes  !  A red‐black tree has ≥ 2bh – 1 internal (real) nodes  ▪  Can be proven by induction (e.g. Algorithms, Cormen et al.)  October 2004 John Edgar 8 !  Claim: a red‐black tree has height, h ≤ 2*log(n+1)  !  n ≥ 2bh – 1 (see above)  !  bh ≥ h / 2 (red nodes must have black children)   !  n ≥ 2h/2 – 1 (replace bh with h)  !  log(n + 1) ≥ h / 2 (add 1, log2 of both sides)  !  h ≤ 2*log(n + 1) (multiply both sides by 2)  October 2004 John Edgar 9 !  !  An item must be inserted into a red‐black tree at  the correct position  The shape of a tree is determined by  !  The values of the items inserted into the tree  !  The order in which those values are inserted  This suggests that there is more than one tree (shape)  that can contain the same values  !  A tree’s shape can be altered by rotation while still  preserving the bst property  !  !  Note: only applies to bst with no duplicate keys!  October 2004 John Edgar 11 Left rotate(x)  z  x  D  y  A  B  C  D  A  B  C  x  y  z  October 2004 John Edgar 12 Right rotate(z)  z  x  x  D  y  C  A  A  B  B  C  D  y  z  October 2004 John Edgar 13 Left rotation of 32, call the node x  Assign a pointer to x's R child  47  32  temp  81  13  40  37  44  October 2004 John Edgar 14 Left rotation of 32, call the node x  Assign a pointer to x's R child  Make temp’s L child x’s R child  Detach temp’s L child  32  temp  47  81  13  40  37  44  October 2004 John Edgar 15 Left rotation of 32, call the node x  Assign a pointer to x's R child  Make temp’s L child x’s R child  Detach temp’s L child  Make x temp's L child  Make temp x's parent's child  13  40  temp  32  47  81  37  44  October 2004 John Edgar 16 Left rotation of 32, call the node x  47  40  81  32  44  13  37  October 2004 John Edgar 17 Right rotation of 47, call the node x  Assign a pointer to x's L child  47  32  temp  40  81  13  7  29  37  October 2004 John Edgar 18 Right rotation of 47, call the node x  Assign a pointer to x's L child  Make temp’s R child x’s L child  Detach temp’s R child  32  temp  40  47  81  13  7  29  37  October 2004 John Edgar 19 Right rotation of 47, call the node x  Assign a pointer to x's L child  Make temp’s R child x’s L child  Detach temp’s R child  Make x temp's L child  13  40  32  temp  47  81  7  29  37  October 2004 John Edgar 20 Right rotation of 47, call the node x  Assign a pointer to x's L child  Make temp’s R child x’s L child  Detach temp’s R child  Make x temp's L child  Make temp the new root  7  29  40  81  13  32  temp  47  37  October 2004 John Edgar 21 !  Insert as for a binary search tree  !  Make the new node red  October 2004 John Edgar 23 Insert 65  47  32  71  93  October 2004 John Edgar 24 Insert 65  47  32  71  65  93  October 2004 John Edgar 25 !  Insert as for a binary search tree  !  Make the new node red  !  What can go wrong? (see slide 6)  !  The only property that can be violated is that both a red  node’s children are black (its parent may be red)  !  So, after inserting, fix the tree by re‐colouring nodes  and performing rotations  October 2004 John Edgar 26 !  The fixing of the tree remedies the problem  of two consecutive red nodes  !  It is iterative (or recursive) and pushes this  !  There are a number of cases (that’s what is next)  problem one step up the tree at each step  the next step they are at d‐1  !  This is why it turns out to be O(log n)  ▪  We won’t go into the analysis  !  I.e. if the consecutive red nodes are at level d, at  October 2004 John Edgar 27 !  !  !  If parent and uncle are both red  !  Then colour them black  !  And colour the grandparent red  Need to fix tree if new node’s parent is red  Case I for fixing:  ▪  It must have been black beforehand, why?  October 2004 John Edgar 28 Insert 65  Insert 82  32  47  71  65  93  October 2004 John Edgar 29 Insert 65  Insert 82  32  47  71  65  93  82  October 2004 John Edgar 30 Insert 65  Insert 82  32  47  71  71  65  change nodes’ colours  82  93  October 2004 John Edgar 31 !  !  !  If parent is red but uncle is black  !  Need to do some tree rotations to fix it  Need to fix tree if new node’s parent is red  Case II for fixing:  October 2004 John Edgar 32 Insert 65  Insert 82  Insert 87  32  47  71  65  93  82  October 2004 John Edgar 33 Insert 65  Insert 82  Insert 87  32  47  71  65  93  82  87  October 2004 John Edgar 34 Insert 65  Insert 82  Insert 87  32  47  71  65  93  82  87  October 2004 John Edgar 35 Insert 65  Insert 82  Insert 87  32  47  71  65  93  87  82  October 2004 John Edgar 36 Insert 65  Insert 82  Insert 87  32  47  71  65  change nodes’ colours  87  93  93  82  October 2004  John Edgar  37  Insert 65  Insert 82  Insert 87  32  47  71  65  87  82  93  October 2004 John Edgar 38 !  Why were these rotations performed?  !  First rotation made the two red nodes left  children of their parents  !  This rotation isn’t performed if this is already the  !  Second rotation and subsequent recolouring  case  !  Note that grandparent must be a black node  fixes the tree  October 2004 John Edgar 39 !  Full details require a few cases  !  See link to example code snippets at end  !  Understand the application of tree rotations  October 2004 John Edgar 40 !  Modify the bst deletion algorithm  !  If the deleted node is be replaced by its predecessor  replace its data, rather than the entire node   ▪  So that the node's colour remains the same  !  Then remove the predecessor  ▪  Hence, we’re always deleting a node with one or zero “real” children  October 2004 John Edgar 42 Delete 87  47  32  71  65  87  82  93  October 2004 John Edgar 43 Delete 87  47  32  71  Replace data with predecessor  Predecessor red: no violation  65  82  7  82  93  October 2004 John Edgar 44 !  !  If the removed node was red  If the removed node was black then fix the tree  !  If the deleted node had two children then the node that is  !  No problem  removed is the deleted node’s predecessor  ▪  The removed node’s child is passed to the tree fix algorithm  ▪  This child may be a (black) imaginary (null) child  October 2004 John Edgar 45 !  The tree‐fix algorithm first colours its node  parameter (call it x) black  Two cases:  !  Case I: If x was red it is now black (and the tree is fixed)  !  This corrects the violation to the black height property   caused by removing a black node    !  October 2004 John Edgar 46 Delete 71  47  32  71  65  87  51  82  93  October 2004 John Edgar 47 Delete 71  47  32  65  71  Replace with predecessor  Attach predecessor’s child  51  65  87  82  93  October 2004 John Edgar 48 Delete 71  47  32  65  Replace with predecessor  Attach predecessor’s child  Fix tree by colouring       predecessor’s child black  51  51  82  87  93  October 2004 John Edgar 49 !  The tree‐fix algorithm first colours its node  parameter (call it x) black  !  This corrects the violation to the black height property   caused by removing a black node    !  Two cases:  !  Case II: If x was black then it becomes "doubly black"  !  Violating the property that nodes are red or black  !  The extra black colour is pushed up the tree until  ▪  A red node is reached, when it is made black  ▪  The root node is reached or   ▪  The tree can be rotated and re‐coloured to fix the problem  October 2004 John Edgar 50 Delete 32  47  32  65  51  87  82  93  October 2004 John Edgar 51 Delete 32  47  32  x  Identify x: the removed node’s L child  Remove target node  Attach x to parent of target  82  93  x  65  51  87  October 2004 John Edgar 52 Delete 32  47  x  65  Identify x: the removed node’s L child  Remove target node  Attach x to parent of target  Call r‐bTreeFix on x  51  87  82  93  October 2004 John Edgar 53 Delete 32  Calling TreeFix   Identify y, x’s sibling  Make y black, y’s  parent red  L rotate x’s parent  82  93  47  x  65  y  51  87  October 2004 John Edgar 54 Delete 32  Calling TreeFix   Identify y, x’s sibling  Make y black, y’s  parent red  L rotate x’s parent  Identify y – x’s new  sibling  47  x  new y  51  65  y  87  82  93  October 2004 John Edgar 55 Delete 32  Calling TreeFix   Identify y, x’s sibling  Make y black, y’s  parent red  L rotate x’s parent  Identify y – x’s new  sibling  Colour y red  Assign x its parent,  and colour it black  October 2004 John Edgar 56 new x  47  x  51  51  y  65  87  82  93  !  !  The example showed the first two of four cases in  the tree fix algorithm  The four cases are as follows  !  The first case colours a red sibling of x black, which  converts it into one of the other three cases  !  In the second case both of x’s sibling’s children  (nephews?) are black  !  The third and fourth cases occur when just one of x’s  sibling’s children are black  October 2004 John Edgar 57 !  Red‐black trees are balanced binary search  trees  !  Augment each node with a colour  maintains balance of tree  !  Maintaining relationships between node colours  !  Important operation to understand: rotation  !  Modify tree but keep binary search tree property  (ordering of nodes)  John Edgar October 2004 59 For implementation details, please see:  http://en.wikipedia.org/wiki/Red‐black_tree  !  (see “Operations”)  You may use the insertion/deletion code from this  page in A3.  If you do so, include documentation  (comments in your code) citing this as the source  for each function you use.  October 2004 John Edgar 60 ...
View Full Document

This note was uploaded on 04/17/2010 for the course CMPT 11151 taught by Professor Gregorymori during the Spring '10 term at Simon Fraser.

Ask a homework question - tutors are online