Infinate Heap.
This commit is contained in:
parent
1da7074bfc
commit
b736074066
106
main.cpp
106
main.cpp
@ -3,6 +3,7 @@
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
|
||||
using string = std::string;
|
||||
using slist = std::list<string>;
|
||||
@ -19,28 +20,99 @@ void printlm(T 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*);
|
||||
int prompti(string,bool*);
|
||||
double promptd(string,bool*);
|
||||
string prompts(string,bool*);
|
||||
|
||||
int main() {
|
||||
printlm("Parse comma deliminated list:");
|
||||
bool s {};
|
||||
slist lst {};
|
||||
auto v = prompts("Enter the string: ", &s);
|
||||
if (!s) {
|
||||
printlm("Error, No Data!");
|
||||
return -1;
|
||||
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);}
|
||||
}
|
||||
auto comma = find(v.cbegin(),v.cend(),',');
|
||||
lst.push_back(string(v.cbegin(),comma));
|
||||
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);
|
||||
printlm("Output Heap:");
|
||||
while (!h.isEmpty()) printlm(h.extractMax());
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user