The Heap

Why is it that we do an increase with totalNR when we do addition of new elements into the heap while we do not do so while going through the elements of this same heap?
My hope is that I have made myself clear.

  void insert(const T value) {          //  value in heap
          if (totalNr < long - 1) {     //  the place:    
              data[++totalNr] = value; //  put it in the bottom. (data here pointer)
              upHeap(totalNr);         //  upHeape
          }  else                       //  no place in heap:
              cout << "\nHeap is not empty " << long
                   << " elements (includea. sen key)!\n\n";
Last edited on
"data" presumably has "long" possible elements. To add NEW data, you want to add it into data where there isn't already data. totalNR keeps track of the number of elements put inside of data, so you increment it to go to the next empty space in data where you can put your new information.

If you're going through the elements of the array, you are NOT inserting new data - and totalNr's job is to keep track of how many elements you've put into the array so far. To go through the array, you want a different variable - and it'll go from 0 to the value of totalNr.
Topic archived. No new replies allowed.