Heaps

Dec 8, 2008 at 1:05am
I am studying for my final tomorrow so i redid my heap assignment. When i run this just sits there waiting for some input. I think the problem is in my upHeap function but im not sure. Any ideas what im doing wrong!

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
#include "header.h"

#define RANGE 100;

template<class T, class U>
void downHeap(vector<T>& vec, int first, U sort)
{
	int size = vec.size();
	int child = 2 * first;
	while(child < size) 
	{
		if(child < size -1 && sort(vec[child], vec[child + 1]))
			child++;
		if(vec[first] < vec[child])
		{
			swap(vec[first], vec[child]);
			first = child;
			child = 2 * child;
		}
		else
			child = size;
	}
}
template<class T, class U>
T remove(vector<T>& vec, U sort)
{
	T object = vec[1];
	vec[1] = vec.back();
	vec.pop_back();
	downHeap(vec, 1, sort);
	
	return object;
}
template<class T, class U>
void print(vector<T>& vec, U sort)
{
	T object;
	for(int j = 1; j < vec.size(); j++)
	{ 
		object = remove(vec, sort);
		cout << object << " ";
	}
}	
template<class T, class U> 
void upHeap(vector<T>& vec, int index, U sort)
{
	
	while(index > 1 && sort(vec[index], vec[index / 2]))
	{
		swap(vec[index], vec[index / 2]);
		index /= 2;
	}		
}

template<class T, class U>
void insert(vector<T>& vec, const T& num, U sort)
{
	int size = vec.size();
	vec.push_back(num);
	upHeap(vec, size, sort);
}

int main()
{
	int s1 = 10;
	int size = 10;
	vector<int> v1(size);
	int number;


	srand(s1);
	for(int i = 0; i < v1.size(); i++)
	{
		v1[i] = rand() % RANGE + 1;
		number = v1[i];
		insert(v1, number, less<int>());
	}
		
	cout << endl;

	print(v1, less<int>());
	cout << endl;
	print(v1, greater<int>());
	cout << endl;
 
	return 0;
}
Dec 8, 2008 at 1:34am
If you are using std::sort(), I'm pretty sure you are right because
sort() expects to be able to increment its first parameter (iterator) until it
reaches the second parameter (iterator), and you are passing in the
iterators in the opposite order.

You probably also want to pass pointers to sort:

 
sort( &vec[index/2], &vec[index] );


Also, your #define of RANGE is wrong because it should not have a semicolon on it. Consider what the does to the line

 
v1[i] = rand() % RANGE + 1;


when the text "100;" is substituted for RANGE.

Dec 8, 2008 at 2:18am
Its not std::sort(), sort just happened to be what i called it cause im passing the less<int>() and greater<int>() functions in main
Dec 8, 2008 at 2:23pm
Sorry, this isn't much help for your specific problem, but:

It appears from your code that you are expecting vectors to be indexed 1-based, but they are 0-based (At least print() seems to make this assumption).

From a design standpoint, it is very strange to have a function called "print()" modify the container it is printing. I'd expect print() to take the vector by const reference and not modify it, if I were a programmer using your code.

Dec 8, 2008 at 2:37pm
The program is in an infinite loop because size is growing by one each time you call insert() at the bottom of the loop.

In main(), i is always vl.size() + 10.

You seem to be mixing ideas, filling a fixed size vector, then appending to it too. Are you sure you want to do both?
Topic archived. No new replies allowed.