In: Computer Science
Max heap is a specialized fully binary tree in which every parent node contains greater than or equal value than its child node.
Steps to build a Max Heap ( Max Heap Insertion) are:
Step 1: Create a new node at the end of heap
Step 2: Assign new value to the node
Step 3: Compare the value of this child node with its parent node
Step 4: If the value of parent is less than child, then swap them
Step 5: Repeat step 3 ans step 4 until heap propery holds.
In the above example of building Max Heap, the elements are A={20,15,10,14,8,6,9}
Step 1: Take the first element 20, as the root node
Step 2: 15 and 10 are the child nodes of the root node 20. Now compare newnode (child) 15, with parent node 20. That means 15<20, so no swapping is required. Now compare newnode (child) 10, with parent node 20. That means 10<20, so no swapping is required.
Step 3: Child nodes of 15 are 14 and 8. Compare child node 14, with its parent node 15.Here, 14<15, so no swapping is needed. Now compare 8 with its parent node 15. Here, 8<15, so no swapping is needed.
Step 4: Child nodes of 10 are 6 and 9. Compare child node 6 with its parent node 10. Here 6<10, so no swapping is required. Now compare 9 with its parent node 10. Here 9<10, so no swapping is required.
Max Heap Insertion: