A
A
Arthur2016-12-06 15:53:24
Algorithms
Arthur, 2016-12-06 15:53:24

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

1 answer(s)
S
SagePtr, 2016-12-06
@Carver182

Depending on the order in which this heap should be loosely sorted. If at the top there should be an element with the maximum value of the key, then descend the element towards the larger one, if with the minimum - towards the smaller one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question