Why does a Binary Heap has to be a Complete Binary Tree?
Asked Answered
R

8

12

The heap property says:

If A is a parent node of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap. Either the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node (this kind of heap is called max heap) or the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node (min heap).

But why in this wiki, the Binary Heap has to be a Complete Binary Tree? The Heap Property doesn't imply that in my impression.

Rikkiriksdag answered 14/8, 2014 at 23:47 Comment(1)
"The tree is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right." It's just trying to preserve good runtime performance by keeping the heights of arbitrary nodes close to their ideal.Taille
G
9

According to the wikipedia article you provided, a binary heap must conform to both the heap property (as you discussed) and the shape property (which mandates that it is a complete binary tree). Without the shape property, one would lose the runtime advantage that the data structure provides (i.e. the completeness ensures that there is a well defined way to determine the new root when an element is removed, etc.)

Gallicanism answered 14/8, 2014 at 23:59 Comment(0)
F
5

Every item in the array has a position in the binary tree, and this position is calculated from the array index. The positioning formula ensures that the tree is 'tightly packed'.

For example, this binary tree here:

enter image description here

is represented by the array

[1, 2, 3, 17, 19, 36, 7, 25, 100].

Notice that the array is ordered as if you're starting at the top of the tree, then reading each row from left-to-right.

If you add another item to this array, it will represent the slot below the 19 and to the right of the 100. If this new number is less than 19, then values will have to be swapped around, but nonetheless, that is the slot that will be filled by the 10th item of the array.


Another way to look at it: try constructing a binary heap which isn't a complete binary tree. You literally cannot.
Fluorometer answered 14/8, 2014 at 23:58 Comment(0)
V
5

You can only guarantee O(log(n)) insertion and (root) deletion if the tree is complete. Here's why:

If the tree is not complete, then it may be unbalanced and in the worst case, simply a linked list, requiring O(n) to find a leaf, and O(n) for insertion and deletion. With the shape requirement of completeness, you are guaranteed O(log(n)) operations since it takes constant time to find a leaf (last in array), and you are guaranteed that the tree is no deeper than log2(N), meaning the "bubble up" (used in insertion) and "sink down" (used in deletion) will require at most log2(N) modifications (swaps) of data in the heap.

This being said, you don't absolutely have to have a complete binary tree, but you just loose these runtime guarantees. In addition, as others have mentioned, having a complete binary tree makes it easy to store the tree in array format forgoing object reference representation.

Vaporize answered 30/9, 2017 at 19:16 Comment(0)
T
1

The point that 'complete' makes is that in a heap all interior (not leaf) nodes have two children, except where there are no children left -- all the interior nodes are 'complete'. As you add to the heap, the lowest level of nodes is filled (with childless leaf nodes), from the left, before a new level is started. As you remove nodes from the heap, the right-most leaf at the lowest level is removed (and pushed back in at the top). The heap is also perfectly balanced (hurrah!).

A binary heap can be looked at as a binary tree, but the nodes do not have child pointers, and insertion (push) and deletion (pop or from inside the heap) are quite different to those procedures for an actual binary tree.

This is a direct consequence of the way in which the heap is organised. The heap is held as a vector with no gaps between the nodes. The parent of the i'th item in the heap is item (i - 1) / 2 (assuming a binary heap, and assuming the top of the heap is item 0). The left child of the i'th item is (i * 2) + 1, and the right child one greater than that. When there are n nodes in the heap, a node has no left child if (i * 2) + 1 exceeds n, and no right child if (i * 2) + 2 does.

The heap is a beautiful thing. It's one flaw is that you do need a vector large enough for all entries... unlike a real binary tree, you cannot allocate a node at a time. So if you have a heap for an indefinite number of items, you have to be ready to extend the underlying vector as and when needed -- or run some fragmented structure which can be addressed as if it was a vector.

FWIW: when stepping down the heap, I find it convenient to step to the right child -- (i + 1) * 2 -- if that is < n then both children are present, if it is == n only the left child is present, otherwise there are no children.

Tauten answered 14/8, 2014 at 23:58 Comment(0)
M
1

By maintaining binary heap as a complete binary gives multiple advantages such as 1.heap is complete binary tree so height of heap is minimum possible i.e log(size of tree). And insertion, build heap operation depends on height. So if height is minimum then their time complexity will be reduced.

2.All the items of complete binary tree stored in contiguous manner in array so random access is possible and it also provide cache friendliness.

Mitzi answered 13/8, 2020 at 8:56 Comment(0)
A
0

In order for a Binary Tree to be considered a heap two it must meet two criteria. 1) It must have the heap property. 2) it must be a complete tree.

It is possible for a structure to have either of these properties and not have the other, but we would not call such a data structure a heap. You are right that the heap property does not entail the shape property. They are separate constraints.

Afraid answered 15/8, 2014 at 0:2 Comment(0)
T
0

The underlying structure of a heap is an array where every node is an index in an array so if the tree is not complete that means that one of the index is kept empty which is not possible beause it is coded in such a way that each node is an index .I have given a link below so that u can see how the heap structure is built

http://www.sanfoundry.com/java-program-implement-min-heap/

Hope it helps

Thermotherapy answered 19/1, 2016 at 15:59 Comment(0)
A
0

I find that all answers so far either do not address the question or are, essentially, saying "because the definition says so" or use a similar circular argument. They are surely true but (to me) not very informative.

To me it became immediately obvious that the heap must be a complete tree when I remembered that you insert a new element not at the root (as you do in a binary search tree) but, rather, at the bottom right.

Thus, in a heap, a new element propagates from the bottom up - it is "moved up" within the tree till it finds a suitable place.

In a binary search tree a newly inserted element moves the other way round - it is inserted at the root and it "moves down" till it finds its place.

The fact that each new element in a heap starts as the bottom right node means that the heap is going to be a complete tree at all times.

Amide answered 8/2, 2023 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.