0

I've neglected to work on this code (or any other coding projects) for a while, so while I know what is basically wrong with the code, I've been having a hard time finding exactly where the vector is going out of range. I've been running gdb on it all morning to no avail. I'm trying to make a min-heap out of a vector "theData" in C++.

#include <iostream>
#include <vector>
#include <algorithm>

using std::vector;
using std::cin;
using std::cout;
using std::swap;
using std::pair;
using std::make_pair;

class HeapBuilder {
     private:
        vector<int> data_;
        vector< pair<int, int> > swaps_;

 void WriteResponse() const {
      cout << swaps_.size() << "\n";
for (int i = 0; i < swaps_.size(); ++i) {
  cout << swaps_[i].first << " " << swaps_[i].second << "\n";
       }
}

 void ReadData() {
        int n;
        cin >> n;
        data_.resize(n);
        for(int i = 0; i < n; ++i)
         cin >> data_[i];
}

  void makeMinHeap(vector<int> &theData, int i, int n) {
    int minIndex;
    int left = 2*i;
    int right = 2*i + 1;
      if (left < n && theData.at(left) < theData.at(i)) {
        minIndex = left;
    }
    else if (right < n && theData.at(right) < theData.at(i)) {
        minIndex = right;
    }

if (minIndex != i) {
    swap(theData.at(i), theData.at(minIndex));
    swaps_.push_back(make_pair(i, minIndex));
    makeMinHeap(theData, minIndex, n);
    }
}

  void GenerateSwaps() {
    swaps_.clear();
    int size = data_.size();
    for (int i = (size/2); i >= 0; i--) {
    makeMinHeap(data_, i, size);
     }

  }

 public:
  void Solve() {
     ReadData();
     GenerateSwaps();
     WriteResponse();
  }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  HeapBuilder heap_builder;
  heap_builder.Solve();
  return 0;
}
Anonymous
  • 43
  • 6
  • Shouldn't it be `left < n` instead of `left <= n` and similarly for `right` – sshashank124 May 14 '16 at 03:11
  • I wrote it that way earlier when I was attempting to debug the code and have returned to it after doing some research. Doesn't make a difference in this bug, though. – Anonymous May 14 '16 at 03:55
  • Please try to show enough code for an [MVCE](http://stackoverflow.com/help/mcve). The code from the question cannot be compiled. For example, the variables `swaps_` and `data_` are never declared. It would also help if you provided test data which breaks the function, even if breaks for any data. – Bart van Nierop May 14 '16 at 05:42

1 Answers1

1

You are not putting in a check for minIndex. Look what happens when your left<=n and right <=n both fails, most likely when the whole recursion is about to stop, since you just check

 minIndex != i
// ^-- default each time is garbage which in case last>n && right>n leaves it garbage
// hence when it comes to
if(minIndex!=i){
// It's actually true where it was suppose to break out n thus throws out_of_range
}

Quick n easy solution would be to add a flagcheck

bool flagcheck = false;
if(){ flagcheck = true; }
else if(){ flagcheck = true; }
if(minIndex!=i && flagcheck){}
Abhinav S
  • 111
  • 3
  • 12
  • Thanks, that did the trick. I was so focused with checking the vector access lines of code that I didn't think of that. – Anonymous May 14 '16 at 06:20
  • With every recursion there is that break thing(base case). When'll it break, that's combined with out_of_range kinda hit me. Glad to be of help though – Abhinav S May 14 '16 at 06:34