Back to General discussions forum
Hello.
In example this problem (#87) we have an array of values (3, 5, 4, 2, 8) and we want to build a tree from it.
But, when I read http://www.ideserve.co.in/learn/heap-sort or http://www.stoimen.com/blog/2012/08/07/computer-algorithms-heap-and-heapsort-data-structure/ or many other - there are other way to build Binary Tree:
Mapping heap to array A heap is stored in array level by level. The topmost level contains root only. It is mapped to the first element of an array (with index 0). Root's children are mapped to the second and third elements and so on.
And it shoud be like this:
__________________3
_______________5____4
_____________2___8
What is wrong?
Hi Viach,
While there are other ways to build a binary tree, for this problem you have to build it as described in the problem description, so that all left children of a node contain smaller values, while all right children contain greater values.
Hope this helps
It should be pointed out that a binary tree simply means that it is a tree such that each node has no more than two children. Strictly speaking, we are building a binary search tree, which is a special case of the binary tree.