Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.

Similar presentations


Presentation on theme: "1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues."— Presentation transcript:

1 1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues

2 2 6.2 Simple Implementations Simple linked list –insert in front O (1) –delete minimum O (N) Sorted linked list –insert O (N) –delete minimum O (1)

3 3 6.2 Simple Implementations Binary search tree – O (log N) for both operations Binary heap – worst case time O (log N) – building a priority queue in linear time

4 4 6.3 Binary Heap Sometimes just called heap A binary heap tree that is completely filled, except at the bottom level, which is filled from left to right. A complete binary tree of height h has between 2 h and 2 h+1 - 1 nodes The height of a complete binary tree is floor (log N).

5 5 6.3 Binary Heap A complete binary tree can be represented in an array and no pointers are necessary.

6 6 6.3 Binary Heap The root is at position 1 (reserve position 0 for a sentinel - MinData). For an element at position i, its left child is at position 2i and its it right child at 2i +1; its parent is at floor (i/2). Heap order property –The value at any node should be smaller than all of its descendants.

7 7 6.3 Binary Heap struct HeapStruct { int Capacity; /* maximum size */ int Size; /* actual size */ ElementType *Elements; }; typedef struct HeapStruct *PriorityQueue; Definitions

8 8 6.3 Binary Heap PriorityQueue Initialize (int MaxElements); void Destroy (PriorityQueue H); void MakeEmpty (PriorityQueue H); void Insert (ElementType X, PriorityQueue H); ElementType DeleteMin (PriorityQueue H); ElementType FindMin (PriorityQueue H); int IsEmpty (PriorityQueue H); int IsFull (PriorityQueue H); Some functions

9 9 6.3 Binary Heap - Initialize /* Fig 6.4 */ PriorityQueue Initialize (int MaxElements) { PriorityQueue H; if (MaxElements < MinPQSize) Error ("Priority queue size is too small"); H = malloc (sizeof (struct HeapStruct)); if (H == NULL) FatalError ("Out of space!!!");

10 10 6.3 Binary Heap - Initialize /* Allocate the array plus one extra for sentinel */ H->Elements = malloc ((MaxElements + 1) * sizeof (ElementType)); if (H->Elements == NULL) FatalError ("Out of space!!!"); H->Capacity = MaxElements; H->Size = 0; H->Elements [0] = MinData; return H; }

11 11 6.3 Binary Heap - Insert To insert an element X, create a hole in the next available location. If X can be placed in the hole without violating heap order, insertion is complete. Otherwise slide the element that is in the hole’s parent node into the hole, i.e., bubbling the hole up toward the root.

12 12 6.3 Binary Heap - Initialize Continue this process until X can be placed in the hole (a percolating up process). Worst case running time is O (log N), the new element is percolating up all the way to the root.

13 13 6.3 Binary Heap - Insert

14 14 6.3 Binary Heap - Insert

15 15 6.3 Binary Heap - Insert /* Fig 6.8 */ /* H->Element[ 0 ] is a sentinel */ void Insert (ElementType X, PriorityQueue H) { int i; if (IsFull (H)) { Error ("Priority queue is full"); return; }

16 16 6.3 Binary Heap - Insert for (i = ++H->Size; H->Elements [i / 2] > X; i /= 2 ) H->Elements [i] = H->Elements [i / 2]; H->Elements [i] = X; }

17 17 6.3 Binary Heap - DeleteMin The element at the root (position 1) is to be removed, and a hole is created. Place the last element X in the hole. If X is smaller than the child(ren), job is done. Otherwise slide the smaller of the hole’s children into the hole, thus pushing the hole down one level.

18 18 6.3 Binary Heap - DeleteMin Repeat the previous step until X can be placed in the hole (percolating down). Some node may have only one child. Worst case running time is O (log N). On average, the element that is placed at the root is percolated almost to the bottom of the heap, so the average running time is O (log N).

19 19 6.3 Binary Heap - DeleteMin

20 20 6.3 Binary Heap - DeleteMin

21 21 6.3 Binary Heap - DeleteMin

22 22 6.3 Binary Heap - DeleteMin /* Fig. 6.12 */ ElementType DeleteMin (PriorityQueue H) { int i, Child; ElementType MinElement, LastElement; if (IsEmpty (H)) { Error ("Priority queue is empty"); return H->Elements [0]; }

23 23 6.3 Binary Heap - DeleteMin MinElement = H->Elements [1]; LastElement = H->Elements [H->Size--]; for (i = 1; i * 2 Size; i = Child) { /* Find smaller child */ Child = i * 2; if (Child != H->Size && H->Elements [Child + 1] Elements [Child]) Child++;

24 24 6.3 Binary Heap - DeleteMin /* Percolate one level */ if (LastElement > H->Elements [Child]) H->Elements [i] = H->Elements [Child]; else break; } H->Elements [i] = LastElement; return MinElement; }


Download ppt "1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues."

Similar presentations


Ads by Google