1 / 20

Tables and Priority Queues

Tables and Priority Queues. Chapter 11. The ADT Table. Selecting an Implementation. A Sorted Array-Based Implementation of the ADT Table. A Binary Search Tree Implementation of the ADT Table. The ADT Priority Queue: A Variation of the ADT Table. Heaps

mlorenzo
Download Presentation

Tables and Priority Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tables and Priority Queues Chapter 11

  2. Chapter 11 -- Tables and Priority Queues

  3. The ADT Table Chapter 11 -- Tables and Priority Queues

  4. Selecting an Implementation Chapter 11 -- Tables and Priority Queues

  5. A Sorted Array-Based Implementation of the ADT Table Chapter 11 -- Tables and Priority Queues

  6. A Binary Search Tree Implementation of the ADT Table Chapter 11 -- Tables and Priority Queues

  7. The ADT Priority Queue: A Variation of the ADT Table Chapter 11 -- Tables and Priority Queues

  8. Heaps • A heap is an ADT that is similar to a binary search tree, although it differs from a binary search tree in two significant ways • A heap is ordered in a much weaker sense • Heaps are always complete binary trees. • So, • A heap is a complete binary tree • 1) that is empty or • 2) Root contains a search key that is greater than or equal to the search key of each of its children, and Whose root has heaps as its subtrees Chapter 11 -- Tables and Priority Queues

  9. Here is the UML diagram for a heap class. Chapter 11 -- Tables and Priority Queues

  10. Because a heap is a complete binary tree, you can use an array-based implementation of a binary tree, as you saw in Chapter 10. Chapter 11 -- Tables and Priority Queues

  11. A Heap Implementation of the ADT Priority Queue • We need the following members: • items: an array of heap items • size: an integer equal to the number of items in the heap. • Now lets talk about the following functions: • heapDelete • heapInsert Chapter 11 -- Tables and Priority Queues

  12. heapDelete • where is the largest element? • rootItem = items[0]; • Now we are left with two disjoint heaps • So we have to fix it. Chapter 11 -- Tables and Priority Queues

  13. Copy the last element up • Items[0] = items[size-1]; • size--; Chapter 11 -- Tables and Priority Queues

  14. Now fix it (trickle down) Chapter 11 -- Tables and Priority Queues

  15. heapInsert • add new item to the end • items[size] = newItem; • size++; • Now fix the heap (float the new item up to the correct location) Chapter 11 -- Tables and Priority Queues

  16. Heapsort • Strategy • Transforms the array into a heap • Removes the heap's root (the largest element) by exchanging it with the heap’s last element • Transforms the resulting semiheap back into a heap Chapter 11 -- Tables and Priority Queues

  17. Heapsort • Compared to mergesort • Both heapsort and mergesort are O(n * log n) in both the worst and average cases • However, heapsort does not require a second array • Compared to quicksort • Quicksort is O(n * log n) in the average case • It is generally the preferred sorting method, even though it has poor worst-case efficiency : O(n2) Chapter 11 -- Tables and Priority Queues

  18. Heapsort Chapter 11 -- Tables and Priority Queues

  19. Summary Chapter 11 -- Tables and Priority Queues

  20. Chapter 11 -- Tables and Priority Queues

More Related