1 / 12

COMP5712

COMP5712. Tutorial 4. Using an Array to Represent a Heap. When a binary tree is complete Can use level-order traversal to store data in consecutive locations of an array Enables easy location of the data in a node's parent or children

Download Presentation

COMP5712

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. COMP5712 Tutorial 4

  2. Using an Array to Represent a Heap • When a binary tree is complete • Can use level-order traversal to store data in consecutive locations of an array • Enables easy location of the data in a node's parent or children • Parent of a node at i is found at i/2(unless i is 1) • Children of node at i found at indices 2i and 2i + 1

  3. Using an Array to Represent a Heap (a) A complete binary tree with its nodes numbered in level order; (b) its representation as an array.

  4. Adding an Entry A revision of steps of addEntry to avoid swaps.

  5. Adding an Entry I An array representation of the steps in previous slide … continued →

  6. Adding an Entry II An array representation of the steps

  7. Removing the Root The steps to remove the entry in the root of the maxheap

  8. Heapsort • Possible to use a heap to sort an array • Place array items into a maxheap • Then remove them • Items will be in descending order • Place them back into the original array and they will be in order

  9. Heapsort A trace of heapsort (a – c)

  10. Heapsort A trace of heapsort (d – f)

  11. Heapsort A trace of heapsort (g – i)

  12. Heapsort A trace of heapsort (j – l)

More Related