Compare commits
No commits in common. "44f10ff65c91a6b2ef23aec1620433a311b7bb3f" and "0c862db2059f983bf7e7eab937176390e9602eee" have entirely different histories.
44f10ff65c
...
0c862db205
84
heap.cpp
84
heap.cpp
|
@ -1,5 +1,6 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cmath>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,62 +10,40 @@ public:
|
||||||
void push(int);
|
void push(int);
|
||||||
void print() const;
|
void print() const;
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
|
|
||||||
static std::vector<int>& sort(std::vector<int>&);
|
|
||||||
Heap(std::vector<int> v);
|
|
||||||
private:
|
private:
|
||||||
std::vector<int> data;
|
std::vector<int> data;
|
||||||
|
|
||||||
void bubble_up(int index);
|
|
||||||
void bubble_down(int index);
|
|
||||||
void dump(std::ofstream&, int) const;
|
void dump(std::ofstream&, int) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<int>& Heap::sort(std::vector<int>& v){
|
void Heap::push(int h){
|
||||||
Heap h(v);
|
data.push_back(h);
|
||||||
for(auto& i: v) i = h.pop();
|
for(int i = data.size(); i > 1; i /= 2){
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
Heap::Heap(std::vector<int> v): data(std::move(v)){
|
|
||||||
for(int i = data.size()/2; i >= 0; i--){
|
|
||||||
bubble_down(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Heap::bubble_up(int index){
|
|
||||||
for(int i = index; i > 1; i /= 2){//neindexuje od 1, jen nulty prvek nema kam vysplhat
|
|
||||||
if(data.at(i-1) < data.at(i/2-1)) std::swap(data[i-1], data[(i/2)-1]);
|
if(data.at(i-1) < data.at(i/2-1)) std::swap(data[i-1], data[(i/2)-1]);
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Heap::bubble_down(int index){
|
|
||||||
while(true){
|
|
||||||
if(data.size() <= 2*index+1) break;
|
|
||||||
int small_child_i;
|
|
||||||
if(data.size() > 2*index+2 && data.at(2*index+2) < data.at(2*index+1))
|
|
||||||
small_child_i = 2*index+2;
|
|
||||||
else small_child_i = 2*index+1;
|
|
||||||
|
|
||||||
if(data.at(index) > data.at(small_child_i)){
|
|
||||||
std::swap(data[index], data[small_child_i]);
|
|
||||||
index = small_child_i;
|
|
||||||
}else break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Heap::push(int h){
|
|
||||||
data.push_back(h);
|
|
||||||
bubble_up(data.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
int Heap::pop(){
|
int Heap::pop(){
|
||||||
if (empty()) throw std::out_of_range("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
|
if (empty()) throw std::out_of_range("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
|
||||||
std::swap(data[0], data[data.size()-1]);
|
std::swap(data[0], data[data.size()-1]);
|
||||||
int garbage = data.back();
|
int garbage = data.back();
|
||||||
data.pop_back();
|
data.pop_back();
|
||||||
bubble_down(0);
|
|
||||||
|
int muj_index = 0;
|
||||||
|
while(true){
|
||||||
|
if(data.size() <= 2*muj_index+1) break;
|
||||||
|
int small_child_i;
|
||||||
|
if(data.size() > 2*muj_index+2 && data.at(2*muj_index+2) < data.at(2*muj_index+1))
|
||||||
|
small_child_i = 2*muj_index+2;
|
||||||
|
else small_child_i = 2*muj_index+1;
|
||||||
|
|
||||||
|
if(data.at(muj_index) > data.at(small_child_i)){
|
||||||
|
std::swap(data[muj_index], data[small_child_i]);
|
||||||
|
muj_index = small_child_i;
|
||||||
|
}else break;
|
||||||
|
}
|
||||||
|
|
||||||
return garbage;
|
return garbage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,20 +77,17 @@ void Heap::print() const {
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
Heap h;
|
||||||
|
std::vector<int> v = {1, 4, 9, 0, 7, 5, 8};
|
||||||
|
|
||||||
Heap h({9, 7, 6, 5, 4, 3, 2, 1, 0, -3, 8});
|
for (auto i : v) {
|
||||||
std::vector<int> v = {4,6,2,8,0,56,3,4,7,8,2,3,45,5,4,2,432,69,42};
|
h.push(i);
|
||||||
Heap::sort(v);
|
|
||||||
for(auto& i: v){
|
|
||||||
std::cout<<i<<", ";
|
|
||||||
}
|
|
||||||
// for (auto i : v) {
|
|
||||||
// h.push(i);
|
|
||||||
h.print();
|
h.print();
|
||||||
// }
|
}
|
||||||
// while (!h.empty()){
|
|
||||||
// int i = h.pop();
|
while (!h.empty()){
|
||||||
// h.print();
|
int i = h.pop();
|
||||||
// }
|
h.print();
|
||||||
// int aaaaaaaaaaaaaaa = h.pop();
|
}
|
||||||
|
int aaaaaaaaaaaaaaa = h.pop();
|
||||||
}
|
}
|
Loading…
Reference in New Issue