about heap up

Nov 15, 2011 at 2:17am
Hi guys,
I am stuck in this problem, I don't know how to build the heap I did most of the functions. take a look on my code you may can give me hints that would help me
I have two hours for the submission. any hints it would help me for any function you may see errors in it
I have problem I don't how to start with build heap function pleas

Appreciate your help in advance
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
221
222
223
224


#include <iostream>
#include <sstream>
using namespace std;

class Heap{

public:

	//Build empty heap
	Heap();
	
	//Builds a heap from list
	Heap(int list[], int listSize);
	
	//not used for this lab
	void insert(int x);
	
	//removes the smallest element in the heap
	int remove();
	
	//returns the size of the heap;
	int size();

private:
	
	//heap is implemented as an array of 25 ints 
	//better implementation would be a dynamic array
	int heap[25];
	
	//number of elements currently in the heap
	int heapSize;
	
	//down heap function
	void heapify(int location);
	
	//up heap function for insert
	void upheaval(int location); 
	
	//build heap function used by constructor that takes in array
	void buildHeap();
	
	int leftChild(int index);
	int rightChild(int index);
	int parent(int index);
	
};


Heap::Heap()
{
	heapSize = 0;
}

Heap::Heap(int list[], int listSize)
{

	//remember heaps start at 1 not zero
	heap[0]=-1;

	for(int i=0; i < listSize; i++)
	{
		heap[i+1] = list[i];
	}
	
	heapSize = listSize;

	buildHeap();

}
void Heap::insert(int x)
{
	//location to insert is heapSize+1 
	//(Recall, 0th location not used)
	heap[heapSize+1] = x;
	heapSize++;
	//heapsize points to index of last element now
	//reheap
	upheaval(heapSize);

}

int Heap::remove()
{
	int par, rightChild, leftChild; 
	int	index = 0;
	par = (index - 1)/2;
	leftChild = 2 * index;
	rightChild = 2 * index + 1;
	if(!heap)
		cout << "Heap is Empty";
	else
	{
		heap[0] = heap [heapSize - 1];
		heapSize--;
		while (heapSize > 0)
		{
			if( par < leftChild && par > rightChild)
				swap (par, leftChild);
			else

				swap (par, rightChild);
		}
			
	}
	return heapSize;
}

int Heap::size()
{
	return heapSize;
}



void Heap::heapify(int location)
{
	int rightChild, leftChild, smallest;
  leftChild = 2 * location + 1;
	rightChild = 2 * location +2;
	 
	 if(leftChild <= location)
	 {
      if(leftChild == location)
        
        smallest = leftChild;
      else
        {
            if(heap[leftChild] < heap [rightChild])
              
              smallest = rightChild;
              else
                smallest = leftChild;
        }
        if(heap[smallest] > heap[location])
        {
            swap(heap[smallest], heap[location]);
            heapify(smallest);
        }
    }
	
    

//YOUR CODE HERE
	
}

void Heap::upheaval(int location)
{
	int par, r, l;
	par = (location - 1)/2;
	l = 2 * location;
	r = 2 * location + 1;
	if (r != location)
	{
		if (heap[location] > heap[par])
			swap(heap[location], heap[par]);
			upheaval (location);
	}
	
	//coming soon as it gives away the answer to heapify
}	

void Heap::buildHeap()
{

	
}

int Heap::leftChild(int index)
{
	return index*2;
}
int Heap::rightChild(int index)
{
	return index*2+1;

}
int Heap::parent(int index)
{
	return index/2;
}
	
//not part of the heap class. but uses a heap
void heapSort(int list[], int size)
{

	int* temp;
	temp = new int[size];
	
	Heap h(list,size);
	for(int i=0; i < size; i++)
	{
		temp[i] = h.remove();
	}

	for(int j=0; j < size; j++)
	{
		list[j] = temp[j];
	}

}



int main()
{
	int testList[] = {34, 5, 23, 12, 33, 98, 4, 13, 44, 37, 1, 86, 8}; 
	int size = 13;
	
	 
	heapSort(testList, size);
	cout << "Lists should match " << endl;
	cout << "1, 4, 5, 8, 12, 13, 23, 33, 34, 37, 44, 86, 98" << endl;
	
	for(int i=0; i < size; i++)
	{
		cout << testList[i] << ", ";
	}
	cout << endl;
	
}
Last edited on Nov 15, 2011 at 3:12am
Nov 15, 2011 at 3:13am
please guys need little help

:-( after solving :-)
Nov 15, 2011 at 3:36am
:D :O :p
hey guys I am struggling with this functions


1
2
3
4
5
6
void Heap::buildHeap()
{

	
}
Topic archived. No new replies allowed.