How to construct the Min heap of the following:
82, 90, 10, 12, 15, 77, 55, 23
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hey!
What I would recommend here is to start by checking out this helpful tutorial: Min Heap Binary Tree Tutorial.
In general, a min heap is structured so that each parent node is smaller than its child nodes, with the smallest element at the root. For example, as you insert each number, swap elements as needed to keep this order.
For your exact question, this would look like:
82
, 90
, 10
, 12
, 15
, 77
, 55
, 23
one by one.Hope this helps!
- Bobby
Heya,
To construct a Min-Heap from the list of values (82, 90, 10, 12, 15, 77, 55, 23), we’ll insert each element one-by-one, ensuring that the heap property is maintained at each step. The Min-Heap property requires that each parent node is less than or equal to its child nodes.
Let’s go through the insertion and rebalancing process for each element:
Start with an empty Min-Heap.
Insert 82:
[82]
Insert 90:
[82, 90]
Insert 10:
[82, 90, 10]
[10, 90, 82]
Insert 12:
[10, 90, 82, 12]
Insert 15:
[10, 90, 82, 12, 15]
Insert 77:
[10, 90, 82, 12, 15, 77]
Insert 55:
[10, 90, 82, 12, 15, 77, 55]
Insert 23:
[10, 90, 82, 12, 15, 77, 55, 23]
After all insertions, the heap structure, arranged to meet the Min-Heap property, is:
10
/ \
12 77
/ \ / \
23 15 90 55
\
82
So, the array representation of this Min-Heap is:
[10, 12, 77, 23, 15, 90, 55, 82]
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.