0% found this document useful (0 votes)
38 views5 pages

7 Heaps

This document discusses binary heaps, including their definition as a nearly complete binary tree stored in an array. It covers implementing heaps using arrays, maintaining the heap property when modifying nodes, building a heap from an unsorted array in a bottom-up manner, inserting new nodes and restoring the heap property, deleting and returning the maximum value by exchanging with the last node, and using heaps to implement a priority queue and the heapsort algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

7 Heaps

This document discusses binary heaps, including their definition as a nearly complete binary tree stored in an array. It covers implementing heaps using arrays, maintaining the heap property when modifying nodes, building a heap from an unsorted array in a bottom-up manner, inserting new nodes and restoring the heap property, deleting and returning the maximum value by exchanging with the last node, and using heaps to implement a priority queue and the heapsort algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

University of the Philippines Open University

CMSC 204: Data Structures and Algorithms

HEAPS

DEFINITION

A BINARY HEAP is an ARRAY OBJECT that represents an almost complete binary


tree.

ALMOST COMPLETE/BALANCED AS POSSIBLE


 All levels of the tree (except the last one) are FULLY FILLED
 The last level is filled from left to right

WHAT MAKES A HEAP A HEAP?


 The values stored in each node are always NO GREATER THAN (i.e. less than
or equal to) the value of its parent. (Max Heap)
 The values stored in each node are always NO LESS THAN the value of its
parent. (Min Heap)

IMPLEMENTATION

Binary heaps are implemented using ARRAYS unlike Binary Search Trees and AVL
Trees.

The elements in a binary heap are indexed by LEVEL ORDER and from LEFT TO
RIGHT.
University of the Philippines Open University
CMSC 204: Data Structures and Algorithms

REMEMBER THESE

The root of the tree is indexed 1.

If a node has index j

1. The parent of j is indexed L j/2 ˩ (i.e. floor function, ex. 5/2 is 2.5 and the
floor is 2)
2. The right child of j is indexed 2j+1
3. The left child of j is indexed 2j

EXAMPLE

int[] heap = new int[8] {0, 60, 20, 15, 3, 5, 1, 7};

0 60 20 15 3 5 1 7
0 1 2 3 4 5 6 7

 Root == heap[1] == 60
 heap[3]; //node 15
 parent of node 15 is heap[1] //heap[L 3/2 ˩]; 3/2 = 1.5, floor of 1.5 is 1
 right child of node 15 is heap[7] //heap[2*3+1]; 2*3+1 = 6+1 = 7
 left child of node 15 is heap[6] //heap[2*3]; 2*3 = 6
 try the formula on the other elements in the heap
University of the Philippines Open University
CMSC 204: Data Structures and Algorithms

MAINTAINING THE BINARY HEAP PROPERTY

“Given a binary heap, when the value of one of the nodes is modified, this may
violate the heap property. In the heap, it is usually the root that is modified or
deleted. Once this happens, the heap property must be restored.”

Steps (Build Heap):

1. Start from the root. Compare the node with its children. If the node is the
maximum, stop.
2. If the left child is the maximum, swap value of the left child with the value
of the parent node. Continue checking if the heap property still holds in the
left subtree (recursively).
3. If the right child is the maximum, swap the value of the right child with the
value of the parent node. Continue checking if the heap property still holds
in the right subtree (recursively).

How do you translate these in code?

BUILDING A BINARY HEAP (Build-Heap)

“A binary heap can be constructed by restoring the heap property on subtrees


with heights one, then restoring the heap property on subtrees with heights two,
and so on until the heap property is restored on the whole tree.”

Note: Start from the bottom up, i.e. Percolate up!

How do you translate these in code?


University of the Philippines Open University
CMSC 204: Data Structures and Algorithms

INSERTING A NODE INTO A BINARY HEAP

When a new element is inserted, the new element is inserted at position


heapsize+1

NOTE: Inserting the new element will possibly destroy the heap property. Hence,
the heap must be restored after its insertion.

Steps:

1. Insert element at position heapsize+1


2. Compare element with its parent. If greater than parent, swap with parent.
If still greater, continue to swap until you reach the root. (Percolate up!)

RETURNING AND DELETING THE MAXIMUM

One of the binary heaps major applications is the priority queue. A priority queue
is a queue wherein the element with the highest priority (or highest value) is
returned first.

This is easily done by returning or deleting the maximum from a binary heap.

Return maximum – Simply return the value found in heap[1] (the root)

Delete maximum – Exchange the values of heap[1] and heap[heapsize] (the first
and last values in the heap) and then perform the restore heap operation on
heapsize-1. (Percolate down!)

HEAPSORT

A sorting algorithm based on heaps


University of the Philippines Open University
CMSC 204: Data Structures and Algorithms

Steps:

1. Perform a buildheap operation on the array


2. Perform the delete operation (note that the maximum is simply exchanged
with the last element in the array.
3. Performing the delete operation means you perform the restore heap
operation on heapsize-1
4. Perform the delete operation on the “new maximum”...

Watch this:
https://www.youtube.com/watch?v=q7R_upR81FU

Try this:
http://visualgo.net/heap.html

Read:
Module 7 (Page69) of CMSC 204 Manual

Takeaway Thoughts and Questions:

1. What new knowledge about heaps is the most interesting for you?
2. How will you apply heaps?

You might also like