You are on page 1of 2

01/12/2016

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

Heap.html

/* This program builds heap from an array. */


#include <iostream>
#include <exception>
#inlcude < Bool>
using namespace std;
class HeapArrayLimitExceededException: public exception
{
string _message;
public:
HeapArrayLimitExceededException(string message = "Accessing elements beyond the limit of heap array.\n") : _message(message)
{
}
//Returns the error message.
const string& what()
{
return _message;
}
};
class Heap
{
int *arr;
enum { MAX_ARRAY_SIZE = 20 };
int heapSize;
int arraySize;
//Places the node at its correct position.
void heapify(int i, int heapsize)
{
int largest = -1;
if (2 * i <= heapsize)
{
if (arr[2 * i] > arr[i])
largest = 2 * i;
else
largest = i;
}
if (2 * i + 1 <= heapsize && arr[2 * i + 1] > arr[largest])
largest = 2 * i + 1;
if (largest != i && largest != -1)
{
swap(arr[largest], arr[i]);
heapify(largest, heapsize);
}
}
//Utility function to swap two numbers.
void swap(int &a, int &b)
{
int c = a;
a = b;
b = c;
}
public:
Heap(int n = MAX_ARRAY_SIZE)
{
arr = new int[n + 1];
arraySize = n + 1;
heapSize = 0;
}
void InitializeArray(int length)
{
if (length > arraySize)
{
throw HeapArrayLimitExceededException();
}
heapSize = length;
for (int i = 1; i <= length; i++)
{
cin >> arr[i];
}
BuildHeap();
}
void Heapify(int i)
{
heapify(i, heapSize);
}
void Print()
{
for (int i = 1; i <= heapSize; i++)
cout << arr[i] << " ";
cout << endl;
}
//Creates a heap like structure in O(n) time complexity.
void BuildHeap()
{
for (int i = heapSize / 2; i >= 1; i--)
{
Heapify(i);
}
}
void HeapSort()

le:///mnt/E/My%20Documents/Desktop/Heap.html

1/2

01/12/2016
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

Heap.html

{
int n = heapSize;
for (int i = n; i > 1; i--)
{
swap(arr[1], arr[i]);
heapify(1, i - 1); //Call heapify utility function for n - 1 remaining elements.
}
}
//Add an element to the heap.
void AddElement(int item)
{
//This function adds an element at the end then traverses up while checking for heap conndition.
if (heapSize + 1 == arraySize)
throw HeapArrayLimitExceededException();
arr[++heapSize] = item;
int i = heapSize;
while (i != 1 && arr[i] > arr[i / 2])
{
swap(arr[i], arr[i / 2]);
i /= 2;
}
}
//Returns largest element from the heap but does not removes it.
int LargestElement()
{
return arr[1];
}
//Removes largest element from heap.
int Remove()
{
//Logic: Firstly, swap first and last element then call heapify for first element.
if (heapSize == 0)
throw HeapArrayLimitExceededException();
swap(arr[1], arr[heapSize]);
heapSize--;
Heapify(1);
return arr[heapSize + 1];
}
};
int main()
{
try
{
Heap h(8);
h.AddElement(6);
h.AddElement(10);
h.AddElement(5);
h.AddElement(14);
h.AddElement(16);
h.AddElement(20);
h.AddElement(13);
h.AddElement(2);
h.Print();
cout << h.Remove() << "\n";
h.Print();
}
catch (HeapArrayLimitExceededException e)
{
cout << e.what();
}
system("pause");
return 0;
}

le:///mnt/E/My%20Documents/Desktop/Heap.html

2/2

You might also like