Uploaded by ViVi Y

05 Indexable Priority Queue

advertisement
Indexable Min PQ
Let’s talk about it
● Indexable Min Priority Queue
● Each word describes a different part of the data structure.
2
Indexable Min Priority Queue
Indexable Min Priority Queue
Indexable Min Priority Queue
Indexable Min Priority Queue
4
5
0
2
6
1
3
Min Heap
● Every parent node is less
than or equal to the value
of its children nodes.
2
8
4
● The minimum value is
always the root node.
7
11
16
13
9
Representing Trees in Arrays
0
1
2
3
4
5
6
7
8
2
4
8
7
16
13
9
11
2
8
4
If X is an index of this array,
AND THIS ARRAY IS 1-BASED (starts at
index 1):
Left child of X is 2*X
Right child of X is 2*X + 1
Parent of X is (int) X/2
7
11
16
13
9
Min Heap – Insertion
2
8
4
7
16
13
9
11
0
1
2
3
4
5
6
7
8
9
10
2
4
8
7
16
13
9
11
n/a
n/a
Min Heap – Insertion
2
8
4
7
11
0
16
13
9
5
1
2
3
4
5
6
7
8
9
10
2
4
8
7
16
13
9
11
5
n/a
Min Heap – Insertion
2
8
4
7
11
0
16
13
9
5
1
2
3
4
5
6
7
8
9
10
2
4
8
7
16
13
9
11
5
n/a
Min Heap – Insertion
2
8
4
5
11
0
16
13
9
7
1
2
3
4
5
6
7
8
9
10
2
4
8
5
16
13
9
11
7
n/a
Min Heap – Deletion
2
8
4
5
11
0
16
13
9
7
1
2
3
4
5
6
7
8
9
10
2
4
8
5
16
13
9
11
7
n/a
Min Heap – Deletion
2
8
7
5
11
0
16
13
9
4
1
2
3
4
5
6
7
8
9
10
2
7
8
5
16
13
9
11
n/a
n/a
Min Heap – Deletion
2
8
5
7
16
13
9
11
0
1
2
3
4
5
6
7
8
9
10
2
5
8
7
16
13
9
11
n/a
n/a
Use Helper Functions
Supported Operations
● void insert(int index, Key key)
● boolean contains(int index)
● int minIndex()
● Key minKey()
● void deleteMin()
● Key delete(int index)
17
Indexed Min Priority Queue
Key Index
Q: what value does Bill have ?
Name
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
Bill has a value of keys[1] = 4
Q: which node in the heap represents Dylan?
Q: which person is being represented in the heap node at index 2 ?
keys
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
Position mapping
0
pq
qp
-1
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
-1
-1
-1
-1
-1
-1
-1
-1
-1
Inverse mapping
priority
Indexed Min Priority Queue
Key Index
ki=0
val=8
Insert(0, 8)
i = 0; n = 1;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
1
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
-1
-1
-1
-1
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=0
val=8
ki=1
val=4
Insert(1, 4)
i = 1; n = 2;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
1
1
2
3
4
5
6
7
8
9
0
1
0
0
0
0
0
0
0
2
-1
-1
-1
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=0
val=8
ki=1
val=4
Insert(1, 4)
i = 1; n = 2;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
1
1
2
3
4
5
6
7
8
9
0
1
0
0
0
0
0
0
0
2
-1
-1
-1
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=1
val=4
ki=0
val=8
Insert(1, 4)
i = 1; n = 2;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
2
1
2
3
4
5
6
7
8
9
1
0
0
0
0
0
0
0
0
1
-1
-1
-1
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=1
val=4
ki=2
val=5
ki=0
val=8
Insert(2, 5)
i = 2; n = 3;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
2
1
2
3
4
5
6
7
8
9
1
0
2
0
0
0
0
0
0
1
3
-1
-1
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=1
val=4
ki=2
val=5
ki=0
val=8
ki=3
val=13
Insert(3, 13)
i = 3; n = 4;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
2
1
2
3
4
5
6
7
8
9
1
0
2
3
0
0
0
0
0
1
3
4
-1
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=1
val=4
ki=0
val=8
ki=2
val=5
ki=4
val=2
ki=3
val=13
Insert(4, 2)
i = 4; n = 5;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
2
1
2
3
4
5
6
7
8
9
1
0
2
3
4
0
0
0
0
1
3
4
5
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=1
ki=1
val=4
val=4
ki=4
val=2
ki=2
val=5
ki=0
val=8
ki=3
val=13
Insert(4, 2)
i = 4; n = 5;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
5
1
2
3
4
5
6
7
8
9
1
4
2
3
0
0
0
0
0
1
3
4
2
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=4
val=2
ki=1
val=4
ki=2
val=5
ki=0
val=8
ki=3
val=13
Insert(4, 2)
i = 4; n = 5;
keys
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Name
5
1
2
3
4
5
6
7
8
9
4
1
2
3
0
0
0
0
0
2
3
4
1
-1
-1
-1
-1
-1
priority
Indexed Min Priority Queue
Key Index
ki=4
val=2
ki=1
val=4
ki=8
val=7
ki=3
val=13
keys
ki=6
val=16
ki=5
val=9
ki=0
val=8
ki=7
val=11
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
Fred
3
13
Kelly
4
2
Ellen
5
9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
ki=2
val=5
Name
5
1
2
3
4
5
6
7
8
9
4
1
2
8
0
5
6
3
7
2
3
8
1
6
7
9
4
-1
priority
Indexed Min Priority Queue
1
Name
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
9
Fred
3
13
ki=7
val=11
Kelly
4
2
Ellen
5
9
3
2
ki=1
val=4
4
5
ki=8
val=7
8
ki=3
val=13
keys
ki=2
val=5
1
ki=0
val=8
7
6
ki=6
val=16
ki=5
val=9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Key Index
Q: which node in the heap represents Dylan?
ki=4
val=2
5
1
2
3
4
5
6
7
8
9
4
1
2
8
0
5
6
3
7
2
3
8
1
6
7
9
4
-1
priority
Indexed Min Priority Queue
1
Q: which person is being represented in the
heap node at index 2 ?
ki=4
val=2
Name
Ki
Value
Anna
0
8
Bill
1
4
Carly
2
5
9
Fred
3
13
ki=7
val=11
Kelly
4
2
Ellen
5
9
3
2
ki=1
val=4
4
5
ki=8
val=7
8
ki=3
val=13
keys
ki=2
val=5
1
ki=0
val=8
7
6
ki=6
val=16
ki=5
val=9
0
1
2
3
4
5
6
7
8
Dylan
6
16
8
4
5
13
2
9
16
11
7
Laura
7
11
Henry
8
7
0
pq
qp
Key Index
5
1
2
3
4
5
6
7
8
9
4
1
2
8
0
5
6
3
7
2
3
8
1
6
7
9
4
-1
priority
Download