### Min heap out of order

why is my min heap out of order?

when i input doubles into the heap, and then run extract min, i'm getting this output

 ```41 153 288 292 491 778 1842 1869 2995 3035 3902 4664 4827 5436 5447 5705 6334 6868 7711 8723 8942 9040 9741 9894 9961 11478 11538 11942 12316 12382 12859 14604 14771 15141 15724 17035 17421 17673 18467 19264 18716 19169 19718 19912 21726 22190 22648 23281 23811 24464 25667 20037 26299 26500 25547 16827 26962 27644 19895 28145 27529 28253 28703 29358 30106 30333 31322 32391 32662 32757 ```

as you can see some of them are not in order.
My guess: on line 126 of your code, you've used the wrong variable name.

HTH.
I'll put my code

graph

 ``1234567891011121314151617181920212223`` ``````void addVertex(int integer, int integer2, double a) { vertexList.push_back(new vertex(a)); if(y < integer2) { grid[x][y] = a; y++; } else if(y == 7) { grid[x+1][0] = a; y = 1; x = x+1; } numofVertices++; }``````

heap.h
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111`` ``````class Heap : public weightedGraph { public: Heap(); ~Heap(); void insert(double *element); double * deletemin(); void print(); int size(){return heap.size();} private: int left(int parent); int right(int parent); int parent(int child); void heapifyup(int index); void heapifydown(int index); private: vector heap; }; Heap::Heap() {} Heap::~Heap() {} void Heap::insert(double *element) { heap.push_back(element); heapifyup(heap.size()-1); } void Heap::heapifyup(int index) { while((index>0) && (parent(index) >=0) && (heap[parent(index)][1] > heap[index][1])) { double tmp[2]; tmp[0] = heap[parent(index)][0]; tmp[1] = heap[parent(index)][1]; heap[parent(index)][0] = heap[index][0]; heap[parent(index)][1] = heap[index][1]; heap[index][0] = tmp[0]; heap[index][1] = tmp[1]; index = parent(index); } } void Heap::heapifydown(int index) { int child = left(index); if ( ( child > 0 ) && ( right(index) > 0 ) && ( heap[child][1] > heap[right(index)][1] ) ) { child = right(index); } if ( child > 0 ) { int tmp[2]; tmp[0] = heap[index][0]; tmp[1] = heap[index][1]; heap[index][0] = heap[child][0]; heap[index][1] = heap[child][1]; heap[child][0] = tmp[0]; heap[child][1] = tmp[1]; heapifydown(child); } } double* Heap::deletemin() { double *min = heap.front(); heap[0] = heap.at(heap.size() - 1); heap.pop_back(); heapifydown(0); return min; } void Heap::print() { vector::iterator pos = heap.begin(); cout << "Heap = "; while ( pos != heap.end() ) { cout << **pos << " "<<*(*pos+1) << " -" ; ++pos; } cout << endl; } int Heap::left(int parent) { int i = ( parent <<1) + 1; return(i < heap.size() ) ? i : - 1; } int Heap::right(int parent) { int i = ( parent <<1) + 2; return(i < heap.size() ) ? i : - 1; } int Heap::parent(int child) { if(child > 0) { int i = (child /2); return i; } else return -1; }``````

and then my cpp
 ``123456`` ``````while(i != 70) { dub = rand(); G.addVertex(a,b-2,dub); i++; }``````
help?
Topic archived. No new replies allowed.