The root node of a max-heap contains the largest value. Each child is smaller than the parent. We’ll start with the same array that we had in the previous few articles. We’ll first construct the heap. Next, we’ll start constructing the max-heap. Just like when we were constructing the min-heap, we’ll start with the 3rd node: floor(6/2) = floor(3) = 3 Since 9 has only one child node, 9 will be compared to 3. If the parent is less than the child, the nodes will be swapped. Since 9 is larger than 3, the nodes are not swapped. We move to the
Tag: Max Heap
Let’s use the same array that we used to construct the min-heap to create the max-heap from an array. We start by adding the first node, 7. We move top-to-bottom, left-to-right and we add the 2nd node, 2. Since 7 is larger than 2, the nodes remain in their current position. Next, 9 is added to the heap. Since 9 is larger than 7, the two nodes are swapped. Next, 4 is added as a child of 2. Since 4 is larger than 2, the two nodes are swapped. We observe that 4 moved up, so 4 is also compared to 9.