Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS Anya E. Vostinar Grinnell College

Similar presentations


Presentation on theme: "CS Anya E. Vostinar Grinnell College"— Presentation transcript:

1 CS 301.02 Anya E. Vostinar Grinnell College
Heaps! (of fun) CS Anya E. Vostinar Grinnell College

2 Outline Logistical updates Review of heaps and priority queues
Homework 1 is up! Random partners today Review of heaps and priority queues Heap HackerRank problem

3 Heap Definition What is a priority queue? What is a heap? What is the structure of a heap? How is a heap labeled? Priority queues: Insert (Queue, item), Find-Minimum(Queue), Delete-Minimum(Queue); can have basic data structure of an unsorted array, tree, sorted array, heap A binary heap is defined to be a binary tree with a key in each node such that: 1. All leaves are on, at most, two adjacent levels 2. All leaves on the lowest level occur to the left, and all levels except the lowest one are completely filled 3. The key in the root is less than all its children, and the left and right subtrees are again binary heaps

4 Binary Heaps How are the keys organized in the heap? Why?
Heaps maintain a partial order on the set of elements which is weaker than the sorted order (why is that helpful?) yet stronger than random order (why is that helpful?) so it can be efficient to maintain so the minimum element can be quickly identified How are the keys organized in the heap? Why? *Content from Skienna

5 How to actually store a heap?
Array-Based Heaps The most natural representation of this binary tree would involve storing each key in a node with pointers to its two children. However, we can store a tree as an array of keys, usiing the position of the keys to implicitly satisfy the role of the pointers. The left child of k sits in position 2k and the right child in 2k + 1. The parent of k is in position ⌊n/2⌋. What is the rule for finding the children of a node k in this heap?

6 Any tree as an array? Does it make sense to store any arbitrary tree implicitly in an array? Why or why not? What’s the alternative? The implicit representation is only efficient if number of nodes n < 2h. All missing internal nodes still take up space in our structure. This is why we insist on heaps as being as balanced/full at each level as possible. The array-based representation is also not as flexible to arbitrary modifications as a pointer-based tree.

7 How do you construct a heap?
Current Heap 2 1 15 12 Heaps can be constructed incrementally, by inserting new elements into the left-most open spot in the array. If the new element is greater than its parent, swap their positions and recur.

8 The thing we actually want to do
How to you extract the priority value? What else do you need to do? Remove top value, swap last value into top spot, fix the heap by swapping smaller value up until done

9 Complexity? Heaps can be constructed incrementally, by inserting new elements into the left-most open spot in the array. If the new element is greater than its parent, swap their positions and recur. The height h of an n element heap is bounded because: so, and insertions take O(log n) time

10 Practice time! Go to and solve the problem with your assigned partner Show the mentor or me when you have completed the problem to get attendance credit If you don’t complete the problem in the class period, that’s okay too If you finish early, there are lots more problems on HackerRank to try!


Download ppt "CS Anya E. Vostinar Grinnell College"

Similar presentations


Ads by Google