





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
slide for data structure and algorithms imam uni
Typology: Slides
1 / 37
This page cannot be seen from the preview
Don't miss anything!






























Queue: order items by when they were placed - first in, first
out ( FIFO )
Function
s
enqueue
dequeue
first
size
isEmpty
Unsorted List
PQueue
Sorted Singly-Linked
List
Unsorted Doubly-Linked
List
Binary
Heap
Slides by Chris Gregg!
Binary Heaps
12
22 43
5
11
8
10
14 13
2
17
7
50
3
36
19
25 1
Binary Heaps (Min Heap)
Binary Heaps
22
12
11
5
85
8
10
14 13
25
24
26
13
99
36
19
46 42
Binary Heaps
22
12
11
5
85
8
10
14 13
25
24
26
13
99
36
19
46 42
Binary Heaps
What is the best way to store a
heap?
12
5
11
8
10
13
14
22 43
We could use a node-based solution, but…
Binary Heaps
It turns out that an array works great
for storing a binary heap!
We will put the root at index 1 instead of
index 0 (this makes the math work out just
a bit nicer).
12
22 43
5
11
8
10
13
14
5 10 8 12 11 14 13 22 43
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Heap Operations
smallest key.
heap.
element from h.
12
22 43
5
11
8
10
13
14
Heap Operations: min()
min()
Just return the root!
return heap[1] 12
22 43
5
11
8
10
13
14
5 10 8 12 11 14 13 22 43
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Heap Operations: insert(k)
Insert item at element array[heap.size()+1]
(this probably destroys the heap property)
Perform a “ bubble up ” or “ up-heap ”
operation:
Compare the added element with its parent
if in correct order, stop
If not, swap and repeat
Heap Operations: insert(k)
12
22 43
5
11
8
10
13
14
12
22 43
5
11
8
10
13
14
5 10 8 12 11 14 13 22 43
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Start by inserting the key at the first empty
position. This is always at index
heap.size()+.
Heap Operations: insert(9)