1 / 66

2-3-4 Trees and Red-Black Trees

2-3-4 Trees and Red-Black Trees. Gordon College Prof. Brinton. Regular Binary Trees. Insertion sequence: 9, 5, 20, 3, 7, 15, 75, 6, 12, 17, 35, 100, 18, 25, 40. Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40. Balanced Trees.

barbra
Download Presentation

2-3-4 Trees and 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. 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton

  2. Regular Binary Trees Insertion sequence: 9, 5, 20, 3, 7, 15, 75, 6, 12, 17, 35, 100, 18, 25, 40 Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40

  3. Balanced Trees • Need a new search-tree structure - a balanced binary search tree • Maintains balanced node when adding or removing them Extra time needed at insert and remove • Guarantees at worst O(log n) search time We start with 2-3-4 trees… - Perfectly balanced - Difficult and inefficient to implement

  4. 2-3-4 Trees Definitions 2-node - a data value and pointers to 2 subtrees 3-node - two data values and pointers to 3 subtrees 4-node - three data values and pointers to 4 subtrees A<B A<B<C

  5. 2-3-4 Tree What is the basic 2-3-4 search algorithm? 2-3-4 trees have search tree properties

  6. Inserting into a 2-3-4 Tree C Insert begins with a single node and adds elements until it is full Insert another item 1. splitthe 4 node using the median value as the parent (promoting the median value to the parent level) 2. Insert the new item (inserted based on the BST rules for insertion) (always insert node in leaf of tree)

  7. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  8. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  9. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  10. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  11. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  12. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  13. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25) Proactive top-down approach to splitting a 4-node

  14. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25)

  15. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  16. 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  17. Another example Keys: A S E R C H I N G X What would the 2-3-4 tree look like after inserting this set of keys?

  18. Another example Keys: A S E R C H I N G X A

  19. Another example Keys: A S E R C H I N G X A S

  20. Another example Keys: A S E R C H I N G X A E S

  21. Another example Keys: A S E R C H I N G X E R S A

  22. Another example Keys: A S E R C H I N G X E R S A C

  23. Another example Keys: A S E R C H I N G X E H R S A C

  24. Another example Keys: A S E R C H I N G X E R H I S A C

  25. Another example Keys: A S E R C H I N G X E R H I N S A C

  26. Another example Keys: A S E R C H I N G X E I R S G H N A C

  27. Another example Keys: A S E R C H I N G X I E R S X G H N A C

  28. 2-3-4 Tree Facts • With N elements, the max number of nodes visited during the search for an element is int(log2 n) + 1 • Inserting an element into a tree with n elements requires splitting no more than int(log2 n) + 1 4-nodes (often far fewer) • Problem: allocated a large amount of wasted space 3n + 1 unused pointers (n - nodes) 28 wasted pointers

  29. 2-3-4 Warmup Exercise Create a 2-3-4 tree from the following sequence of numbers: 45 6 23 5 78 9 10 11 48 99 12 55

  30. 2-3-4 Warmup Exercise Create a 2-3-4 tree from the following sequence of numbers: 45 6 23 5 78 9 10 11 48 99 12 55

  31. Red-Black Trees • Designed to represent 2-3-4 tree without the additional link overhead. • Colors are used to represent the 3-node and 4-node. Red-Black trees are simple binary trees with a color indicator that is used to maintain certain properties - these properties keep the tree balanced.

  32. N I H I N N H Red-Black Nodes 2-nodes simple binary node (black node) 4-nodes center value becomes the parent (black) with outside values becoming the children (red)

  33. A B A B or B A Red-Black Nodes 3-nodes Note: Red-black trees are not unique However, the corresponding 2-3-4 tree is unique

  34. N I N H Red-Black Nodes A Use color grouping of nodes to indicate the corresponding nodes in the 2-3-4 tree B B A

  35. Converting a 2-3-4 Tree to Red-Black Tree Example Top-down conversion algorithm: (start at the root) Apply red-black tree representation to each node Repeat for next level…

  36. Converting a 2-3-4 Tree to Red-Black Tree Example

  37. I H I N N H Converting a 2-3-4 Tree to Red-Black Tree Example

  38. Converting a 2-3-4 Tree to Red-Black Tree Example

  39. Converting a 2-3-4 Tree to Red-Black Tree Example

  40. Converting a 2-3-4 Tree to Red-Black Tree Example How could this be different?

  41. Red-Black Tree Properties • The Root of a red-black tree is BLACK • A RED parent never has a RED child – there are never 2 RED nodes in succession • Every path from the root to an empty subtree (NULL pointer) has the same number of BLACK nodes. a BLACK node corresponds to a level change in the 2-3-4 tree

  42. Inserting Nodes Guidelines • Maintain root as BLACK node • Enter a new node as a RED node – since each new node enters a 2-node or a 3-node. • Whenever it results in two RED nodes in succession – rotate nodes to create a BLACK parent. • When scanning down a path to find insertion location – split any 4-node.

  43. Inserting Nodes Inserting a 2 3-node 5 12

  44. 5 12 2 Inserting Nodes Inserting a 2 3-node 4-node 5 12

  45. Inserting Nodes Inserting a 14 5 12

  46. Inserting Nodes Inserting a 14 5 12 5 12 14

  47. Inserting Nodes Inserting a 14 5 12 12 5 14 5 12 Single left rotation 14

  48. Inserting Nodes Inserting a 10 5 12

  49. Inserting Nodes Inserting a 10 5 12 5 12 10

  50. Inserting Nodes Inserting a 10 5 12 10 5 12 5 12 right - left rotation 10

More Related