Slides cover understanding heap, heap properties, representation of heap, up-heap and down heap bubbling followed by Adaptable priority queues, list and heap implementation of priority queues.
2. Session 2 review
In list-based implementation of priority queue:
If we use unsorted lists:
insertion takes O(1) time
deletion takes O(n) time.
If we use sorted lists:
insertion takes O(n) time
deletion takes O(1) time.
5. Heap Data structure
Is a binary tree T that stores a collection of entries at its nodes
and satisfies two additional properties:
Order Property
Structure Property
6. Heap properties
Order Property:
For every internal node v, other than the root, key(v) ≥
key(parent(v)).
Structure Property:
A heap T with height h is a complete binary tree, i.e.:
for depth/level i < h, there are 2i nodes.
at depth h−1, the internal nodes are to the left of the external
nodes.
7. Heap data structure
The last node of a heap is the rightmost node of the
maximum depth.
2
65
89 Last node
8. Height of heap
A heap storing n keys has height h = [lg n]
For each depth i < h , there are 2i nodes and
at least one node at depth h. Thus,
n ≥ 20 + 21+ 22 + ... + 2h-1 + 1
≥ 1 + 2 + 4 + ... + 2h-1 + 1
≥ 2h
Which gives, h ≤ lgn.
9. Array list representation of heap
For the node at rank i
the left child is at rank 2i.
the right child is at rank 2i+1.
The cell at rank 0 is not used
A heap with n keys is represented by means of a array size n+1.
2
65
79
0 1 2 3 4 5
2 5 6 79
10. Insertion Algorithm
The insertion algorithm consists of three steps:
Find the insertion node z (i.e. the new last node).
Store k at z. (Here k=1)
Restore the heap-order property (discussed next).
2
65
79 2
5
79
z
Insertion Node
6
1
12. Removal from a heap
The removal algorithm consists of three steps:
Replace the root key with the key of the last node
w.
Remove w.
Restore the heap-order property (discussed next).
2
65
79 7
5
9
w
Last Node
6
New Last Node
14. Heap-sort
A heap-based priority queue, sorts a sequence of n
elements in O(nlgn) time.
performs n series of removeMin() operation.
Since each call of removeMin() takes O(lgn) time, sorting a
heap with n elements takes O(nlgn) time.
17. Adaptable Priority Queues
Priority queues which are adaptable.
When priority queue methods are not sufficient.
For example, in a standby airline passenger application.
18. Adaptable Priority Queue ADT
remove(e):
Remove from P and return e.
replaceKey(e, k):
Replace with k and return the key of entry e of P.
replaceValue(e, x):
Replace with x and return the value of entry e of P.
20. Location aware entry
mechanism for finding the position of entry e of Priority Queue P.
This position is called the location of the entry
implementation of an entry that keeps track of its position is called
location-aware entry.
21. List implementation of an adaptable
PQ
A location-aware list entry is an object storing
key
value
position (or rank) of the entry in the list.
22. Heap Implementation of an Adaptable PQ
A location-aware list entry is an object storing
key
value
position of the entry in the heap.
Order property is about every internal node whose key value must be greater than the key value of its parent.
Depth: number of ancestors a node has
Height: the height of a tree is given as the maximum depth of any node
// structure property considers a heap of height &quot;h&quot; a complete binary tree when:
its depth is less than its height and there are 2 raise to power of i nodes.
another condition is that at height h-1 , internal nodes are to the left of the external nodes that //
also means in a way that last node of a heap is the rightmost node of the maximum depth.
&lt;number&gt;
&lt;number&gt;
this is about height of the heap, which is said to be as log n where n is the number of entries,
now here we consider //the structural property of heap which says that at depth i ,// heap has 2 raise to power of i nodes. and //hence using mathematical operations// and in the end taking// log on both sides it is easily proved that height is log of number of entries.
&lt;number&gt;
We store a (key and value) for each entry item at each internal node, and keep track of the position of the last node.
&lt;number&gt;
Insertion into heap has three steps. // // //
After the insertion of a new node, the heap-order property may be violated. we need to restore it by some swappings of the values stored at z with the entry stored in its ancestor nodes.//
&lt;number&gt;
The upward movement of the newly inserted entry //by means of swaps is called up-heap bubbling. Since the heap has height lgn, up-heap runs in O(lgn) time.
&lt;number&gt;
removal from a heap has three steps as well, // // // After replacing the root key with key k of the last node, again the heap-order property gets violated. Then we need to restore the heap-order by swapping the entries stored at root with the entries stored in its descendent nodes.// here The downward movement of the root entry by means of swaps is call down-heap bubbling. as the heap has height lgn, down-heap runs in O(lgn) time.
&lt;number&gt;
Using a heap-based priority queue, we can sort a sequence of n elements in O(nlgn) time, we have already covered sorting in detail, heap sort is also similar. //The algorithm simply performs n series of removeMin() operation. //Since each call of removeMin() takes O(lgn) time, sorting a heap with n elements takes O(nlgn) time.
Traaversing takes O(logn) time --. Sorting takes O(nlog(n)) time
&lt;number&gt;
lets see how two are merged together, lets say We are given two heaps and a key k. //We create a new heap with the root node storing k and with the two heaps as subtrees.// We perform down-heap bubbling to restore the heap-order property.
&lt;number&gt;
&lt;number&gt;
APQ are the priority queues which are adaptable, //There are some situations where the basic methods of the priority queue ADT (given in previous section) are not sufficient. //For example, in a standby airline passenger application: A passenger may request to be removed from the waiting list.A passenger presents a document (e.g. gold frequent flyer card) that implies to update his/her order in the waiting list.a passenger notices her name is misspelled on the ticket and asks it to be corrected. So, we need the priority queues be adaptable.
&lt;number&gt;