Max-Heaps | Codecademy (2024)

Learn about the max-heap data structure.

In this article, we’ll learn about the structure of a max-heap as well as how elements can be added to a max-heap.

Introduction to heaps

Heaps are used to maintain a maximum or minimum value in a dataset.

Imagine you have a demanding boss (hopefully this is theoretical!). They always want the most important thing done. Of course, once you finish the most important task, another one takes its place.

You can manage this problem using a priority queue to ensure you’re always working on the most pressing assignment and heaps are commonly used to create a priority queue.

Heaps that track the maximum value in a dataset are max-heaps while heaps that track the minimum value are referred to as min-heaps. We will focus on max-heaps in this article, but the concepts for a min-heap are nearly identical.

Think of the max-heap as a binary tree with two qualities:

  • The root is the maximum value of the dataset.
  • Every parent’s value is greater than its children.

These two properties are the defining characteristics of the max-heap. By maintaining these two properties, we can efficiently retrieve and update the maximum value.

Heap representations

We can picture max-heaps as binary trees, where each node has at most two children. As we add elements to the heap, they’re added from left to right until we’ve filled the entire level.

Max-Heaps | Codecademy (1)

Note: The examples in this article use numbers since this is a straightforward value, but heaps have many practical applications.

At the top, we have our root value, 11. Then, we’ve filled the next level containing the root value’s children, 10 and 9. The next addition will be added as the left child of 10, starting a new level in the tree. We would continue filling this level from left to right until 9 had its right child filled.

Conceptually, the tree representation is beneficial for understanding. Practically, we implement heaps in a sequential data structure like an array or list for efficiency.

Notice how by filling the tree from left to right; we’re leaving no gaps in the array. The location of each child or parent derives from a formula using the index.

  • left child: (index * 2) + 1
  • right child: (index * 2) + 2
  • parent: (index - 1) / 2not used on the root!

Adding an element

Sometimes you will add an element to the heap that violates the heap’s essential properties.

We’re adding 18 as a left child of 2, which violates the max-heap property that parents must be larger or equal to their child.

We need to restore the fundamental heap properties. This restoration is known as heapify or heapifying. We’re adding an element to the bottom of the tree and moving upwards, so we’re heapifying up.

As long as we’ve violated the heap properties, we’ll swap the offending child with its parent until we restore the properties, or until there’s no parent left. If there is no parent left, that element becomes the new root of the tree.

18 swaps with 2, but there’s still work to do because now 18 is a child of 11. One more swap and we’ve restored the heap properties. The child value, 18, is lesser than the parent and root of the tree, 20. We can see that 18‘s children 11 and 10 are also smaller than their parent.

Max-Heaps | Codecademy (2)

Review

Nice job reaching the end of this article! Let’s review what we learned:

  • A max-heap tracks the maximum element as the element within a data set.
  • Max-heaps must maintain the heap property that the parent values must be greater than their children.
  • When adding elements, we use .heapify_up() to compare the new element with its parent; if it violates the heap property, then we must swap the two values.

The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.

Max-Heaps | Codecademy (2024)
Top Articles
Landlord insurance in Victoria
30 Foods You Should Never Eat Past the Expiration Date
55Th And Kedzie Elite Staffing
Spn 1816 Fmi 9
Mcgeorge Academic Calendar
The Atlanta Constitution from Atlanta, Georgia
Summit County Juvenile Court
Ofw Pinoy Channel Su
Ingles Weekly Ad Lilburn Ga
Txtvrfy Sheridan Wy
Crime Scene Photos West Memphis Three
Amateur Lesbian Spanking
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vichatter Gifs
Pittsburgh Ultra Advanced Stain And Sealant Color Chart
Fredericksburg Free Lance Star Obituaries
TS-Optics ToupTek Color Astro Camera 2600CP Sony IMX571 Sensor D=28.3 mm-TS2600CP
Ups Access Point Lockers
Pay Boot Barn Credit Card
College Basketball Picks: NCAAB Picks Against The Spread | Pickswise
Del Amo Fashion Center Map
Kroger Feed Login
New Stores Coming To Canton Ohio 2022
Section 408 Allegiant Stadium
Obituaries, 2001 | El Paso County, TXGenWeb
How rich were the McCallisters in 'Home Alone'? Family's income unveiled
Till The End Of The Moon Ep 13 Eng Sub
Dentist That Accept Horizon Nj Health
Abga Gestation Calculator
Grandstand 13 Fenway
Baldur's Gate 3 Dislocated Shoulder
Rocksteady Steakhouse Menu
new haven free stuff - craigslist
Bozjan Platinum Coins
Gabrielle Enright Weight Loss
Newcardapply Com 21961
The 38 Best Restaurants in Montreal
Hannibal Mo Craigslist Pets
How much does Painttool SAI costs?
Conroe Isd Sign In
Froedtert Billing Phone Number
San Bernardino Pick A Part Inventory
Gravel Racing
Ghareeb Nawaz Texas Menu
Walmart 24 Hrs Pharmacy
Flappy Bird Cool Math Games
What is 'Breaking Bad' star Aaron Paul's Net Worth?
Gt500 Forums
Spn 3464 Engine Throttle Actuator 1 Control Command
View From My Seat Madison Square Garden
Strange World Showtimes Near Century Federal Way
Hcs Smartfind
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 5888

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.