B-Tree and Heaps Today’s announcements
I HW3 out, due Nov 15, 23:59
I MT2 Nov 7, 19:00-21:00 WOOD 2 Today’s Plan
I B-Tree wrap
I Priority queues Warm up: If m = 128, then a B-Tree of height 4 must contain at least 1. 300 items 2. 3,000 items 3. 30,000 items 4. 300,000 items 5. 3,000,000 items 6. 30,000,000 items
1 / 10 Thinking about B-Trees
I Remove is fast if leaf doesn’t underflow or we can take from a sibling. Merging and propagation take more time.
I Insert is fast if leaf doesn’t overflow. (Could we give to a sibling?) Splitting and propagation take more time.
I Propagation is rare if m is large (Why?)
I Repeated insertions and deletion can cause thrashing
I Range queries (i.e., findBetween(key1, key2)) are fast because of sibling pointers.
2 / 10 B-Trees in practice Multiple B-Trees can index the same data records. Employee Name bat emu kea
ant auk bee boa elk ewe fox gnu goa kit owl yak
Employee ID 15 20 30
10 11 12 16 17 22 25 26 31 32 33 36
disk Name: auk Name: yak ID: 16 ID: 22 Food: fish Food: grass
3 / 10 A Tree by Any Other Name...
I B-Trees with m = 3 are called 2-3 trees
I B-Trees with m = 4 are called 2-3-4 trees
Why would we ever use these?
4 / 10 Back to Queues
I Applications
I ordering CPU jobs I simulating events I picking the next search site I But we don’t want FIFO ...
I short jobs should go first I earliest (simulated time) events should go first I most promising sites should be searched first
5 / 10 Priority Queue ADT
I Priority Queue operations
I create dog 14 I destroy insert ant 8 deleteMin I insert bee 2 emu 6 I removeMin I is empty cat 4
I Priority Queue property: For two elements in the queue, x and y, if x has a lower priority value than y, x will be dequeued before y.
6 / 10 Applications of the Priority Q
I Hold jobs for a printer in order of length
I Store packets on network routers in order of urgency
I Simulate events
I Select symbols for compression
I Sort numbers
I Anything greedy: an algorithm that makes the “locally best choice” at each step
7 / 10 Priority Q Data Structures
I Unsorted list
I insert time:
I removeMin time:
I Sorted list
I insert time:
I removeMin time:
8 / 10 Binary Heap Priority Q Data Structure
What properties does this structure have?
2
4 5
7 6 10 8
13 9 12 14 11
9 / 10 Nifty Storage Trick
Navigation using indices:
I left child(i) = 0 I right child(i) = 2
I parent(i) = 1 2 4 5 I root = 3 4 5 6 I next free position = 7 6 10 8
13 9 12 14 11 7 8 9 10 11
0123456789 10 11 12 2 4 5 7 6 10 8 13 9 12 14 11
10 / 10