Balanced search trees Dynamic sets • Search • Insert Balanced Search Trees • Delete • Maximum • Minimum 2-3-4 trees red-black trees • Successor(x) (find minimum element ≥ x) • Predecessor(x) (find maximum element ≤ x) This lecture: 2-3-4 trees, red-black trees References: Algorithms in Java (handout) Next time: Tiered vektor (not a binary search tree, but maintains a dynamic set). In two weeks time: Splay trees 2 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) 2-3-4 trees 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
2-3-4 trees Searching in a 2-3-4 tree 2-3-4 trees. Allow nodes to have multiple keys. Search. • Compare search key against keys in node. Perfect balance. Every path from root to leaf has same length. • Find interval containing search key Allow 1, 2, or 3 keys per node • Follow associated link (recursively) • 2-node: one key, 2 children • 3-node: 2 keys, 3 children • 4-node: 3 keys, 4 children K R K R smaller than K larger than R between K and R C E M O X B D F M O X N Q A D L S V Y Z F G J N Q A C E G J L S V Y Z 5 6 Searching in a 2-3-4 tree Predecessor and successor in a 2-3-4 tree Search. Where is the predecessor of L? • Compare search key against keys in node. And the successor of L? • Find interval containing search key • Follow associated link (recursively) Ex. Search for L K R K R between between K and R K and R C E M O X C E M O X smaller than M smaller than M N Q N Q A D F G J L S V Y Z A D F G J L S V Y Z found L found L 7 8
Insertion in a 2-3-4 tree Insertion in a 2-3-4 tree Insert. • Search to bottom for key. K R K R smaller than K M O X M O X C E C E Ex. Insert B smaller than C N Q N Q A D L S V Y Z A D L S V Y Z F G J F G J B not found 9 10 Insertion in a 2-3-4 tree Insertion in a 2-3-4 tree Insert. Insert. • Search to bottom for key. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 2-node at bottom: convert to 3-node K R K R smaller than K larger than R Ex. Insert X Ex. Insert B X C E M O C E M O U smaller than C larger than U N Q D F G J L S V Y Z A D F G J L N Q S T Y Z A B B fits here X not found 11 12
Insertion in a 2-3-4 tree Insertion in a 2-3-4 tree Insert. Insert. • Search to bottom for key. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node • 3-node at bottom: convert to 4-node K R K R larger than R smaller than K Ex. Insert X Ex. Insert H M O M O X C E U C E larger than W larger than E N Q A D L N Q S T A D L S V Y Z F G J X Y Z F G J X fits here 13 H not found 14 Insertion in a 2-3-4 tree Splitting a 4-node in a 2-3-4 tree C E G Idea: split the 4-node to make room Insert. • Search to bottom for key. C E • 2-node at bottom: convert to 3-node D F J A B • 3-node at bottom: convert to 4-node D F G J A B • 4-node at bottom: ?? H does fit here! K R smaller than K H does not fit here C E G Ex. Insert H X C E M O Problem: Doesn’t work if parent is a 4-node larger than E Solution 1: Split the parent (and continue splitting D F H J A B N Q A D F G J L S V Y Z while necessary). Solution 2: Split 4-nodes on the way down. H does not fit here! 15 16
Splitting 4-nodes in a 2-3-4 tree Insertion in a 2-3-4 tree Idea: split 4-nodes on the way down the tree. Insert. B B G • Ensures last node is not a 4-node. • Search to bottom for key. F J F G J • Transformations to split 4-nodes: • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node x y z v x y z v not a 4-node • 4-node at bottom: ?? K R B D B D G F J F G J not a 4-node Ex. Insert H x y z v x y z v Invariant. Current node is not a 4-node. M O X C E root D 4-node Consequence. Insertion at bottom is easy B D G since it's not a 4-node. N Q B G A D L S V Y Z F G J x y z v x y z v 17 18 Insertion in a 2-3-4 tree Insertion in a 2-3-4 tree Insert. Insert. • Search to bottom for key. • Search to bottom for key. • 2-node at bottom: convert to 3-node • 2-node at bottom: convert to 3-node • 3-node at bottom: convert to 4-node • 3-node at bottom: convert to 4-node • 4-node at bottom: ?? • 4-node at bottom: ?? K R K R Ex. Insert H Ex. Insert H X X C E G M O C E G M O N Q N Q A D F J L S V Y Z A D F L S V Y Z H J 19 20
Splitting 4-nodes in a 2-3-4 tree Splitting 4-nodes in a 2-3-4 tree Local transformations that work anywhere in the tree. Local transformations that work anywhere in the tree Ex. Splitting a 4-node attached to a 2-node Ex. Splitting a 4-node attached to a 3-node D D Q D H D H Q W K Q W K K Q W W K A-C A-C A-C E-G A-C E-G E-J L-P R-V X-Z E-J L-P R-V X-Z I-J L-P R-V X-Z I-J L-P R-V X-Z could be huge unchanged could be huge unchanged 21 22 Splitting 4-nodes in a 2-3-4 tree Insertion 2-3-4 trees Insert G Local transformations that work anywhere in the tree. Insert U E I R E R E R Splitting a 4-node attached to a 4-node never happens when we split nodes on H N S U the way down the tree. A B C S U A B C H I N S A B C H I N Insert G Invariant. Current node is not a 4-node. Split Insert U Insert G Insert T I I Split E I R E R E R A B C G H N S U N N A B C G H S U A B C G H S T U Insert T 23 24
Deletions in 2-3-4 trees Deletions in 2-3-4 trees Delete minimum: Delete minimum: • minimum always in leftmost leaf • minimum always in leftmost leaf • If 3- or 4-node: delete key • If 3- or 4-node: delete key K R K R Ex. Delete minimum Ex. Delete minimum M O M O C E X C E X A is minimum Delete A A B D F G J L N Q S V Y Z B D F G J L N Q S V Y Z 25 26 Deletions in 2-3-4 trees Deletions in 2-3-4 trees Delete minimum: Idea: On the way down maintain the invariant that current node is not a 2-node. • minimum always in leftmost leaf • Child of root and root is a 2-node: B A B C • If 3- or 4-node: delete key B C or A C y x z w • 2-node?? A C D E A B D E K R y x z w • on the way down: Ex. Delete minimum z w r s x z w r s x y y C F G B F G or C E M O X A C D E A B D E B D E D E Delete B? x y z w r s x z w r s y B D L N Q S V Y Z F G J A C A B C 27 28 y x z w y x z w
Deletions in 2-3-4 trees Deletions in 2-3-4 trees Delete minimum: Delete minimum: • minimum always in leftmost leaf • minimum always in leftmost leaf • If 3- or 4-node: delete key • If 3- or 4-node: delete key • 2-node: split/merge on way down. • 2-node: split/merge on way down. K R K R Ex. Delete minimum Ex. Delete minimum not a 2-node M O M O C E X E X 2-node B D F G J L N Q S V Y Z B C D F G J L N Q S V Y Z 29 30 Deletions in 2-3-4 trees Deletions in 2-3-4 trees Delete minimum: Delete: • minimum always in leftmost leaf • If 3- or 4-node: delete key • 2-node: split/merge on way down. K R Ex. Delete minimum K R E M O X M O C E X L N Q S V Y Z C D F G J B D L N Q S V Y Z F G J 31 32
Deletions in 2-3-4 trees Deletions in 2-3-4 trees Delete: Delete: • During search maintain invariant that current node is not a 2-node • During search maintain invariant that current node is not a 2-node • If key is in a leaf: delete key K R K R C E M O X C E M O X B D F G J L N Q S V Y Z B D F G J L N Q S V Y Z 33 34 Deletions in 2-3-4 trees Deletions in 2-3-4 trees Delete: Delete: • During search maintain invariant that current node is not a 2-node • During search maintain invariant that current node is not a 2-node • If key is in a leaf: delete key • If key is in a leaf: delete key • Key not in leaf: replace with successor (always leaf in subtree) and delete • Key not in leaf: replace with successor (always leaf in subtree) and delete successor from leaf. successor from leaf. K R K R Ex. Delete K M O M O C E X C E X B D L N Q S V Y Z B D L N Q S V Y Z F G J F G J 35 36
Recommend
More recommend