1 / 8

Definitions

Definitions. Priority Queue – A data structure with quick access to its smallest (or largest) key. Heap – A binary tree that is complete and satisfies the heap condition

gates
Download Presentation

Definitions

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. Definitions • Priority Queue – A data structure with quick access to its smallest (or largest) key. • Heap – A binary tree that is complete and satisfies the heap condition • Complete: All levels of the tree are filled except for the last which may have consecutive nodes on the right of the last level empty. • Heap Condition: The key value of every node is less than (greater than) that of its children nodes. A min-heap satisfies the less than condition; a max-heap satisfies the greater than condition.

  2. Implementation of lists/Trees with arrays • Linked Lists • Use indices for next pointers instead of object references. • Free list creates a chain of unused entries. • Trees • Relationship between parent and child nodes • Node pointers are not necessary with complete trees because the indices can be calculated.

  3. Heap ImplementationFull Example in the Text • Because heaps use complete binary trees, they are usually implemented with arrays. • Operations are: Fill, Insert, Remove, Trickle-up, Trickle-down • Another operation Change can be implemented with a second data structure in which entries point to heap locations.

  4. Trickle Down/Trickle Up O(lgN)Assume Min-heap Trickle Down (Position) If position is a leaf node then ReturnIf keyatPosition < smallestChildKey then Return Swap Entry with smallestChildKeyTrickleDown(smllestChildPosition) Trickle Up(Position) IfPosition = Root or keyatPosition > ParentKey then Return SwapEntry with Parent TrickleUp(ParentEntry)

  5. Insert and Remove O(lgN)Assume min-heap Insert(newEntry) If heap is full then Return falseIncrease heapSize by one Instantiate new node and place in lastEntryPosition of heap TrickleUp(lastEntryPosition) and Returntrue Remove() If heap is empty then Returnnull Save root entry Store lastHeapEntry in rootPpositionDecrementheapSize TrickleDown(rootPosition) Return Saved root entry

  6. Heap with pointers • Combine heap with a hash table to enable heap entries to be quickly found • Hashing is a later topic • Finds occur faster than lg(N) • Adjust priority If key value is increasing, trickle down. If key value is decreasing, trickle up. • Note: trickle up and down must also maintain the hash entries

  7. Create (O(N)) and Heap Sort (O(NlgN)Note that create is faster than sortingSort complexity: O(n) steps of O(lgN) each CreatePosition = halfway through the listWhile Position is not at the top of the heapTrickle down entry at position to end of listDecrementPosition by one entry SortCreate Heap in backwards order from the desired sort orderwhile the heap is not empth RemoverootEntry Place rootEntry at lastPosition that was just vacated Note: Heap Sort sorts in place. How does it compare?

  8. Why is Heap Create O(n) • N/4 nodes can have at most 1 trickle • N/8 nodes can have at most 2 trickles • N/16 nodes can have at most 3 trickels … • This totals to:N/4 + 2*N/8 + 3*N/16 + … trickles ≈ N

More Related