1 / 56

Lecture 17 Red-Black Trees

Lecture 17 Red-Black Trees. Slides taken from lectures by Prof. Naveen Garg. 2 - 4 Tree. insert, search, and delete of O(log n) but nodes are dynamic with 2-4 keys. Can we have benefits of 2-4 trees with simple binary nodes?. Lets convert 2-4 tree to a binary tree!.

akiko
Download Presentation

Lecture 17 Red-Black Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 17Red-Black Trees Slides taken from lectures by Prof. Naveen Garg

  2. 2-4 Tree • insert, search, and delete of O(log n) • but nodes are dynamic with 2-4 keys

  3. Can we have benefits of 2-4 trees with simple binary nodes?

  4. Lets convert 2-4 tree to a binary tree!

  5. Stretch it from top and bottom! a b c d e m A t f g h i j k l n o p q r s B C D u v w x y

  6. b Red Black Tree a c e m t A d r v y l C o D x B u j k w s q n p g i f h

  7. 13 1 4 9 11 14 20 18 3 8 10 Convert the below 2-4 tree to Red Black Tree! 12 15 2 5 6 13 8 18 20 10 15 3 12 14 2 9 5 1 4 6 11

  8. Red Black Trees • A BST with Red and Black colored nodes • Root is black • Black node can have black children, but red node can only have black children • Every external node has the same black depth

  9. Red Black Tree Examples Black height of tree is 2 Black height of tree is 2

  10. Are these Red-Black Trees? Black height not uniform Double red

  11. Red-Black Tree to 2-4 Tree • Take a black node and its red children and combine them into one node of a 2-4 tree. • Each node so formed has at least 1 and at most 3 keys. • Black height = number of levels in 2-4 tree

  12. 1 4 9 13 2 3 5 7 11 17 19 9 4 Convert Red-Black Tree to 2-4 Tree 13 19 7 11 2 1 5 17 3

  13. Height of Red-Black Tree

  14. Minimum number of nodes for black height h? n≥2h+1-1 h≤log2(n+1)-1

  15. Maximum number of nodes for black height h? n≤22h+1-1 n≤4h+1-1 h≥log4(n+1)-1

  16. Red-Black Tree Black Height log4(n+1)-1 ≤ h ≤ log2(n+1)-1 1/2log2(n+1) ≤ h+1 ≤ log2(n+1) O(log n)

  17. Height of a Red-Black tree is at max twice the black height, i.e.O(log n)

  18. Search for k as in BST that will give you the correct place to insert • Create a new node at this place and insert the key • The new node is colored red • Restore the red-black tree property Insert (k)

  19. Two Cases k k Double red problem No problem k Red root problem Color the root black

  20. Case 1: Sibling of the parent of the inserted node is black or NULL a b NULL or k b a c k c Equivalent 2-4 tree node contains {b,a,k} but malformed. The rotation corrects the defect.

  21. Case 2: Sibling of the parent of the inserted node is red b b a c a c k k b c b a k c a k

  22. The double red problem can move up the tree… d b a c k What if d is root?

  23. Time complexity of insert? • BST search O(log n) • Rebalancing: 1 rotation, O(log n) recoloring • O(log n)

  24. Delete • To delete a node we proceed as in a BST. • Thus the node which is deleted is the parent of an external node. • Hence it is either a leaf or the parent of a leaf.

  25. Three Situations 19 1 11 17 17

  26. Deleting black Node • Reduces the height by 1 • In general, it may reduce the height of a subtree from h to h-1

  27. a a a b a b a b c c b c c c a d d a a b b b a a c b a b c b c c d a is black a is red b is black b is red The cases: Both c are black Both c are black Both d are black Some c is red Some c is red Some d is red

  28. c a Deletion: case1.1 b b a T4 h to h-1 h-1 T1 c T4 T1 T2 T3 h-1 h-1 h-1 h-1 h-1 h-1 T3 T2 a a b h to h-1 c h-1 c b h-1 h-1

  29. a a Deletion: case1.2 b b h to h-1 h-1 h-1 h-1 h-1 h-1 a b a b

  30. a c d c Deletion: case2.1.1 a b h to h-1 b d h h h-1 h-1 h-1 h-1 h-1 h-1 h-1 b c b a a d d c

  31. b a c b a a Deletion: case2.1.2 b h c h to h-1 h-1 c h h-1 h-1 h-1 h-1 b c a

  32. a a d Deletion: case2.2.1 c c a h to h-1 d h-1 h-1 h-1 h-1 h-1 h-1 h-1 d c c d a

  33. a a Deletion: case2.2.2 c c h to h-1 h-1 h-1 h-1 h-1 h-1 a c a c

  34. Summary • In all cases, except 2.2.2, deletion can be completed by a simple rotation/recoloring • In case 2.2.2, the height of the subtree reduces and so we need to proceed up the tree. • But in case 2.2.2 we only recolor nodes. • Thus, if we proceed up the tree then we only need to recolor. Eventually we would do a rotation.

  35. Insert and Delete Summary • In both insertion and deletion we need to make at most one rotation. • We might have to move up the tree but in doing so we only recolor nodes. • Time taken is O(log n).

  36. In what situations would you prefer red-black tree over AVL Tree?

  37. (a,b) Trees • A multiway search tree. • Each node has at least a and at most b children. • Root can have less than a children but it has at least 2 children. • All leaf nodes are at the same level. • Height h of (a,b) tree is at least logb n and at most loga n.

  38. B-Tree • Every leaf has the same depth. • Except the root, all nodes have between t and 2t children. • The root node has between 0 and 2t children. • Height is O(log n) • A large value of t is used for disk based storage.

  39. Examples Courtesy: Jan Manuch

  40. 47 32 71 93 Insert 65 Insertion Example

  41. 47 32 71 93 65 Insert 65 Insertion Example

  42. 47 32 71 93 65 Insert 65 Insert 82 Insertion Example

  43. 47 32 71 82 93 65 Insert 65 Insert 82 Insertion Example

  44. 47 65 93 32 71 82 93 65 71 Insert 65 Insert 82 Insertion Example

  45. 65 93 47 32 82 71 Insert 65 Insert 82 Insert 87 Insertion Example

  46. 93 65 47 32 82 71 87 Insert 65 Insert 82 Insert 87 Insertion Example

  47. 93 65 47 32 82 71 87 Insert 65 Insert 82 Insert 87 Insertion Example

  48. 87 65 47 32 93 82 71 Insert 65 Insert 82 Insert 87 Insertion Example

  49. 87 65 47 82 32 93 82 71 Delete 87 Deletion Example 1

  50. 87 65 47 51 32 93 82 71 65 51 Delete 71 Deletion Example 2 Replace with predecessor Attach predecessor’s child

More Related