Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2012:journals:jeanpaul:chaptter2section5 [2012/01/24 22:11] – mugabej | courses:cs211:winter2012:journals:jeanpaul:chaptter2section5 [2012/01/26 17:59] (current) – [Implementing the Priority queue] mugabej | ||
|---|---|---|---|
| Line 24: | Line 24: | ||
| - | The heap comes in as a solution to these issues, as it combines the benefits of a sorted array and a list. Elements in a //heap order// are arranged in such a way the key of any parent element is not bigger than the key of any of its children.\\ | + | The heap comes in as a solution to these issues, as it combines the benefits of a sorted array and a list. Elements in a //heap order// are arranged in such a way the key of any parent element is not bigger than the key of any of its children, and the heap itself is a balanced binary tree.\\ |
| + | in a heap implemented using arrays: | ||
| + | * Each node stores its key,and three pointers: two pointing to its left and right child, and the third pointing to its parent | ||
| + | * For a heap H,H[1] = root | ||
| + | * leftChild(i) = 2i | ||
| + | * rightChild(i) = 2i + 1 | ||
| + | * parent(i) = int(i/ | ||
| + | * length(H) = number of elements in the heap(Logical size) | ||
| + | |||
| + | ====Implementing the heap operations==== | ||
| + | |||
| + | * It takes O(1) to identify the **Min** element since the smallest key is the root | ||
| + | * To add a new element,we use the procedure called **Heapify-up** to keep the heap property.(Algorithm: | ||
| + | * **Heapify-up** fixes the heap in O(log(i)) time, | ||
| + | * inserting a new element in a heap of length //n//,takes O(log//n//) when we use **Heapify-up**. | ||
| + | * Deleting an element,i, is a little trickier | ||
| + | * Since deleting an element,i, from the heap leaves a hole in the heap, we move the last element in the heap into that hole | ||
| + | * Thus we encounter two scenarios: | ||
| + | * If the element moved at position i is too small,then we can use **Heapify-up** to fix the heap | ||
| + | * If the element moved at position i is too big,we use another procedure similar to **Heapify-up**, | ||
| + | * **Heapify-down** fixes the heap in O(log//n//) time assuming the heap is almost a heap with the key value of H[i] too big. | ||
| + | * Using **Heapify-up** | ||
| + | (For definitions of too big and too small,see Tardos and Kleinberg, pp 61-4). | ||
| + | |||
| + | ====Implementing Priority Queues with Heaps==== | ||
| + | |||
| + | Operations used for a heap with N < //n// elements: | ||
| + | *StartHeap(N): | ||
| + | *Insert(v) Inserts item v into heap. Takes O(log//n//) time at most, if heap currently has //n// elements. | ||
| + | *FindMin() Identifies minimum element in heap but does not remove it.Takes O(1) time. | ||
| + | *Delete(i) Deletes element in heap at position i. Takes O(log//n//) time if heap currently has //n// elements. | ||
| + | *ExtractMin() Identifies and deletes an element with minimum key from heap. Takes O(log//n//) time if heap currently has //n// elements. | ||
| + | |||
| + | |||
| + | If we want to operate on a particular node, | ||
| + | |||
| + | In this case, | ||
| + | |||
| + | * To delete a node v, we use Delete(H, | ||
| + | * To change the key of v to w, we use ChangeKey(H, | ||
| + | |||
| + | |||
| + | I definitely need to remember the positional relationships between the parent and its children in a heap. I also need to remember the two procedures, namely **Heapify-up** and **Heapify-down** | ||
| + | |||
| + | This section was a very important and interesting reading, but I give it an 8/10 since it required additional attention compared to the other sections. | ||
