balanced search trees
play

Balanced Search Trees 2-3-4 trees red-black trees References: - PowerPoint PPT Presentation

Balanced Search Trees 2-3-4 trees red-black trees References: Algorithms in Java (handout) Balanced search trees Dynamic sets Search Insert Delete Maximum Minimum Successor(x) (find minimum element x)


  1. Balanced Search Trees 2-3-4 trees red-black trees References: Algorithms in Java (handout)

  2. Balanced search trees Dynamic sets • Search • Insert • Delete • Maximum • Minimum • Successor(x) (find minimum element ≥ x) • Predecessor(x) (find maximum element ≤ x) This lecture: 2-3-4 trees, red-black trees Next time: Tiered vektor (not a binary search tree, but maintains a dynamic set). In two weeks time: Splay trees 2

  3. Dynamic set implementations Worst case running times Implementation search insert delete minimum maximum successor predecessor linked lists O(n) O(1) O(1) O(n) O(n) O(n) O(n) ordered array O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) BST O(h) O(h) O(h) O(h) O(h) O(h) O(h) In worst case h=n. In best case h= log n (fully balanced binary tree) Today: How to keep the trees balanced. 3

  4. 2-3-4 trees

  5. 2-3-4 trees 2-3-4 trees. Allow nodes to have multiple keys. Perfect balance. Every path from root to leaf has same length. Allow 1, 2, or 3 keys per node • 2-node: one key, 2 children • 3-node: 2 keys, 3 children • 4-node: 3 keys, 4 children K R smaller than K larger than R between K and R B D F M O X N A C E Q G J L S V Y Z 5

  6. Searching in a 2-3-4 tree Search. • Compare search key against keys in node. • Find interval containing search key • Follow associated link (recursively) K R M O C E X Q N A D L S V Y Z F G J 6

  7. Searching in a 2-3-4 tree Search. • Compare search key against keys in node. • Find interval containing search key • Follow associated link (recursively) Ex. Search for L K R between K and R M O C E X smaller than M Q N A D L S V Y Z F G J found L 7

  8. Predecessor and successor in a 2-3-4 tree Where is the predecessor of L? And the successor of L? K R between K and R M O C E X smaller than M Q N A D L S V Y Z F G J found L 8

  9. Insertion in a 2-3-4 tree K R X M O C E N Q A D L S V Y Z F G J 9

  10. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. K R smaller than K X M O C E Ex. Insert B smaller than C N Q A D L S V Y Z F G J B not found 10

  11. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. • 2-node at bottom: convert to 3-node K R smaller than K Ex. Insert B X M O C E smaller than C N Q D L S V Y Z F G J A B B fits here 11

  12. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. • 2-node at bottom: convert to 3-node K R larger than R Ex. Insert X M O C E U larger than U A D L N Q S T Y Z F G J X not found 12

  13. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node K R larger than R Ex. Insert X M O C E U larger than W A D L N Q S T F G J X Y Z X fits here 13

  14. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node K R smaller than K Ex. Insert H X M O C E larger than E N Q A D L S V Y Z F G J H not found 14

  15. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node • 4-node at bottom: ?? K R smaller than K Ex. Insert H X M O C E larger than E N Q A D L S V Y Z F G J H does not fit here! 15

  16. Splitting a 4-node in a 2-3-4 tree C E G Idea: split the 4-node to make room C E D F J A B D F G J A B H does fit here! H does not fit here C E G Problem: Doesn’t work if parent is a 4-node H J Solution 1: Split the parent (and continue splitting D F A B while necessary). Solution 2: Split 4-nodes on the way down. 16

  17. Splitting 4-nodes in a 2-3-4 tree Idea: split 4-nodes on the way down the tree. B B G • Ensures last node is not a 4-node. F J F G J • Transformations to split 4-nodes: x y z v x z y v B D B D G F J F G J x y z v x y z v Invariant. Current node is not a 4-node. root D Consequence. Insertion at bottom is easy B D G since it's not a 4-node. B G x y z v x y z v 17

  18. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node not a 4-node • 4-node at bottom: ?? K R not a 4-node Ex. Insert H X M O C E 4-node N Q A D L S V Y Z F G J 18

  19. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node • 4-node at bottom: ?? K R Ex. Insert H X M O C E G N Q A D F J L S V Y Z 19

  20. Insertion in a 2-3-4 tree Insert. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node • 4-node at bottom: ?? K R Ex. Insert H X M O C E G N Q A D F L S V Y Z H J 20

  21. Splitting 4-nodes in a 2-3-4 tree Local transformations that work anywhere in the tree. Ex. Splitting a 4-node attached to a 2-node D D Q W K Q W K A-C A-C E-J L-P R-V X-Z E-J L-P R-V X-Z could be huge unchanged 21

  22. Splitting 4-nodes in a 2-3-4 tree Local transformations that work anywhere in the tree Ex. Splitting a 4-node attached to a 3-node D H D H Q W K Q W K A-C E-G A-C E-G I-J L-P R-V X-Z I-J L-P R-V X-Z could be huge unchanged 22

  23. Splitting 4-nodes in a 2-3-4 tree Local transformations that work anywhere in the tree. Splitting a 4-node attached to a 4-node never happens when we split nodes on the way down the tree. Invariant. Current node is not a 4-node. 23

  24. Insertion 2-3-4 trees Insert G Insert U E I R E R E R H N S U A B C S U S A B C H I N A B C H I N Insert G Insert U Insert G Split Insert T I I Split E I R E R E R N A B C G H S U N N A B C S U A B C S T U G H G H Insert T 24

  25. Deletions in 2-3-4 trees Delete minimum: • minimum always in leftmost leaf • If 3- or 4-node: delete key K R Ex. Delete minimum M O C E X A is minimum D L N Q S V Y Z A B F G J 25

  26. Deletions in 2-3-4 trees Delete minimum: • minimum always in leftmost leaf • If 3- or 4-node: delete key K R Ex. Delete minimum M O C E X Delete A B D L N Q S V Y Z F G J 26

  27. Deletions in 2-3-4 trees Delete minimum: • minimum always in leftmost leaf • If 3- or 4-node: delete key • 2-node?? K R Ex. Delete minimum M O C E X Delete B? B D L N Q S V Y Z F G J 27

  28. Deletions in 2-3-4 trees Idea: On the way down maintain the invariant that current node is not a 2-node. • Child of root and root is a 2-node: B A B C B C or A C y x z w A C D E A B D E y x z w • on the way down: z w r s z w r s x x y y C F G B F G or A C D E A B D E B D E D E z w r s z w r s x y x y A C A B C 28 y x z w y x z w

  29. Deletions in 2-3-4 trees Delete minimum: • minimum always in leftmost leaf • If 3- or 4-node: delete key • 2-node: split/merge on way down. K R Ex. Delete minimum not a 2-node M O C E X 2-node B D L N Q S V Y Z F G J 29

  30. Deletions in 2-3-4 trees Delete minimum: • minimum always in leftmost leaf • If 3- or 4-node: delete key • 2-node: split/merge on way down. K R Ex. Delete minimum M O E X L N Q S V Y Z B C D F G J 30

  31. Deletions in 2-3-4 trees Delete minimum: • minimum always in leftmost leaf • If 3- or 4-node: delete key • 2-node: split/merge on way down. K R Ex. Delete minimum M O E X L N Q S V Y Z C D F G J 31

  32. Deletions in 2-3-4 trees Delete: K R M O C E X B D L N Q S V Y Z F G J 32

  33. Deletions in 2-3-4 trees Delete: • During search maintain invariant that current node is not a 2-node K R M O C E X B D L N Q S V Y Z F G J 33

  34. Deletions in 2-3-4 trees Delete: • During search maintain invariant that current node is not a 2-node • If key is in a leaf: delete key K R M O C E X B D L N Q S V Y Z F G J 34

  35. Deletions in 2-3-4 trees Delete: • During search maintain invariant that current node is not a 2-node • If key is in a leaf: delete key • Key not in leaf: replace with successor (always leaf in subtree) and delete successor from leaf. K R M O C E X B D L N Q S V Y Z F G J 35

  36. Deletions in 2-3-4 trees Delete: • During search maintain invariant that current node is not a 2-node • If key is in a leaf: delete key • Key not in leaf: replace with successor (always leaf in subtree) and delete successor from leaf. K R Ex. Delete K M O C E X B D L N Q S V Y Z F G J 36

  37. Deletions in 2-3-4 trees Delete: • During search maintain invariant that current node is not a 2-node • If key is in a leaf: delete key • Key not in leaf: replace with successor (always leaf in subtree) and delete successor from leaf. K R Ex. Delete K • Find successor M O C E X B D L N Q S V Y Z F G J 37

Recommend


More recommend