11

Here's my rough sketch of the beginning of a heap with arbitrary values

 0   1    2    3    4    5    6    7    8    9   ...
[-] [10] [14] [15] [22] [21] [24] [23] [44] [30] ...

Why must the element in array[0] always be set to null?
Or why is it that we are not supposed to use it?

Ernest
  • 1,370
  • 13
  • 23

3 Answers3

19

There are several ways to represent a binary heap as an array.

There is a way whereby element zero is used; there is also a way whereby element zero is not used:

  1. root is element 0; children of element n are elements 2n+1 and 2n+2;
  2. root is element 1; children of element n are elements 2n and 2n+1.

Neither is more "correct" than the other. The former is a better fit for programming languages that use zero-based arrays, whereas the latter is a better fit for languages with one-based arrays.

It would appear that you've encountered an implementation that uses the second approach. Since the language in question, Java, uses zero-based arrays, element zero exists but is not being used.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Well the second approach wastes perfectly good memory for no benefit whatsoever, so I'd say that makes it inferior to the first one. Still you're probably right – Voo Feb 27 '12 at 10:43
  • @Voo: The second approach isn't wasteful if the language uses 1-based arrays. There, array[1] is the first element, there isn't an array[0], and nothing is wasted. – Phil Cohen Mar 27 '12 at 23:38
  • @Philip Yeah if we were talking about such a language, true. But last time I checked java used the sensible approach to array indexing ;) More serious: My comment was with regard to an earlier version of the answer - it isn't much good now with the updated version I agree (you can look up old versions of the answer by clicking on the date next to "edited") – Voo Mar 27 '12 at 23:58
  • 5
    I would like to point out that using one-based indexing, operations `PARENT=[n/2]` and `LEFTCHILD=2n` can be implemented efficiently by bit shifts. – max Jul 24 '13 at 13:57
  • @Max you also can do bit shifts to 0-based indexing by subtracting 1 before shifting like this `(n - 1) >> 1` – Leo Oct 29 '16 at 06:48
4

This really boils down to a cultural difference between mathematicians and software engineers.

  • Conventionally, mathematicians use 1 (one) as the index of the first element of an vector or first column / row of a matrix.

  • For a number of (sound) reasons, software engineers use 0 (zero) instead. And all modern mainstream programming languages do likewise.

You've probably encountered a description of the heap data structure / algorithms in some text that was written by someone who prefers the "mathematical" convention to the "software engineering" convention. Then you've coded the algorithm in Java which (like most modern PLs) uses zero-based arrays.

If you find this disturbing, simply modify your code to subtract 1 from the index values.


Note that there are exceptions in the software engineering field:

  • FORTRAN, COBOL, RPG and a few other programming languages - see Wikipedia.
  • Parameter and column numbering in JDBC.
  • Node numbering in the DOM API.

(The sound reasons that I referred to all boil down to the fact that algorithms involving index calculations are generally simpler if the zero is index of the first element. This article explains it.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Or why is it that we are not supposed to use it?

By not using it then your indexing starts at 1 and you can just use the textbook algorithms.

Most algorithm books describe the algorithm starting at 1 instead of 0.

You can use element 0 but then you would have to modify your indexes as @aix answer.

Some people feel that not using 0 is waste of space.
Other feel that not using 0 results in more readable code.

Take your pick

Cratylus
  • 52,998
  • 69
  • 209
  • 339