DS Lec 16 Heap

Post on 29-Jan-2016

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Data Structures, Tree, Queue

Transcript

Priority Queue (Heap)

Lecture # 16

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.

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.

Heaps as Priority Queues

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

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.

Are these Heaps?

15

6

10

8 7

62 3

20

10 12

What about these?

6

15

10

2 7

68 3

12

10 21

6

15

10

8 7

62 3

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

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

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

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.

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;

Dequeuing an element from heap

10

8 7

62 5

20

15

13 14

dequeue

10

8 7

2 5

6

15

13 14

Dequeuing an element from heap

10

8 7

2 5

15

6

13 14

10

8 7

2 5

15

14

13 6

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.

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;}

};

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

};

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);

}

}

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;

}

}

}

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;

}

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;

}

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;

}

}

}

top related