Answer the question
In order to leave comments, you need to log in
How is binary heap recovery (heapify method) done?
The hitch arose when the parent was smaller than both of its children. In which direction should it be lowered, towards a larger or smaller descendant? On different sites they write differently, on habré and on the wiki they write that towards the largest descendant, on several others that towards the smaller of the two, but somewhere, in general, they wrote towards the smaller / larger, somewhere just in the left subtree . I decided to build a tree myself based on a numerical sequence. I made it just a binary tree and then, using the heapify function (with leaving to the side of a larger child), turned it into a heap and, to check the correctness, the same thing, only by adding each node in turn. The results didn't match. Help me figure out how the algorithm should work, eh?
Sequence: 9-11-3-6-14-10-11-11-7-12-11-4
Using the heapify function:
14
/ \
12 11
/ \ / \
11 11 10 3
/ \ / \ /
6 7 9 11 4
Adding each node in turn:
14
/ \
12 11
/ \ / \
11 11 4 10
/ \ / \ /
6 7 9 11 3
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question