All lessons

9. Heaps

Heap structure

0 of 5 activities0%

Reading 1

Array as tree

Open

A binary heap is a complete tree stored in an array. Index i: left 2i+1, right 2i+2, parent (i-1)/2.

Max-heap: parent ≥ children. Min-heap: parent ≤ children.

Used for priority queues and heap sort.

int left(int i){ return 2*i+1; }
int right(int i){ return 2*i+2; }
int parent(int i){ return (i-1)/2; }

Check 2

Left child

Open

In 0-based heap array, left child of i is

Fill in 3

Shape

Open

A heap’s tree shape is

Try it 4

Children of 0

Open

Indexes.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Left child index

Open

Read i. Print 2*i+1.

main.cpp
Loading editor…