welcome
play

Welcome Inge Li Grtz. Reverse teaching and discussion of exercises: - PowerPoint PPT Presentation

Welcome Inge Li Grtz. Reverse teaching and discussion of exercises: 3 teaching assistants 02110 8.00-9.15 Group work 9.15-9.45 Discussions of your solutions in class Inge Li Grtz 10.00-11.15 Lecture 11.15-11.45 Work


  1. Welcome • Inge Li Gørtz. • Reverse teaching and discussion of exercises: • 3 teaching assistants 02110 • 8.00-9.15 Group work • 9.15-9.45 Discussions of your solutions in class Inge Li Gørtz • 10.00-11.15 Lecture • 11.15-11.45 Work on exercises in the new material • 11.45-12.00 Round up • Weekly assignments (You have to get 40 points + pass 2 implementation exercises in order to be able to attend the written exam). • Prerequisites: 02105/02326 Algorithms and Data Structures I Thank you to Kevin Wayne for inspiration to slides � 2 Balanced search trees Dynamic sets • Search • Insert Balanced Search Trees • Delete • Maximum 2-3-4 trees • Minimum red-black trees • Successor(x) (find minimum element ≥ x) • Predecessor(x) (find maximum element ≤ x) References: CLRS Chapter 13, Algorithms in Java This lecture: 2-3-4 trees, red-black trees (handout) Next time: Splay trees � 4

  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. � 5 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 Ex. Search for L • 4-node: 3 keys, 4 children K R K R smaller than K larger than R between between K and R K and R C E M O X B D F M O X smaller than M N Q A D F G J L S V Y Z A C E L N Q G J S V Y Z found L � 7 � 8

  3. 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 X X C E M O C E M O 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 M O X M O C E C E 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

  4. 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 X C E M O U C E M O larger than W larger than E N Q A D L N Q S T X Y Z A D L S V Y Z F G J 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 Insert. Idea: split the 4-node to make room • 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: ?? K R H does fit here! smaller than K H does not fit here C E G Ex. Insert H M O X C E Problem: Doesn’t work if parent is a 4-node larger than E D F H J Solution 1: Split the parent (and continue splitting 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

  5. 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. X C E M O 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 M O X M O X C E G C E G 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

  6. 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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 W K Q 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 S U A B C H I N 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

  7. 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 y z w r s x 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 y z w r s B D F G J L N Q S V Y Z A C A B C � 27 � 28 y x z w y x z w

  8. 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 C D F G J L N Q S V Y Z B D F G J L N Q S V Y Z � 31 � 32

  9. 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 L N Q S V Y Z B D L N Q S V Y Z F G J F G J � 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 F G J L N Q S V Y Z B D F G J L N Q S V Y Z � 35 � 36

Recommend


More recommend