You are on page 1of 44

HEAPS

Lecture 9
Dr. Adil Yousif

Full v.s. Complete Binary Trees

A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node other than the leaves has two children.

Complete binary tree

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Heaps

Heaps are a specialized complete tree-based data structure that satisfies the heap property. The heap property is : If A is a parent node of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap. Either the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node (this kind of heap is called max heap) Or the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node (min heap).

Example of Max-Heap

Heap Ordering

As shown in the graphic, there is no implied ordering between siblings or cousins and no implied sequence for an in-order traversal (as there would be in, e.g., a binary search tree). The heap relation mentioned above applies only between nodes and their immediate parents. The maximum number of children each node can have depends on the type of heap, but in many types it is at most two, which is known as a "binary heap"

Max Heap with 9 Nodes


7

Min Heap with 9 Nodes


8

Array Representation of Heap


9

A heap is efficiently represented as an array.

Array Representation of Heap

Array Representation of Heap

Array Representation of Heap

Constructing a heap

A tree consisting of a single node is automatically a heap We construct a heap by adding nodes one at a time:
Add

the node just to the right of the rightmost node in the deepest level If the deepest level is full, start a new level

Examples:

Add a new node here

Add a new node here

Constructing a heap III


8 10
1

8 8

10 8
2

10 5
3

10 8 12 5 8 12

10 5 8 10

12 5

Other children are not affected


12 10 8 14 5 8 14 10 12 5 8 12 10 14 5

The node containing 8 is not affected because its parent gets larger, not smaller The node containing 5 is not affected because its parent gets larger, not smaller The node containing 8 is still not affected because, although its parent got smaller, its parent is still greater than it was originally

Another Example: Insertion into a Max Heap


16

New element is 5 Are we finished?

Insertion into a Max Heap


17

20

New element is 20 Are we finished?

Insertion into a Max Heap


18

20

Exchange the positions with 7 Are we finished?

Insertion into a Max Heap


19

20

7 Exchange the positions with 8 Are we finished?

Insertion into a Max Heap


20

20

Exchange the positions with 9 Are we finished?

Removing the root


Notice that the largest number is now in the root Suppose we discard the root:
11 22 17

19 18

22 14 21 3 9

14 11

15

How can we fix the binary tree so it is once again balanced and left-justified? Solution: remove the rightmost leaf at the deepest level and use it for the new root

The reHeap method I


Our tree is balanced and left-justified, but no longer a heap However, only the root lacks the heap property
11 22 17

19 18

22 14 21 3 9

14

15

We can siftUp() the root After doing this, one and only one of its children may have lost the heap property

The reHeap method II

Now the left child of the root (still the number 11) lacks the heap property
22 11 17

19 18

22 14 21 3 9

14

15

We can siftUp() this node After doing this, one and only one of its children may have lost the heap property

The reHeap method III

Now the right child of the left child of the root (still the number 11) lacks the heap property:
22 22 17

19 18

11 14 21 3 9

14

15

We can siftUp() this node After doing this, one and only one of its children may have lost the heap property but it doesnt, because its a leaf

The reHeap method IV

Our tree is once again a heap, because every node in it has the heap property
22 22 17

19 18

21 14 11 3 9

14

15

Once again, the largest (or a largest) value is in the root We can repeat this process until the tree becomes empty This produces a sequence of values in order largest to smallest

Another Example: Deletion from a Max Heap


26

20

15

Max element is in the root What happens when we delete an element?

Deletion from a Max Heap


27

15

After the max element is removed. Are we finished?

Deletion from a Max Heap


28

15

Heap with 10 nodes. Reinsert 8 into the heap.

Deletion from a Max Heap


29

15

Reinsert 8 into the heap. Are we finished?

Deletion from a Max Heap


30

15

Exchange the position with 15 Are we finished?

Deletion from a Max Heap


31

15

Exchange the position with 9 Are we finished?

Max Heap Initialization


32

Heap initialization means to construct a heap by adjusting the tree if necessary Example: input array = [-,1,2,3,4,5,6,7,8,9,10,11]

Max Heap Initialization


33

- Start at rightmost array position that has a child. - Index is floor(n/2).

Max Heap Initialization


34

Max Heap Initialization


35

Max Heap Initialization


36

Max Heap Initialization


37

Max Heap Initialization


38

Are we finished? Done!

Heapsort

Goal:
Sort

an array using heap representations

Idea:
Build Swap

a max-heap from the array


the root (the maximum element) with the last this last node by decreasing the heap size

element in the array


Discard Call

MAX-HEAPIFY on the new root

Repeat

this process until only one node remains

39

Example:

A=[7, 4, 3, 1, 2]

MAX-HEAPIFY(A, 1, 4)

MAX-HEAPIFY(A, 1, 3)

MAX-HEAPIFY(A, 1, 2)

MAX-HEAPIFY(A, 1, 1)

40

Mapping into an array


25 22 17

19

22

14

15

18
0 1

14
2 3

21
4 5 6

3
7

9
8 9

11
10 11 12

25 22 17 19 22 14 15 18 14 21 3

9 11

Notice:
The left child of index i is at index 2*i+1 The right child of index i is at index 2*i+2 Example: the children of node 3 (19) are 7 (18) and 8 (14)

Removing and replacing the root


The root is the first element in the array The rightmost node at the deepest level is the last element Swap them...
0 1 2 3 4 5 6 7 8 9 10 11 12

25 22 17 19 22 14 15 18 14 21 3

9 11

10

11

12

11 22 17 19 22 14 15 18 14 21 3

9 25

...And pretend that the last element in the array no longer existsthat is, the last index is 11 (9)

Reheap and repeat

Reheap the root node (index 0, containing 11)...


0 1 2 3 4 5 6 7 8 9 10 11 12

11 22 17 19 22 14 15 18 14 21 3
0 1 2 3 4 5 6 7 8 9 10

9 25
11 12

22 22 17 19 21 14 15 18 14 11 3

9 25

10

11

12

9 22 17 19 22 14 15 18 14 21 3 22 25

...And again, remove and replace the root node Remember, though, that the last array index is changed Repeat until the last becomes first, and the array is sorted!

Questions

You might also like