DS Lec 16 Heap

22
Priority Queue (Heap) Lecture # 16

description

Data Structures, Tree, Queue

Transcript of DS Lec 16 Heap

Page 1: DS Lec 16 Heap

Priority Queue (Heap)

Lecture # 16

Page 2: DS Lec 16 Heap

Priority Queues In many situations, simple queues are inadequate,

since first in/first out scheduling has to be overruled using some priority criteria.

E.g. in a sequence of processes, process P2 may need to be executed before process P1 for the proper functioning of a system, even though P1 was put on the queue of waiting processes before P2.

In situations like these, a modified queue or priority queue, is needed.

In priority queues, elements are dequeued according to their priority and their current queue positions.

Page 3: DS Lec 16 Heap

Priority Queues

The problem with a priority queue is in finding an efficient implementation which allows relatively fast enqueuing and dequeuing

Since elements may arrive randomly to the queue, there is no guarantee that the front elements will be the most likely to be dequeued and that the elements put at the end will be the last candidates for dequeuing.

The situation is complicated because a wide spectrum of possible priority criteria can be used in different cases.

Page 4: DS Lec 16 Heap

Heaps as Priority Queues

A heap is an excellent way to implement a priority queue.

Page 5: DS Lec 16 Heap

Heaps

A particular kind of binary tree, called a heap, has the following properties.

The value of each node is not less than the values stored in each of its children

The tree is an almost complete binary tree. These two properties define a max heap. If “less” in the first property is replaced with

“greater”; then the definition specifies a min heap.

Page 6: DS Lec 16 Heap

Are these Heaps?

15

6

10

8 7

62 3

20

10 12

Page 7: DS Lec 16 Heap

What about these?

6

15

10

2 7

68 3

12

10 21

6

15

10

8 7

62 3

Page 8: DS Lec 16 Heap

Enqueuing an element to a heap

To enqueue an element, the element is added at the end of the heap as the last leaf.

Restoring the heap property in the case of enqueuing is achievied by moving from the last leaf toward the root.

The algorithm for enqueuing is as follows

heapEnqueue(el)put el at the end of heap;while el is not in the root and el > parent(el)

swap el with its parent

Page 9: DS Lec 16 Heap

Enqueuing an element to a heap

10

8 7

62 5

20

15

13 14

10

8 7

62 5

20

15

13 14

15

Page 10: DS Lec 16 Heap

Enqueuing an element to a heap

10

8 15

62 5

20

15

13 14

7

15

8 10

62 5

20

15

13 14

7

Page 11: DS Lec 16 Heap

Dequeuing an element from heap

Dequeuing an element from the heap consists of removing the root element from the heap, since

By the heap property it is the element with the greatest priority.

Then the last leaf is put in its place, and the heap property almost certainly has to be

restored, This time by moving down the root down the tree.

Page 12: DS Lec 16 Heap

Dequeuing an element from heap

The algorithm for dequeuing is as follows:heapDequeue()

extract the element from the root;

put the element from the last leaf in its place;

remove the last leaf;

//both subtrees of the root are heaps;

p = root;

while p is not a leaf and p < any of its children

swap p with the larger child;

Page 13: DS Lec 16 Heap

Dequeuing an element from heap

10

8 7

62 5

20

15

13 14

dequeue

10

8 7

2 5

6

15

13 14

Page 14: DS Lec 16 Heap

Dequeuing an element from heap

10

8 7

2 5

15

6

13 14

10

8 7

2 5

15

14

13 6

Page 15: DS Lec 16 Heap

Dequeuing an element from heap

heapDequeue()extract the element from the root;put the element from the last leaf in its place;remove the last leaf;//both subtrees of the root are heaps;p = root;while p is not a leaf and p < any of its children

swap p with the larger child; The last three lines of this algorithm can be treated as

the separate algorithm that Restore the heap property only if it has been violated by

the root of the tree.

Page 16: DS Lec 16 Heap

Implementation of Dynamic Heapclass DHeapNode{public:

int info;DHeapNode *left, *right, *father;DHeapNode()

{info = 0;left = right = father = 0;}

DHeapNode(int d, DHeapNode *l = 0, DHeapNode *r= 0,DHeapNode *f = 0){info = d;left = l;right = r;father = f;}

};

Page 17: DS Lec 16 Heap

Implementation of Dynamic Heapclass Dheap

{ private:

DHeapNode *root;

DHeapNode *lastInserted; //points to last inserted node

void percolateUp(DHeapNode*); //Restore heap property

//after insertion of new node

void percolateDown(); //Restore heap property

//after deletion of a node

void adjustLastInserted(); //adjust the position

// of last Inserted Pointer

public:

Dheap() {root=0;}

void insert(int); //inserts element in heap;

int remove(); //remove and return an element from heap

};

Page 18: DS Lec 16 Heap

Enqueuing an element to Dynamic heapvoid Dheap::insert(int d)

{

DHeapNode *n = new DHeapNode(d);

if(root==0) root=n;

else {

Queue<DHeapNode*> Q; //creates queue

bool inserted = false;

Q.enqueue(root);

while(!inserted)

{ DHeapNode *p = Q.dequeue();

if(p->left == 0)

{ p->left = n; n->father = p; inserted = true; }

else if(p->right == 0)

{ p->right = n; n->father = p; inserted = true; }

else

{ Q.enqueue(p->left); Q.enqueue(p->right); }

}

lastInserted = n; percolateUp(n);

}

}

Page 19: DS Lec 16 Heap

Restore Heap property after equeuing an element

void Dheap::percolateUp(DHeapNode *nI)

{

bool heapProperty = false;

while(!heapProperty && nI->father != 0)

{

if(nI->info > nI->father->info)

{

swap(nI->info, nI->father->info);

nI = nI->father;

}

else

{

heapProperty = true;

}

}

}

Page 20: DS Lec 16 Heap

Dequeuing an element from Heapint Dheap::remove() {

int element = root->info;

root->info = lastInserted->info;

DHeapNode *d = lastInserted;

if(lastInserted->father->right = lastInserted)

{

lastInserted->father->right = 0;

lastInserted = lastInserted->father->left;

}

else

{

lastInserted->father->left = 0;

adjustLastInserted(); //adjust the position of lastInseted pointer

}

delete d;

percolateDown(); //Restore heap property;

return element;

}

Page 21: DS Lec 16 Heap

Adjust the position of last Inserted Pointer after dqueuingvoid Dheap::adjustLastInserted()

{

Queue<DHeapNode*> Q; //create a queue

DHeapNode *p;

Q.enqueue(root);

while(!Q.isEmpty())

{

p = Q.dequeue();

if(p->left != 0)

Q.enqueue(p->left);

if(p->right != 0)

Q.enqueue(p->right);

}

lastInserted = p;

}

Page 22: DS Lec 16 Heap

Restore Heap Property after Dqueuingvoid Dheap::percolateDown()

{

DHeapNode *p = root;

bool heapProperty = false;

while(((p->left != 0) || (p->right != 0)) && (!heapProperty))

{ if((p->right != 0) && (p->right->info > p->left->info))

{ if(p->right->info > p->info)

{ swap(p->info, p->right->info);

p = p->right;

}

else heapProperty = true;

}

else

{ if(p->left->info > p->info)

{ swap(p->info, p->left->info);

p = p->left;

}

else heapProperty = true;

}

}

}