Java – why not use the element zero of the heap array?
This is the beginning of my rough sketch 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 are elements in array [0] always set to null? Or why shouldn't we use it?
Solution
There are several ways to represent a binary heap as an array
There is a way to use the element zero; Another way is not to use element zeros:
>Root is element 0; The child elements of element n are elements 2n 1 and 2n 2; > Root is element 1; The child elements of element n are elements 2n and 2n 1
Neither is more "right" than the others The former is more suitable to use the programming language of zero based arrays, while the latter is more suitable to use the language of one based arrays
It seems that you have encountered an implementation using the second method Due to the language in question, Java uses zero - based arrays, and the element zero exists but is not used