This is not homework question, I today learned heap data structure and I don't know how would I prove that relation is true. Thanks.
A Min-Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node. Mapping the elements of a heap into an array is trivial: if a node is stored an index k, then its left child is stored at index 2k + 1 and its right child at index 2k + 2.
Definition: A heap is a specialized tree-based data structure that satisfied the heap property: if B is a child node of A, then key(A) ≥ key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap.
The height is de ned as the number of edges in the longest simple path from the root. The number of nodes in a complete balanced binary tree of height h is 2h+1 ;1. Thus the height increases only when n = 2lgn, or in other words when lgn is an integer.
A binary tree is a tree in which every node has at most 2 children i.e., the left child and the right child. For example, in the above picture, the node 'B' has 2 children, node 'D' has 1 child and node 'G' has 0 children. Since every node has at most 2 children, so the tree is a binary tree.
Prove by Induction:
Proof of 3: since children of kth elements are at 2k and 2k+1 (based on assumption) then, next two elements after are 2k+2 and 2k+3.
2k+2 = 2(k+1) (first child for k+1 is proved)(a)
2k+3 = 2k + 2 + 1 = 2(k+1) + 1 (second child for k+1 is proved)(b)
from (a) & (b) --> thus 3 is valid thus child of element n are 2n and 2n+1
Here is a proof without induction, which is more intuitive and insightful from my point of view. Here are 4 mental steps that proof this relation to me.
Let's enumerate all the vertices of a tree in the following way:
Here is how these verices will look in array:
We can notice that every two consecutive nodes (except the first one) are children of some common vertex:
1 [ 2 3 ] [ 4 5 ] [ 6 7 ] [ 8 9 ]
Now let's think in reverse order. What if we have an array of pairs: [(2, 3), (4 5), (6, 7), (8, 9)] and we want to aggregate them in a single array?
Let's say we have already placed the first 3 pairs:
[ 2 3 ] [ 4 5 ] [ 6 7 ]
What will be the index of the number 8 in the final array? We know that we have placed 3 pairs already and they have taken 3 * 2 = 6 places in the beginning, so we will take the 7'th place.
If we code the placement of pairs, it will look like that:
pair<int, int> pairs[4] = { {2, 3}, {4, 5}, {6, 7}, {8, 9} };
int aggregate[2 * 4];
for (int i = 0; i < 4; i++)
{
aggregate[2 * i] = pairs[i].first;
aggregate[2 * i + 1] = pairs[i].second;
}
The key part is this index of array aggregate[2 * i]. In this code it is obvious to see why we multiply by 2 — we do it, because we have to skip the previous i - 1 pairs.
Now we need to see the correspondance between building aggregate array of pairs and flattening of a binary tree. Every pair of siblings has a parent. If two siblings (2, 3) and (4, 5) are adjacent in array, then theirs parents are also adjacent. Siblings (2, 3) have parent 1, siblings (4, 5) have parent 2, siblings (6, 7) have parent 3. So the parent is like an index in this array of pairs pairs[4] = { (2, 3), (4, 5), (6, 7), (8, 9) }
and when we use 2 * i to access its left child, we can think of skipping i - 1 pairs of children generated by previous vertices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With