Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2018:journals:bairdc:chapter2 [2018/01/30 04:43] – bairdc | courses:cs211:winter2018:journals:bairdc:chapter2 [2018/01/30 04:54] (current) – [2.5 – A More Complex Data Structure: Priority Queues] bairdc | ||
|---|---|---|---|
| Line 40: | Line 40: | ||
| ===== 2.5 – A More Complex Data Structure: Priority Queues ===== | ===== 2.5 – A More Complex Data Structure: Priority Queues ===== | ||
| + | The goal with a priority queue is to implement a data structure with n elements so that elements can be added and deleted, and the minimum selected, all in O(logn) time. You could use a list, but after removing the minimum, there needs to be a scan of all elements in O(n) time to find the new minimum. Also you couldn' | ||
| + | Balancing algorithms are shown below: | ||
| + | < | ||
| + | Heapify-down(H, | ||
| + | n = length(H) | ||
| + | if 2i > n then | ||
| + | Terminate with H unchanged | ||
| + | else if 2i < n then | ||
| + | left=2i and right=2i+1 | ||
| + | j be index that minimizes key[H[left]] and key[[H[right]] | ||
| + | else if 2i = n then | ||
| + | j=2i | ||
| + | if key[H[j]] < key[H[i]] then | ||
| + | swap array entries H[i] and H[j] | ||
| + | Heapify-down(H, | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | Heapify-up(H, | ||
| + | if i > 1 then | ||
| + | j=parent(i)=floor(i/ | ||
| + | if key[H[i]] < key[H[j]] then | ||
| + | swap array entries H[i] and H[j] | ||
| + | Heapify-up(H, | ||
| + | </ | ||
| + | |||
| + | This section was very readable and I would give it a 10/10 on readability and interestingness. I have no lingering questions after reading through the section. | ||
