Min-Max Heap implementation, help needed

Hello, I'm currently trying to implement my own Heap template. I have it pretty much done, but there are few errors..
There is one bool variable and if thats set, it should be Max Heap, and if not it should be min Heap. I tried implementing this in my bubble_up function.

Lets start by showing you Heap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#ifndef HEAP_H_INCLUDED
#define HEAP_H_INCLUDED
#include <vector>

template<class T>
class Heap
{
private:
    //declare our private functions and variables
    std::vector<T> _heap_;
    bool maxheap;

    void bubble_up(int);
    void bubble_down(int);


public:
    //declare public functions
    Heap();
    Heap(bool);  //constructor that takes only BOOL parameter
    Heap(std::vector<T>, bool); //constructor that takes vector and BOOL parameter
    ~Heap();  //destructor

    //other functions we'll need
    int right(int);
    int left(int);
    int parent(int);
    void Insert(T);
    bool Delete(T&);
    void build_heap();
    void set_sort(bool);
    void swap(int, int);
    int size();
    void print_heap();
    void clear_heap();
};

template<class T>
int Heap<T>::parent(int child)
{
    if (child != 0)
    {
        int i = (child - 1) >> 1;
        return i;
    }
    return -1;
}

template<class T>
int Heap<T>::left(int parent)
{
    int i = ( parent * 2 ) + 1; // 2 * parent + 1
    return ( i < size() ) ? i : -1;
}

template<class T>
int Heap<T>::right(int parent)
{
    int i = ( parent * 2 ) + 2; // 2 * parent + 2
    return ( i < size() ) ? i : -1;
}

template<class T>
void Heap<T>::bubble_up(int index)
{
    if(!maxheap)
    {
        while ( ( index > 0 ) && ( parent(index) >= 0 ) &&
                ( _heap_[parent(index)] < _heap_[index] ) )
        {
            swap(parent(index), index);
            index = parent(index);
        }
    } else {
        while ( ( index > 0 ) && ( parent(index) >= 0 ) &&
                ( _heap_[parent(index)] > _heap_[index] ) )
        {
            swap(parent(index), index);
            index = parent(index);
        }
    }

}

template<class T>
void Heap<T>::bubble_down(int index)
{
      int leftChildIndex = left(index);
      int rightChildIndex = right(index);
      int minIndex;

      if (rightChildIndex >= size()) {
            if (leftChildIndex >= size())
                  return;
            else
                  minIndex = leftChildIndex;
      } else {
            if (_heap_[leftChildIndex] <= _heap_[rightChildIndex])
                  minIndex = leftChildIndex;
            else
                  minIndex = rightChildIndex;
      }
      if (_heap_[index] > _heap_[minIndex]) {
            swap(index, minIndex);
            bubble_down(minIndex);
      }
}

template<class T>
void Heap<T>::build_heap()  //check value of our bool variable and accordingly do sorting
{
    bubble_up(size()-1);
}

template<class T>
void Heap<T>::set_sort(bool x)  //set our bool variable ('maxheap'), and sort our heap again
{
    maxheap = x;
    build_heap();
}

template<class T>
void Heap<T>::swap(int x, int y)  //swap two elements in vector
{
    T temp = _heap_[x];
    _heap_[x] = _heap_[y];
    _heap_[y] = temp;
}

//definition of constructor with 0 parameters
template<class T>
Heap<T>::Heap()
{
    maxheap = false;
}

//definition of constructor with 1 parameter
template<class T>
Heap<T>::Heap(bool a)
{
    maxheap = a;
}

//definition of constructor with 2 parameters
template<class T>
Heap<T>::Heap(std::vector<T> vec, bool a)
{
    for(unsigned i = 0; i < vec.size(); i++)
        _heap_.push_back(vec[i]);

    maxheap = a;
    build_heap();
}

template<class T>
Heap<T>::~Heap()
{
//blah blah
}

//insert item in heap
template<class T>
void Heap<T>::Insert(T elem)
{
    _heap_.push_back(elem);
    bubble_up(size() - 1);
}

//search for element in parameter, if elements are found delete _heap_[0]
template<class T>
bool Heap<T>::Delete(T& elem)
{
    if(_heap_.empty())
    {
        return false;
    }

    //push _heap_[0] to the last place in vector so we can pop it
    for(int i = 1; i < size(); i++)
        swap(i-1, i);

    elem = _heap_[size() - 1];

    _heap_.pop_back();

    return true;
}

//return size of our heap vector
template<class T>
int Heap<T>::size()
{
    return _heap_.size();
}

template<class T>
void Heap<T>::print_heap()
{
    if(size() == 0)
    {
        std::cout<<"Heap is empty!"<<std::endl;
        return;
    }
    std::cout<<"Printing heap!"<<std::endl;
    for(int i = 0; i < size(); i++)
        std::cout<<_heap_[i]<<" ";

    std::cout<<std::endl<<std::endl;
}

template<class T>
void Heap<T>::clear_heap()
{
    std::cout<<"Clearing heap!"<<std::endl;
    _heap_.clear();
}

#endif // HEAP_H_INCLUDED



Now main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>


#include "Heap.h"

using std::cout;
using std::endl;

typedef int TYPE;

int main(void) {

  Heap<TYPE> *heap = new Heap<TYPE>(); // numeric type heap
  TYPE item = 0;

  Heap<std::string> *s_heap = new Heap<std::string>();  // string heap
  std::string s_item = "y";
  std::vector<std::string> s_blah(11,s_item);

  cout << "*** Test insert elements ***";
  heap->Insert(15);
  heap->Insert(1);
  heap->Insert(3);
  heap->Insert(4);
  heap->print_heap();  //should print 15 4 3 1 and it does

  cout << endl;


  cout << "\n*** Test delete elements ***\n";  //delete also works fine 15, 4, 3, 1 and then returns 0
  cout << item << " " << heap->Delete(item) << endl;
  cout << item << " " << heap->Delete(item) << endl;
  cout << item << " " << heap->Delete(item) << endl;
  cout << item << " " << heap->Delete(item) << endl;
  cout << "the next delete should return false (i.e. 0)\n";
  cout << item << " " << heap->Delete(item) << endl;

  cout << "\n*** Test Heap(vector,bool) ***\n";
  std::vector<TYPE> blah(11, 99);
  blah[1]  = 15;
  blah[2]  = 4;
  blah[3]  = 12;
  blah[4]  = 3;
  blah[5]  = 22;
  blah[6]  = 37;
  blah[7]  = 9;
  blah[8]  = 1;
  blah[9]  = 7;
  blah[10] = 3;

  delete heap;
  heap = new Heap<TYPE>(blah, false);  // false make it a min heap
  heap->print_heap();  
//here it prints wrong> 99 15 4 12 3 22 37 9 1 7 3
//and it should print> 1 3 4 9 3 22 37 15 12 7 99
  cout << endl;

  // Sting heap test
  cout << "\n*** Test insert elements ***";
  s_heap->Insert("15");
  s_heap->Insert("11");
  s_heap->Insert("13");
  s_heap->Insert("43");

  cout << endl;
  s_heap->print_heap();
//works fine, prints out 43, 15, 13, 11

  cout << "\n*** Test delete elements ***\n";
  cout << s_item << " " << s_heap->Delete(s_item) << endl;
  cout << s_item << " " << s_heap->Delete(s_item) << endl;
  cout << s_item << " " << s_heap->Delete(s_item) << endl;
  cout << s_item << " " << s_heap->Delete(s_item) << endl;
  cout << "the next delete should return false (i.e. 0)\n";
  cout << s_item << " " << s_heap->Delete(s_item) << endl;
//deletes fine

  cout << "\n*** Test Heap(vector,bool) ***\n";
  s_blah[1]  = "a";//"15";
  s_blah[2]  = "c";//"4";
  s_blah[3]  = "e";//"12";
  s_blah[4]  = "d";//"3";
  s_blah[5]  = "f";//"22";
  s_blah[6]  = "h";//"37";
  s_blah[7]  = "s";//"9";
  s_blah[8]  = "b";//"1";
  s_blah[9]  = "v";//"7";
  s_blah[10] = "z";//"3";

  delete s_heap;
  s_heap = new Heap<std::string>(s_blah, false);
  s_heap->print_heap();
  cout << endl;
//again, messes up here> it prints:
//z y c e a f h s b v d

//it should print> 
a b c e d f h s y v z

  cout << "*** Emptying heap ***\n";
  unsigned int size = s_heap->size();
  for(unsigned int i=0; i<size; i++) {
  	s_heap->Delete(s_item);
  	cout << s_item << " ";
  }
  cout << "\n*********************\n";
  cout << endl;

} // end to main 
Anyone?
Topic archived. No new replies allowed.