1
0

Infinate Heap.

This commit is contained in:
Captain ALM 2022-12-09 09:59:07 +00:00
parent 1da7074bfc
commit b736074066
3 changed files with 89 additions and 17 deletions

106
main.cpp
View File

@ -3,6 +3,7 @@
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <iomanip> #include <iomanip>
#include <vector>
using string = std::string; using string = std::string;
using slist = std::list<string>; using slist = std::list<string>;
@ -19,28 +20,99 @@ void printlm(T m){
printm("\n"); 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*); int prompti(string,bool*);
int prompti(string,bool*); double promptd(string,bool*);
string prompts(string,bool*); string prompts(string,bool*);
int main() { int main() {
printlm("Parse comma deliminated list:"); printlm("Create Heap:");
bool s {}; HeapInf h{};
slist lst {}; bool s{true};
auto v = prompts("Enter the string: ", &s); while (s) {
if (!s) { auto x {prompti("Enter integer (Finish by entering invalid): ",&s)};
printlm("Error, No Data!"); if (s) {h.add(x);}
return -1;
} }
auto comma = find(v.cbegin(),v.cend(),','); printlm("Output Heap:");
lst.push_back(string(v.cbegin(),comma)); while (!h.isEmpty()) printlm(h.extractMax());
do {
auto start = ++comma;
comma = find(start,v.cend(),',');
lst.push_back(string(start, comma));
} while(comma != v.cend());
printlm("All entries:");
for (const auto c : lst) printlm(c);
return 0; return 0;
} }

BIN
main.o

Binary file not shown.

BIN
t

Binary file not shown.