1 / 8

ITEC324 Principle of CS III

Learn the definition of heaps and their characteristics, including complete binary trees and the heap condition. Explore basic operations like removal and key change, as well as insertion and heapsort. Discover the efficiency of heapsort and the recursive approach for transforming arrays into heaps.

jimmiej
Download Presentation

ITEC324 Principle of CS III

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. Chapter 12 (Lafore’s Book) Heaps Hwajung Lee ITEC324 Principle of CS III

  2. Heaps • Definition • A heap is a binary tree with these characteristics: • It’s complete. This means it’s completely filled in, reading from left to right across each row, although the last row need not be full. (Example) fig12.1 in the page 580 • Each node in a heap satisfies the heap condition, which states that every node’s key is larger than (or equal to) the keys of its descendants. (Example) fig12.2 in the page 581

  3. Heaps:Removal • Remove the root. • Move the last node into the root. • Trickle (the terms bubble or percolate are also used) the last node down until it’s below a larger node and above a smaller one.  See Remove method

  4. Basic Operations & Key Change • Basic Operations • See Trickle Down method • See Trickle Up method • See Key Change method

  5. Insertion • the node to be inserted is placed in the first open position at the end of the array • Trickle the inserted node up until it’s below a larger node and above a smaller one.  See Insert method

  6. Heapsort [Basic Idea] • insert all the unordered items into a heap using the normal insert() routine. • repeated application of the remove() routine will then remove the items in sorted order. for (j = 0; j < size; j++) theHeap.insert(anArray[j]); //from unsorted array for (j = 0; j < size; j++) anArray[j] = theHeap.remove(); // to sorted array

  7. Efficiency • Efficiency of the heapsort • Expected case: O(N*logN) • Worst case: O(N*logN) • cf. Quicksort • Expected case: O(N*logN) • Worst case: O(N2)

  8. Recursive Approach [Recursive Approach] • Observation: Two correct subheaps make a correct heap. (Example) fig12.8 in the page 602 • See Heapify method • Transform array into heap

More Related