Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 Implementation Full 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 Return If keyatPosition < smallestChildKey then Return Swap Entry with smallestChildKey TrickleDown(smllestChildPosition) Trickle Up(Position) I f Position = Root or keyatPosition > ParentKey then Return Swap Entry with Parent TrickleUp(ParentEntry)

5 Insert and Remove O(lgN) Assume min-heap Insert(newEntry) If heap is full then Return false Increase heapSize by one Instantiate new node and place in lastEntryPosition of heap TrickleUp(lastEntryPosition) and Return true Remove() If heap is empty then Return null Save root entry Store lastHeapEntry in rootPposition Decrement heapSize 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 sorting Sort complexity: O(n) steps of O(lgN) each Create Position = halfway through the list While Position is not at the top of the heap Trickle down entry at position to end of list Decrement Position by one entry Sort Create Heap in backwards order from the desired sort order while the heap is not empth Remove rootEntry 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


Download ppt "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."

Similar presentations


Ads by Google