0% found this document useful (0 votes)
12 views

Heaps

Uploaded by

ricolaurencejay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Heaps

Uploaded by

ricolaurencejay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CCC121

Heaps

MS. APPLE ROSE B. ALCE


Faculty, Dept. of Computer Applications
College of Computer Studies, MSU-IIT
What is a HEAP?
Structure property

A heap is a binary tree that is completely filled,


with the possible exception of the bottom level, which
is filled from left to right. Such a tree is known as
a complete binary tree.

A complete binary tree of height h has between 2h and


2h+1 – 1 nodes.

Data Structures and Algorithms A. ALCE


What is a HEAP?
Structure property
A complete binary tree can be represented
in an array where no links are necessary.

Array Implementation A complete binary tree

Data Structures and Algorithms A. ALCE


What is a HEAP?
Structure property

For any element in array position i, the left child is


in position 2i, the right child is in the cell after
the left child (2i + 1), and the parent is in position
i/2.

Thus, links not required liked that of a linked list.


However, with this implementation, an estimate of the
maximum heap size is required in advance.

Data Structures and Algorithms A. ALCE


What is a HEAP?
Structure property

For any element in array position i, the left child is


in position 2i, the right child is in the cell after
the left child (2i + 1), and the parent is in position
i/2.(A)
Or it could be that for any element in array position
i, the left child is in position 2i + 1, the right
child is in the cell after the left child (2i + 2),
and the parent is in position (i-1)/2. (B)

A B C D E F G H I J (B)
0 1 2 3 4 5 6 7 8 9 10 11 12 13

(A)

Data Structures and Algorithms A. ALCE


What is a HEAP?
HEAP Order Property
The property that allows operations to be performed quickly
is the heap-order property. Since we want to be able to
find the minimum quickly, it makes sense that the smallest
element should be at the root. If we consider that any
subtree should also be a heap, then any node should be
smaller than all of its descendants.

Two complete trees (only one tree is a heap)

Data Structures and Algorithms A. ALCE


What is a HEAP?
HEAP Order Property

Two complete trees (only one tree is a heap)

In a heap, for every node X, the key in the parent of X is


smaller than (or equal to) the key in X, with the exception
of the root (which has no parent. The tree on the left is a
heap, but the tree on the right is not (the dashed line shows
the violation of heap order).By the heap-order property, the
minimum element can always be found at the root.

Data Structures and Algorithms A. ALCE


What is a HEAP?
HEAP Order Property

Two complete trees (only one tree is a heap)

Array Implementation

13 21 16 24 31 19 68 65 26 32
0 1 2 3 4 5 6 7 8 9 10 11 12 13

Data Structures and Algorithms A. ALCE


Operations of Heap
•heapify
- rearranges the elements in the heap to maintain
the heap property.
•insert
- adds an item to a heap while maintaining its
heap property.
•Delete
- removes an item in a heap.
•Extract
- returns the value of an item and then deletes it
from the heap.

Data Structures and Algorithms A. ALCE


Operations of Heap
•isEmpty
- boolean, returns true if boolean is empty and
false if it has a node.
•size
- returns the size of the heap.
•getMax()
- returns the maximum value in a heap

Data Structures and Algorithms A. ALCE


Operations of Heap
MinHeap Operation

In the min heap property, the parent node is always less


than the key at both child nodes. To build a min heap we:

•Create a new child node at the end of the heap (last level).
•Add the new key to that node (append it to the array).
•Move the child up until you reach the root node and the heap
property is satisfied.

Data Structures and Algorithms A. ALCE


Operations of Heap
MinHeap Operation

To remove/delete a root node in a min heap:

•Delete the root node.


•Move the key of the last child to root.
•Compare the parent node with its children.
•If the value of the parent is greater than child nodes, swap
them, and repeat until the heap property is satisfied.

Data Structures and Algorithms A. ALCE


Operations of Heap
MaxHeap Operation

Elements in a max heap follow the max heap property. The


parent node is always greater than the key at both child
nodes. To build a max heap, you:

• Create a new node at the beginning (root) of the heap.


• Assign it a value.
• Compare the value of the child node with the parent node.
• Swap nodes if the value of the parent is less than that of
either child (to the left or right).
• Repeat until the largest element is at the root parent
nodes (then you can say that the heap property holds).

Data Structures and Algorithms A. ALCE


Operations of Heap
MaxHeap Operation

To remove/delete a root node in a Max Heap, you:

• Delete the root node.


• Move the last child node of the last level to root.
• Compare the parent node with its children.
• If the value of the parent is less than child nodes, swap
them, and repeat until the heap property is satisfied.

Data Structures and Algorithms A. ALCE


Operations of Heap
Inserting a Node in the Heap

Push NEW VALUE by adding a node at the end of an array


The new value is added to the end of the array using
heap.push_back(value).

Percolate by swapping

The newly added value is compared with its parent (using the formula
(hole - 1) / 2).
• If the new value is smaller than the parent, the positions are
swapped to restore the Min-Heap property.
• This process continues until the new value is greater than or
equal to its parent, or the new value becomes the root.

Data Structures and Algorithms A. ALCE


Operations of Heap
Inserting a Node in the Heap
For instance, we have input: Insert 10, 20, 5, 15

Step 1: Insert 10
• Heap: [10] (only one element, so it’s already a valid Min-Heap).
Step 2: Insert 20
• New value 20 is added to the end: [10, 20].No swap needed since 20 > 10.
Step 3: Insert 5
• New value 5 is added: [10, 20, 5].
• Percolate up:5 is smaller than 10 (its parent), so they are swapped.
• Final Heap: [5, 20, 10].
Step 4: Insert 15
• New value 15 is added: [5, 20, 10, 15].
• Percolate up:15 is smaller than 20 (its parent), so they are swapped.
• Final Heap: [5, 15, 10, 20].

Data Structures and Algorithms A. ALCE


Minimum Heap

BOOK 30
BOOK 5
BOOK 21

Output Terminal

Data Structures and Algorithms


Assignment
To be submitted on December 10, 8:00AM via MOLE Submission bin.

Instruction:
Program a medium-sized C++ code similar to the sample code provided for a MinHeap. This
time, implement a MaxHeap, ensuring that the value of each parent node is greater than or
equal to the values of its child nodes (left and right).

In your submission, take note of the ff.


• Header Section: At the top left of your document, include the following details: Name, Subject and
Section. On the next line, Assignment on MaxHeapTask.
• Part 1: Copy and paste your MaxHeap C++ code into this section. Ensure the code:Is properly
formatted with clear comments explaining key sections.
• Part 2: Take a clear screenshot of the terminal output and paste the screenshot into this section.
• Submission: Save your document as a .docx file. Title the file as: MaxHeap_Lastname_Firstname.
Submit the file via MOLE (alternative: use email) before or on the deadline.

Data Structures and Algorithms


Reference
For consultation, contact me thru email.

Email Address: [email protected]


Email Subject: CCC121 <Purpose>
Ex. CCC121 Lab3 Submission

Data Structures and Algorithms

You might also like