b tree and heaps
play

B-Tree and Heaps Todays announcements HW3 out, due Nov 15, 23:59 - PowerPoint PPT Presentation

B-Tree and Heaps Todays announcements HW3 out, due Nov 15, 23:59 MT2 Nov 7, 19:00-21:00 WOOD 2 Todays Plan B-Tree wrap Priority queues Warm up: If m = 128, then a B-Tree of height 4 must contain at least 1. 300 items 2.


  1. B-Tree and Heaps Today’s announcements ◮ HW3 out, due Nov 15, 23:59 ◮ MT2 Nov 7, 19:00-21:00 WOOD 2 Today’s Plan ◮ B-Tree wrap ◮ 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

  2. Thinking about B-Trees ◮ Remove is fast if leaf doesn’t underflow or we can take from a sibling. Merging and propagation take more time. ◮ Insert is fast if leaf doesn’t overflow. (Could we give to a sibling?) Splitting and propagation take more time. ◮ Propagation is rare if m is large (Why?) ◮ Repeated insertions and deletion can cause thrashing ◮ Range queries (i.e., findBetween(key1, key2) ) are fast because of sibling pointers. 2 / 10

  3. 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

  4. A Tree by Any Other Name... ◮ B-Trees with m = 3 are called 2-3 trees ◮ B-Trees with m = 4 are called 2-3-4 trees Why would we ever use these? 4 / 10

  5. Back to Queues ◮ Applications ◮ ordering CPU jobs ◮ simulating events ◮ picking the next search site ◮ But we don’t want FIFO ... ◮ short jobs should go first ◮ earliest (simulated time) events should go first ◮ most promising sites should be searched first 5 / 10

  6. Priority Queue ADT ◮ Priority Queue operations ◮ create dog 14 ◮ destroy deleteMin insert ant 8 ◮ insert bee 2 emu 6 ◮ removeMin ◮ is empty cat 4 ◮ 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

  7. Applications of the Priority Q ◮ Hold jobs for a printer in order of length ◮ Store packets on network routers in order of urgency ◮ Simulate events ◮ Select symbols for compression ◮ Sort numbers ◮ Anything greedy : an algorithm that makes the “locally best choice” at each step 7 / 10

  8. Priority Q Data Structures ◮ Unsorted list ◮ insert time: ◮ removeMin time: ◮ Sorted list ◮ insert time: ◮ removeMin time: 8 / 10

  9. 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

  10. Nifty Storage Trick Navigation using indices: ◮ left child( i ) = 0 ◮ right child( i ) = 2 ◮ parent( i ) = 1 2 4 5 ◮ root = 3 4 5 6 ◮ next free position = 7 6 10 8 13 9 12 14 11 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 12 2 4 5 7 6 10 8 13 9 12 14 11 10 / 10

Recommend


More recommend