red-black trees

  1. What is a red-black tree?
    A red-black tree is a kind of binary search tree that is balanced.
  2. Why do we want a binary search tree that is balanced?
    A balanced tree guarantees a limit on the height (O (lg n)), which puts a time cap on non-modifying tree operations (min, max, successor, predecessor, search) of O(lg n).
  3. Red-black trees have __________ that is used to maintain the tree's "balance".
    an extra bit of information - "red" or "black"
  4. (T/F) All balanced trees guarantee logarithmic height.
  5. In a red-black tree every node is either ____________.
    red or black
  6. In a red-black tree the root is ____________.
  7. In a red-black tree every leaf is ____________.
    NIL and black
  8. In a red-black tree if a node is red then both its children are ____________.
  9. In a red-black tree, for each node, all simple paths to its descendant leaves have the ____________.
    same number of black nodes (this is why the concept of black height is important)
  10. Look at the illustration of a red-black tree.
  11. What is the height of a node in a tree?
    The height is the number of edges in the longest path to a leaf.
  12. What is the black height of a node in a red-black tree?
    The black height is the number of black nodes (including T.nil) on the path from x to leaf (not counting x).
  13. Look at the illustration regarding height and black height.
  14. How are the height and black height of a node related?
    If a node has height h, its black height must be >= h/2. Why? The fourth property of a red-black tree states that if a node is red then its children must be black. On a path, each red node must be followed by a black node. Therefore at least h/2 of the nodes on the path from the node to a leaf are black.
  15. What can be said about the number of nodes rooted at any node x in a red black tree?
    Because a red-black tree is balanced, the number of nodes in a level is a power of 2^level. The number of nodes in a tree rooted at a node x in a red black tree can be expressed as 2 ^ (black height of x) - 1.
  16. What formula expresses the relationship between the height of a red-black tree and the number of nodes (n) in a tree?
    h(x) <= 2 lg (n+1)
  17. What method is used to change the shape of a red-black tree?
    Rotation - one side gets taller, the other shrinks.
  18. Why is rotation used in a red-black tree?
    Rotation is used to maintain red-black tree properties upon insertion. It maintains the inorder ordering of keys, and relative subtree heights stay the same.
  19. Look at the rotation example.
  20. Look at pseudocode for Left-Rotate.
  21. (T/F) Search, Min, Max, Successor, and Predecessor are the same for Red-Black tree as for Binary Search Tree.
  22. (T/F) Insert and Delete are the same for Red-Black tree as they are for Binary Search Tree.
    False. A Red-Black has more properties (to maintain a balanced tree) to maintain than a binary search tree. Upon insertion, color must be assigned to the new node and adjustments may need to be made to the tree to ensure that it continues to meet the conditions for a Red-Black tree. Upon deletion, recolorings and positional changes may need to be made to ensure continued compliance with Red-Black Tree conditions.
  23. On insert, what color is the new node initially assigned?
    Red. Even though this might violate property 4 (red parent => both children are black), this problem is easier to correct than making initial color black (might violate property 5 (all paths to descendants have same number of black nodes)).
  24. When a node is deleted, does the color of the deleted node have any bearing on any adjustments that may need to be made to the red-black tree?
    Yes. If the deleted node was red, we won't have changed any black heights, nor will we have caused there to be two red nodes in a row. But, if the deleted node was black, we could cause there to be two red nodes in a row (violation of property 4), or we could cause there to be problem with black heights. Also, if the root was deleted, one of its children (which must be red), will become the root (which must be black) (property 2).
  25. Generally speaking, how do we insert a new node into a red-black tree?
    First, insert the new node as a RED leaf into the red-black tree (just like the insertion into a binary search tree). This MAY cause a red-red violation or a red root violation, which is fixed with a RB-Insert-Fixup procedure.
  26. Look at the pseudocode for RB-Insert.
  27. Look at the pseudocode for RB-Insert-Fixup.
  28. For an insert into a red-black tree, there are three cases involving red-red violations that must be handled if they occur on insert. What are they?
    • 1. If the newly inserted node causes a r-r violation and has a red uncle, recolor above generations - but only until the problem is fixed or another case is encountered (recoloring the whole tree could cause a red root).
    • 2. Black uncle with zig-zag - move problem to a parent and rotate on parent to remove zig-zag.
    • 3. Black uncle without zig-zag - recolor parent to black and grandparent to red, then rotate on grandparent to balance.
    • *There is an example regarding these cases.*
  29. See examples about the 3 cases of red-red violations and how to resolve them.
  30. See example of red-black tree insertion that progresses through all 3 cases.
  31. See question regarding red-black tree insertion.
Card Set
red-black trees
cs340 red black trees