1 / 26

Objectives

Heap. Objectives. Upon completion you will be able to: Define and implement heap structures Understand the operation and use of the heap ADT Design and implement selection applications using a heap Design and implement priority queues using a heap. Basic Concepts.

Download Presentation

Objectives

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. Heap Objectives • Upon completion you will be able to: • Define and implement heap structures • Understand the operation and use of the heap ADT • Design and implement selection applications using a heap • Design and implement priority queues using a heap Data Structures: A Pseudocode Approach with C, Second Edition

  2. Basic Concepts • A heap is a binary tree whose left and right subtrees have values less than their parents. We begin with a discussion of the basic heap structure and its two primary operations, reheap up and reheap down. • Definition • Maintenance Operations Data Structures: A Pseudocode Approach with C, Second Edition

  3. Basic Concepts • Definition • A heap is a binary tree structure with the following properties: • The tree is complete or nearly complete. • The key value of each node is greater than or equal to the key value in each of its descendents. • The root of a heap is guaranteed to hold the largest node in the tree; its subtrees contain data that have lesser values. • Heaps are often implemented in an array rather than a linked list. Data Structures: A Pseudocode Approach with C, Second Edition

  4. Sometimes this structure is called a max-heap. A min-heap is to create a heap which the key value in a node is less than the key values in all of its subtrees. Data Structures: A Pseudocode Approach with C, Second Edition

  5. Data Structures: A Pseudocode Approach with C, Second Edition

  6. Data Structures: A Pseudocode Approach with C, Second Edition

  7. The reheap up operation reorders a “broken” heap by floating the last element up the tree until it is in its correct location in the heap. Data Structures: A Pseudocode Approach with C, Second Edition

  8. Data Structures: A Pseudocode Approach with C, Second Edition

  9. Reheap Down Operation reorders a “broken” heap by pushing the root down the tree until it is in its correct position in the heap. Data Structures: A Pseudocode Approach with C, Second Edition

  10. Data Structures: A Pseudocode Approach with C, Second Edition

  11. Heap Implementation • Heaps are usually implemented in an array structure. In this section we discuss and develop five heap algorithms. • Reheap Up • Reheap Down • Build a Heap • Insert a Node into a Heap • Delete a Node from a Heap Data Structures: A Pseudocode Approach with C, Second Edition

  12. Heap Implementation The relationship between a node and its children is fixed and can be calculated as shown below: 1. For a node located at index i, its children are found at: a. Left child: 2i +1 b. Right child: 2i +2 2. The parent of a node located at index i is located at (i -1)/2 3. Given the index for a left child, j, its right sibling, if any, is found at j + 1. Conversely, given the index for a right child, k, its left sibling, which must exist, is found at k - 1. 4. Given the size, n, of a completed heap, the location of the first leaf is (n/2) 5. Given the location of the first leaf element, the location of the last nonleaf element is one less. Data Structures: A Pseudocode Approach with C, Second Edition

  13. Data Structures: A Pseudocode Approach with C, Second Edition

  14. Data Structures: A Pseudocode Approach with C, Second Edition

  15. Data Structures: A Pseudocode Approach with C, Second Edition

  16. (continued) Data Structures: A Pseudocode Approach with C, Second Edition

  17. Data Structures: A Pseudocode Approach with C, Second Edition

  18. Data Structures: A Pseudocode Approach with C, Second Edition

  19. Data Structures: A Pseudocode Approach with C, Second Edition

  20. Data Structures: A Pseudocode Approach with C, Second Edition

  21. Data Structures: A Pseudocode Approach with C, Second Edition

  22. Data Structures: A Pseudocode Approach with C, Second Edition

  23. Data Structures: A Pseudocode Approach with C, Second Edition

  24. Data Structures: A Pseudocode Approach with C, Second Edition

  25. Data Structures: A Pseudocode Approach with C, Second Edition

  26. Data Structures: A Pseudocode Approach with C, Second Edition

More Related