Heaps
Binary Min-Heap
A binary heap is a binary tree where
17
19
36
7
2
3
1
25
100
To insert an element into a binary heap, we percolate the element up
Problem 4
Try (a)
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
|
|
|
|
|
|
|
|
|
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
|
|
|
|
|
|
|
|
|
10
insert(10)
10 |
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
10 |
|
|
|
|
|
|
|
|
7
10
insert(7)
7 |
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
7 |
10 |
|
|
|
|
|
|
|
10
7
insert(7)
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
|
– |
7 |
10 |
15 |
|
|
|
|
|
|
10
15
7
insert(15)
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
7 |
10 |
15 |
|
|
|
|
|
|
10
15
7
17
insert(17)
17 |
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
7 |
10 |
15 |
17 |
|
|
|
|
|
10
15
7
17
12
insert(12)
12 |
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
12 |
|
|
|
|
10
15
7
17
12
20
insert(20)
7 |
10 |
15 |
17 |
20 |
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
|
|
|
6
insert(6)
10
15
7
17
12
20
12 |
20 |
7 |
10 |
15 |
17 |
6 |
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
15 |
|
|
15
insert(6)
10
6
7
17
12
20
12 |
20 |
7 |
10 |
6 |
17 |
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
15 |
|
|
15
insert(6)
10
7
6
17
12
20
12 |
20 |
6 |
10 |
7 |
17 |
Problem 4a
Insert 10, 7, 15, 17, 12, 20, 6, 32 into a min heap.
– |
15 |
|
|
15
insert(32)
10
7
6
17
12
20
12 |
20 |
6 |
10 |
7 |
17 |
32
32 |