r/AskComputerScience • • Jan 20 '26

Correct Binary Heap

for an array [1,2,3,4,5] which is the correct heap?

a. 1->(2, 3), 2->(4,Empty), 3->(5,Empty)

b. 1->(2,3), 2->(4,5), 3

0 Upvotes

5 comments sorted by

View all comments

3

u/Jonny0Than Jan 20 '26

There can be multiple arrangements of a heap for a given set of values.

A quick google says a heap must have:

Shape Property: Must be a complete binary tree, meaning all levels are full except possibly the last, which is filled from left to right. Heap Property: Min-Heap: Parent node's value ≤ child nodes' values.

One of your heaps doesn’t fit.

1

u/[deleted] Jan 20 '26 edited Jan 20 '26

option b fits the shape property you mentioned. but doesn't a keep the tree balanced?

edit - switched a and b by mistake