We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
But yea your code does move around smaller values and towards the root of the heap, which is why I thought it could be more efficient.
As far as I know one cannot maintain heap property after inserting an element and manipulating a part of the tree. You would have to do heapify of all nodes starting from last/2.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
QHEAP1
You are viewing a single comment's thread. Return to all comments →
But when you are swapping variables it won't preserve heap property.
I guess I can only use an array for showing it here.
But yea your code does move around smaller values and towards the root of the heap, which is why I thought it could be more efficient.
As far as I know one cannot maintain heap property after inserting an element and manipulating a part of the tree. You would have to do heapify of all nodes starting from last/2.