Priority Queues
-
Upload
wilma-mcmahon -
Category
Documents
-
view
42 -
download
0
description
Transcript of Priority Queues
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• 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
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
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
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
Heap Height
Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).
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
Inserting An Element Into A Max Heap
Complete binary tree with 10 nodes.
9
8
6 7 2 6
5 1
7
7
Inserting An Element Into A Max Heap
New element is 5.
9
8
6 7 2 6
5 1
7
75
Inserting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
7
7
Inserting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
77
Inserting An Element Into A Max Heap
New element is 20.
9
86
7
2 6
5 1
7
77
Inserting An Element Into A Max Heap
New element is 20.
9
86
7
2 6
5 1
7
77
20
Inserting An Element Into A Max Heap
Complete binary tree with 11 nodes.
9
86
7
2 6
5 1
7
77
20
Inserting An Element Into A Max Heap
New element is 15.
9
86
7
2 6
5 1
7
77
20
Inserting An Element Into A Max Heap
New element is 15.
9
8
6
7
2 6
5 1
7
77
20
8
Inserting An Element Into A Max Heap
New element is 15.
8
6
7
2 6
5 1
7
77
20
8
9
15
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) )
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;}
Removing The Max Element
Max element is in the root.
8
6
7
2 6
5 1
7
77
20
8
9
15
Removing The Max Element
After max element is removed.
8
6
7
2 6
5 1
7
77 8
9
15
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.
Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
8
Removing The Max Element
Max element is 15.
6
7
2 6
5 1
7
77
9
15
8
Removing The Max Element
After max element is removed.
6
7
2 6
5 1
7
77
9
8
Removing The Max Element
Heap with 9 nodes.
6
7
2 6
5 1
7
77
9
8
Removing The Max Element
Reinsert 7.
6 2 6
5 1
79
8
Removing The Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
Removing The Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
7
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;}
Complexity Of Remove Max Element
Complexity is O(log n).
6 2 6
5 1
7
9
8
7
Binary Search Tree20
10
6
2 8
15
40
30
25
Only keys are shown.
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.
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:
The Operation Insert()20
10
6
2 8
15
40
30
25
Insert a pair whose key is 35.
35
The Operation Insert()
Insert a pair whose key is 7.
20
10
6
2 8
15
40
30
25 35
7
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.
Delete From A Leaf
Delete a leaf element. key = 7
20
10
6
2 8
15
40
30
25 35
7
18
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
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
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
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
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
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
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
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
Delete From A Degree 2 Node20
10
6
2 8
15
40
30
25
Replace with largest in left subtree.
35
7
18
Delete From A Degree 2 Node20
10
6
2 8
15
40
30
25
Replace with largest in left subtree.
35
7
18
Delete From A Degree 2 Node18
10
6
2 8
15
40
30
25
Replace with largest in left subtree.
35
7
18
Delete From A Degree 2 Node18
10
6
2 8
15
40
30
25
Complexity is O(height).
35
7
Indexed Binary Search Tree
• Binary search tree.
• Each node has an additional field. leftSize = number of nodes in its left subtree +
1
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
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; }}