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
How to Get Cash From a Credit Card at an ATM
What is a Pitch Deck & What Makes It Fundable (+Examples)
Swissport Ess
Preggophili
Pinellas Fire Active Calls
High Stakes Homework With My Stepmom
Harry Potter: Magical Portraits, Explained
Virtual Prepaid Minutes
Restaurants Near Applebee's
Att U Verse Outage Map
Coulters Hole Rockland Pa
The Surprising Power of a Smile
Kino finden | cinetixx Filme
Temu Codes 2022
Isabella Schmeichel
Magellan Outdoors Men's Luther Ii Boots
Methstreams Boxing Stream
Hindi Links 4U
Magicseaweed Capitola
Votan's Minimap
Infinite Campus Oldham County Ky
1980 Monte Carlo For Sale Craigslist
Theater + Tickets - Phoenix Theatres Savoy 16 + IMAX - Phoenix Theatres Entertainment
Oodweynenews
Jesus Calling June 21St
Unemployment Benefits Indiana Log In
Bulloch County Police Reports
Blairsville Online Yard Sale
The Voice Season 22 Wiki
Sanctuary 2022 Showtimes Near Santikos Entertainment Palladium
Chickasaw Garbage And Sewer
Mchoul Funeral Home Of Fishkill Inc. Services
Dcf Training Number
Onze zorg - BCM Ouderenzorg
Best Restaurants In Ardmore Pa
Craigslist Michigan Com
Www.stantonoptical/Order-Status
Weather Past 3 Days
Saint Frances Cabrini West Bend Wi
Log in or sign up to view
Unknown Venmo Charges on Bank Statement
Go Karts For Sale Near Me Used
1988 Jaguar XJS Convertible. Location: The Netherlands .
DigBMX - Powers BMX Museum Tour with Jarred Raflik and Mike McHue
Mytp Saba Cloud
2005 Chevrolet Silverado Radio Wiring Diagram
Norwegian Luna | Cruise Ship
Gran Turismo Showtimes Near Century Rio 24 And Xd
Wild Fork Foods Login
Menteri LHK terbitkan aturan yang larang pejuang lingkungan dipidana, akankah menghentikan kriminalisasi? - BBC News Indonesia
Restored Republic Jan 3 2023
Fcs East Rhinos
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.