56
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.

Priority Queues

Embed Size (px)

DESCRIPTION

Priority Queues. Two kinds of priority queues: Min priority queue. Max priority queue. Complexity Of Operations. Sorted List Insert O(N) Delete O(1) Unsorted List Insert O(1) Delete O(N). Heaps. Two key properties Heap shape Complete binary tree Value at node - PowerPoint PPT Presentation

Citation preview

Page 1: Priority Queues

Priority Queues

Two kinds of priority queues:

• Min priority queue.

• Max priority queue.

Page 2: Priority Queues

Complexity Of Operations

• Sorted List– Insert O(N)– Delete O(1)

• Unsorted List– Insert O(1)– Delete O(N)

Page 3: Priority Queues

Heaps• Two key properties

– Heap shape• Complete binary tree

– Value at node• Min Heap: Smaller than or equal to values in subtrees

• Max Heap: Larger than or equal to values in subtrees

• Example heap– X Y– X Z

Y

X

Z

Page 4: Priority Queues

Heap & Non-heap Examples

Heaps Non-heaps

6

2

22

8 45 25

6

2

22

8 45 25

8

6 45

5

6 22

25

5

5 45

5

Page 5: Priority Queues

Min Heap With 9 Nodes

Complete binary tree with 9 nodes that is also a min tree.

2

4

6 7 9 3

8 6

3

Page 6: Priority Queues

Max Heap With 9 Nodes

Complete binary tree with 9 nodes that is also a max tree.

9

8

6 7 2 6

5 1

7

Page 7: Priority Queues

Heap Height

Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).

Page 8: Priority Queues

9 8 7 6 7 2 6 5 1

1 2 3 4 5 6 7 8 9 100

A Heap Is Efficiently Represented As An Array

9

8

6 7 2 6

5 1

7

Page 9: Priority Queues

Inserting An Element Into A Max Heap

Complete binary tree with 10 nodes.

9

8

6 7 2 6

5 1

7

7

Page 10: Priority Queues

Inserting An Element Into A Max Heap

New element is 5.

9

8

6 7 2 6

5 1

7

75

Page 11: Priority Queues

Inserting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

7

7

Page 12: Priority Queues

Inserting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

77

Page 13: Priority Queues

Inserting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

Page 14: Priority Queues

Inserting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

20

Page 15: Priority Queues

Inserting An Element Into A Max Heap

Complete binary tree with 11 nodes.

9

86

7

2 6

5 1

7

77

20

Page 16: Priority Queues

Inserting An Element Into A Max Heap

New element is 15.

9

86

7

2 6

5 1

7

77

20

Page 17: Priority Queues

Inserting An Element Into A Max Heap

New element is 15.

9

8

6

7

2 6

5 1

7

77

20

8

Page 18: Priority Queues

Inserting An Element Into A Max Heap

New element is 15.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 19: Priority Queues

Heap Operations – Insert( X )

• Algorithm– Add X to end of tree– While (X > parent)

• Swap X with parent // X bubbles up tree

• Complexity– # of swaps proportional to height of tree– O( log(n) )

Page 20: Priority Queues

Insertion

Insert(int key){ n++; //heap size+1 i=n; while (key > heap[i/2] && i>1) { heap[i]=heap[i/2]; i=i/2; } heap[i]=key;}

Page 21: Priority Queues

Removing The Max Element

Max element is in the root.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 22: Priority Queues

Removing The Max Element

After max element is removed.

8

6

7

2 6

5 1

7

77 8

9

15

Page 23: Priority Queues

Removing The Max Element

Heap with 10 nodes.

8

6

7

2 6

5 1

7

77 8

9

15

Reinsert 8 into the heap.

Page 24: Priority Queues

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Page 25: Priority Queues

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Page 26: Priority Queues

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

8

Page 27: Priority Queues

Removing The Max Element

Max element is 15.

6

7

2 6

5 1

7

77

9

15

8

Page 28: Priority Queues

Removing The Max Element

After max element is removed.

6

7

2 6

5 1

7

77

9

8

Page 29: Priority Queues

Removing The Max Element

Heap with 9 nodes.

6

7

2 6

5 1

7

77

9

8

Page 30: Priority Queues

Removing The Max Element

Reinsert 7.

6 2 6

5 1

79

8

Page 31: Priority Queues

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

Page 32: Priority Queues

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

7

Page 33: Priority Queues

DeletionDelete(){ key=heap[1]; data=heap[n--]; i=1; while ((j=2*i)<=n) { if (2*i+1<=n) { if (heap[2*i+1] > heap[2*i]) j=2*i+1; } if (heap[j] > data); { heap[i]=heap[j]; i=j; } else break; } heap[i]=data; return key;}

Page 34: Priority Queues

Complexity Of Remove Max Element

Complexity is O(log n).

6 2 6

5 1

7

9

8

7

Page 35: Priority Queues

Binary Search Tree20

10

6

2 8

15

40

30

25

Only keys are shown.

Page 36: Priority Queues

Definition Of Binary Search Tree

• A binary tree.

• Each node has a (key, value) pair.

• For every node x, all keys in the left subtree of x are smaller than that in x.

• For every node x, all keys in the right subtree of x are greater than that in x.

Page 37: Priority Queues

Binary search trees

• Average depth of a node is O(log N); maximum depth of a node is O(N)

Two binary search trees representing

the same set:

Page 38: Priority Queues

The Operation Insert()20

10

6

2 8

15

40

30

25

Insert a pair whose key is 35.

35

Page 39: Priority Queues

The Operation Insert()

Insert a pair whose key is 7.

20

10

6

2 8

15

40

30

25 35

7

Page 40: Priority Queues

The Operation Delete()

Four cases:

No element with delete key.

Element is in a leaf.

Element is in a degree 1 node.

Element is in a degree 2 node.

Page 41: Priority Queues

Delete From A Leaf

Delete a leaf element. key = 7

20

10

6

2 8

15

40

30

25 35

7

18

Page 42: Priority Queues

Delete From A Degree 1 Node

Delete from a degree 1 node. key = 40

20

10

6

2 8

15

40

30

25 35

7

18

Page 43: Priority Queues

Delete From A Degree 1 Node (contd.)

Delete from a degree 1 node. key = 15

20

10

6

2 8

15

40

30

25 35

7

18

Page 44: Priority Queues

Delete From A Degree 2 Node

Delete from a degree 2 node. key = 10

20

10

6

2 8

15

40

30

25 35

7

18

Page 45: Priority Queues

Delete From A Degree 2 Node20

10

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

Page 46: Priority Queues

Delete From A Degree 2 Node20

10

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

Page 47: Priority Queues

Delete From A Degree 2 Node20

8

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

Page 48: Priority Queues

Delete From A Degree 2 Node20

8

6

2 8

15

40

30

25

Largest key must be in a leaf or degree 1 node.

35

7

18

Page 49: Priority Queues

Another Delete From A Degree 2 Node

Delete from a degree 2 node. key = 20

20

10

6

2 8

15

40

30

25 35

7

18

Page 50: Priority Queues

Delete From A Degree 2 Node20

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

Page 51: Priority Queues

Delete From A Degree 2 Node20

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

Page 52: Priority Queues

Delete From A Degree 2 Node18

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

Page 53: Priority Queues

Delete From A Degree 2 Node18

10

6

2 8

15

40

30

25

Complexity is O(height).

35

7

Page 54: Priority Queues

Indexed Binary Search Tree

• Binary search tree.

• Each node has an additional field. leftSize = number of nodes in its left subtree +

1

Page 55: Priority Queues

Example Indexed Binary Search Tree20

10

6

2 8

15

40

30

25 35

7

18

1

1 2

2

5

1

1

8

1 1

2

4

leftSize values are in red

Page 56: Priority Queues

Search by rankint SBR(node* root, int rank){ if (rank>treesize) return; ptr=root; while (1) { if (ptr->index==rank) return ptr->data; else if (ptr->index < rank) { ptr=ptr->rightchild; rank=rank - ptr->index; } else ptr=ptr->leftchild; }}