157 lines
3.2 KiB
C++
157 lines
3.2 KiB
C++
#include <iostream>
|
|
#include <list>
|
|
#include <algorithm>
|
|
#include <map>
|
|
#include <iomanip>
|
|
#include <vector>
|
|
|
|
using string = std::string;
|
|
using slist = std::list<string>;
|
|
|
|
//Place in header!
|
|
template <typename T>
|
|
void printm(T m){
|
|
std::cout << m;
|
|
}
|
|
|
|
template <typename T>
|
|
void printlm(T m){
|
|
printm(m);
|
|
printm("\n");
|
|
}
|
|
|
|
class Heap {
|
|
protected:
|
|
int pos {0};
|
|
int length {0};
|
|
Heap() {}
|
|
private:
|
|
int size;
|
|
int* storage;
|
|
public:
|
|
Heap(int sz) {
|
|
size = sz;
|
|
storage = new int[sz];
|
|
}
|
|
virtual ~Heap() {
|
|
delete[] storage;
|
|
}
|
|
bool isEmpty() {
|
|
return length == 0;
|
|
}
|
|
virtual void add(int item) {
|
|
if (length + 1 > size) return;
|
|
pos = length;
|
|
while (pos > 0 && storage[(pos-1)/2] < item) {
|
|
storage[pos] = storage[(pos-1)/2];
|
|
pos = (pos-1)/2;
|
|
}
|
|
storage[pos] = item;
|
|
++length;
|
|
}
|
|
virtual int extractMax() {
|
|
int max {storage[0]};
|
|
storage[0] = storage[--length];
|
|
int first {0};
|
|
while ((2*first)+1 <= length - 1) {
|
|
int largest {(2*first)+1};
|
|
if ((2*first)+2 <= length - 1 && storage[largest] < storage[(2*first)+2]) largest = (2*first)+2;
|
|
if (storage[first] >= storage[largest]) break;
|
|
int swap = storage[first];
|
|
storage[first] = storage[largest];
|
|
storage[largest] = swap;
|
|
first = largest;
|
|
}
|
|
return max;
|
|
}
|
|
};
|
|
|
|
class HeapInf : public Heap {
|
|
std::vector<int> storage;
|
|
public:
|
|
HeapInf() : Heap() {}
|
|
virtual ~HeapInf() {}
|
|
void add(int item) {
|
|
if (length+1>storage.size()) storage.push_back(0);
|
|
pos = length;
|
|
while (pos > 0 && storage[(pos-1)/2] < item) {
|
|
storage[pos] = storage[(pos-1)/2];
|
|
pos = (pos-1)/2;
|
|
}
|
|
storage[pos] = item;
|
|
++length;
|
|
}
|
|
int extractMax() {
|
|
int max {(storage.size() > 0) ? storage[0] : 0};
|
|
storage[0] = storage[--length];
|
|
int first {0};
|
|
while ((2*first)+1 <= length - 1) {
|
|
int largest {(2*first)+1};
|
|
if ((2*first)+2 <= length - 1 && storage[largest] < storage[(2*first)+2]) largest = (2*first)+2;
|
|
if (storage[first] >= storage[largest]) break;
|
|
int swap = storage[first];
|
|
storage[first] = storage[largest];
|
|
storage[largest] = swap;
|
|
first = largest;
|
|
}
|
|
storage.pop_back();
|
|
return max;
|
|
}
|
|
};
|
|
|
|
int prompti(string,bool*);
|
|
double promptd(string,bool*);
|
|
string prompts(string,bool*);
|
|
|
|
int main() {
|
|
printlm("Create Heap:");
|
|
HeapInf h{};
|
|
bool s{true};
|
|
while (s) {
|
|
auto x {prompti("Enter integer (Finish by entering invalid): ",&s)};
|
|
if (s) {h.add(x);}
|
|
}
|
|
printlm("Output Heap:");
|
|
while (!h.isEmpty()) printlm(h.extractMax());
|
|
return 0;
|
|
}
|
|
|
|
int prompti(string message, bool *status) {
|
|
int tr {};
|
|
*status = false;
|
|
std::cout << message;
|
|
if (!(std::cin >> tr)){
|
|
std::cin.clear();
|
|
std::cin.ignore();
|
|
return 0;
|
|
}
|
|
*status = true;
|
|
return tr;
|
|
}
|
|
|
|
double promptd(string message, bool *status) {
|
|
double tr {};
|
|
*status = false;
|
|
std::cout << message;
|
|
if (!(std::cin >> tr)){
|
|
std::cin.clear();
|
|
std::cin.ignore();
|
|
return 0.0;
|
|
}
|
|
*status = true;
|
|
return tr;
|
|
}
|
|
|
|
string prompts(string message, bool *status) {
|
|
string tr {""};
|
|
*status = false;
|
|
std::cout << message;
|
|
if (!(std::cin >> tr)){
|
|
std::cin.clear();
|
|
std::cin.ignore();
|
|
return "";
|
|
}
|
|
*status = true;
|
|
return tr;
|
|
}
|