You are on page 1of 32

Binary Heap with TDD

Who am I?
Objective of today’s sharing
What is Binary Heap used for?
What is a Binary
Heap?
100

55 36

43 33 25 1

2 7
Rules (Constraints)
of Binary Heap
100

55 36

43 33 25 1

Shape property
Heap property
2 7
Rules (Constraints)
of Binary Heap
100

55 36

All levels of the tree, except possibly the last one 43 33 25 1


(deepest) are fully filled, and,
If the last level of the tree is not complete, the
nodes of that level are filled from left to right.
2 7
Rules (Constraints)
of Binary Heap
100

55 36

1. the key stored in each node is either greater than 43 33 25 1


or equal to (≥) or less than or equal to (≤) the
keys in the node's children
2. Max-Heap & Min-Heap
2 7
Operations of Binary
Heap
100

55 36

1.

43 33 25 1

2 7
Operations of Binary
Heap (Insert) 100

55 36

43 33

45
Operations of Binary
Heap (Insert) 100

55 36

43 33 45
Operations of Binary
Heap (Insert) 100

55 45

43 33 36
Operations of Binary
Heap (Insert) 100

55 45

43 33 36
Operations of Binary
Heap (Extract) 100

55 45

43 33 36
Operations of Binary
Heap (Extract) 36

55 45

43 33
Operations of Binary
Heap (Extract) 36

55 45

43 33
Operations of Binary
Heap (Extract) 55

36 45

43 33
Operations of Binary
Heap (Extract) 55

43 45

36 33
Operations of Binary
Heap (Extract) 55

43 45

36 33
Recap: Binary Heap
Writing Tests!
Writing Tests!

100 55 36 43 33 43 33
Let’s write some tests for
real!
Test Data
100

55 36

43 33
Insert(45)
100

55 45

43 33 36
Test Data
100

55 36

43 33
Extract
55

43 36

33
Solution
Feedbacks are
welcome!
Title
100

19 36

17 3 25 1

2 7
100

55 36

43 33

100

19 36

3 25 1

You might also like