B-Tree implementation on C++ has a memory leak? - c++

I have this B-Tree implementation, which I'm testing using search() and insert(). Testing is basically this:
void function(){
BTree b16(16);
// do lots of inserts and searchs on b16
}
for(many_times){
function();
}
If I understand correctly, after each iteration of function(), b16 should get destroyed. However, after ~250 iterations, I get a bad_alloc error, which means I have a memory leak.
Is there a problem with the destructors? Here's the implementation:
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned int uint;
class BNode{
private:
uint *keys;
int B;
BNode **C;
int n;
bool leaf;
public:
BNode(int temp, bool bool_leaf);
~BNode();
void insertNonFull(uint k);
void splitChild(int i, BNode *y);
void traverse();
BNode *search(uint k);
friend class BTree;
};
class BTree{
private:
BNode *root;
int B;
public:
BTree(int temp);
~BTree();
BNode *search(uint k);
int search_bool(uint k);
void insert(uint k);
};
BNode::BNode(int t1, bool leaf1) {
B = t1;
leaf = leaf1;
keys = new uint[2*B - 1];
C = new BNode *[2*B];
n = 0;
}
BNode::~BNode(){
delete[] keys;
delete[] C;
}
BNode *BNode::search(uint k){
int i = 0;
while (i < n && k > keys[i]){
i++;
}
if(keys[i] == k){
return this;
}
if (leaf == true){
return NULL;
}
return C[i]->search(k);
}
void BTree::insert(uint k){
if (root == NULL) {
root = new BNode(B, true);
root->keys[0] = k;
root->n = 1;
}
else{
if (root->n == 2*B - 1){
BNode *s = new BNode(B, false);
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k){
i++;
}
s->C[i]->insertNonFull(k);
root = s;
}
else{
root->insertNonFull(k);
}
}
}
void BNode::insertNonFull(uint k) {
int i = n - 1;
if (leaf == true) {
while (i>=0 && keys[i] > k) {
keys[i+1] = keys[i];
i--;
}
keys[i+1] = k;
n = n + 1;
}
else {
while (i>=0 && keys[i] > k){
i--;
}
if (C[i+1]->n == 2*B-1) {
splitChild(i+1, C[i+1]);
if (keys[i + 1] < k){
i++;
}
}
C[i+1]->insertNonFull(k);
}
}
void BNode::splitChild(int i, BNode *y) {
BNode *z = new BNode(y->B, y->leaf);
z->n = B - 1;
for (int j = 0; j < B - 1; j++){
z->keys[j] = y->keys[j+B];
}
if (!y->leaf){
for (int j = 0; j < B; j++)
z->C[j] = y->C[j + B];
}
y->n = B - 1;
for(int j=n; j >= i+1; j--){
C[j+1] = C[j];
}
C[i + 1] = z;
for (int j = n - 1; j >= i; j--){
keys[j+1] = keys[j];
}
keys[i] = y->keys[B - 1];
n = n + 1;
}
BTree::BTree(int temp){
root = NULL;
B = temp;
}
BTree::~BTree(){
delete root;
}
BNode *BTree::search(uint k){
return (root == NULL) ? NULL : root->search(k);
}
int BTree::search_bool(uint k){
if(search(k) != NULL){
return true;
}
else{
return false;
}
}
Thanks in advance.

So, the simple diagnosis is that
delete[] C;
deletes only the array, not the nodes contained by it. So, (a) make sure they're properlu zero-initialized (b) delete them as well:
BNode** C = new BNode* [2 * B] { 0 };
// in the destructor:
for (int i = 0; i < 2 * B; ++i)
delete C[i];
delete[] C;
HOWEVER.
This doesn't work well because nodes can be split. After you moved some nodes from one node's C to another node's C, you run into double-free. So, when you split nodes, you have to make sure you set the moved-from C elemeent to NULL again:
z->C[j] = y->C[j + B];
y->C[j + B] = nullptr;
Now, this program runs clean under valgrind, ubsan and asan and without leaks:
Live On Coliru
#include <iostream>
#include <vector>
using uint = unsigned;
class BNode {
private:
int B;
bool leaf;
uint* keys = new uint[2 * B - 1]{0};
BNode** C = new BNode* [2 * B] { 0 };
int n = 0;
public:
BNode(int t1, bool leaf1) : B(t1), leaf(leaf1) {}
~BNode()
{
delete[] keys;
for (int i = 0; i < 2 * B; ++i)
delete C[i];
delete[] C;
}
BNode const* search(uint k) const
{
int i = 0;
while (i < n && k > keys[i]) {
i++;
}
if (keys[i] == k) {
return this;
}
return leaf ? nullptr : C[i]->search(k);
}
void insertNonFull(uint k)
{
int i = n - 1;
if (leaf == true) {
while (i >= 0 && keys[i] > k) {
keys[i + 1] = keys[i];
i--;
}
keys[i + 1] = k;
n = n + 1;
} else {
while (i >= 0 && keys[i] > k) {
i--;
}
if (C[i + 1]->n == 2 * B - 1) {
splitChild(i + 1, C[i + 1]);
if (keys[i + 1] < k) {
i++;
}
}
C[i + 1]->insertNonFull(k);
}
}
void splitChild(int i, BNode* y)
{
BNode* z = new BNode(y->B, y->leaf);
z->n = B - 1;
for (int j = 0; j < B - 1; j++) {
z->keys[j] = y->keys[j + B];
}
if (!y->leaf) {
for (int j = 0; j < B; j++) {
z->C[j] = y->C[j + B];
y->C[j + B] = nullptr;
}
}
y->n = B - 1;
for (int j = n; j >= i + 1; j--) {
C[j + 1] = C[j];
}
C[i + 1] = z;
for (int j = n - 1; j >= i; j--) {
keys[j + 1] = keys[j];
}
keys[i] = y->keys[B - 1];
n = n + 1;
}
friend class BTree;
};
class BTree {
private:
BNode* root = nullptr;
int B;
public:
BTree(int temp)
{
root = nullptr;
B = temp;
}
~BTree() { delete root; }
BNode const* search(uint k) const
{
return (root == nullptr) ? nullptr : root->search(k);
}
bool search_bool(uint k) const { return search(k) != nullptr; }
void insert(uint k)
{
if (!root) {
root = new BNode(B, true);
root->keys[0] = k;
root->n = 1;
} else {
if (root->n == 2 * B - 1) {
BNode* s = new BNode(B, false);
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k) {
i++;
}
s->C[i]->insertNonFull(k);
root = s;
} else {
root->insertNonFull(k);
}
}
}
};
int main()
{
for (int b = 8; b < 17; ++b) {
BTree tree(b);
for (int i = 0; i < 100'000; ++i)
tree.insert(rand() % 1000);
}
}
In Closing
Kudos for getting the algorithmics largely right here. That's not easy.
As you can see in my cleanup/review I hardened a lot of stuff, mainly around initialization. This is an important habit. I can't actually rule out that not having that would have exposed other sleeping bugs.
Also note the increased const-correctness.
Also, like other said, prefer smart pointers and modern C++ techniques. It will be amazing how much less error prone just using std::array, unique_ptr and so will be. For example, the bug with moving nodes in splitChild would never have compiled because you have to explcitly move-assign unique_ptr
Bonus
Example in more modern C++:
without any new/delete
without any raw pointer
no more manual destructors (even no constructors required, really)
compiler checked move semantics, NICE!
statically known B factor, so everything becomes 10x more performant, while still being tunable
generic key (element) type, so you can now store std::string, double, whatnot
generic comparator, so you sort your keys in alternative orders (e.g. Btree<std::string, 16, std::greater<> > to store the keys in descending order instead of ascending).
No leaks!
Live On Coliru
#include <array>
#include <algorithm>
#include <memory>
#include <iostream>
template <typename T, unsigned B = 16, typename Cmp = std::less<T>>
class BTree {
private:
static constexpr unsigned MaxKeys = 2 * B - 1;
static constexpr unsigned MaxChildren = 2 * B;
struct BNode;
using NodePtr = std::unique_ptr<BNode>;
struct BNode {
bool _leaf;
int _n = 0;
std::array<T, MaxKeys> _keys{};
std::array<NodePtr, MaxChildren> _children{};
BNode(bool leaf1) : _leaf(leaf1) {}
BNode const* search(T k, Cmp cmp) const
{
int i = 0;
while (i < _n && cmp(_keys[i], k)) {
i++;
}
if (_keys[i] == k) {
return this;
}
return _leaf ? nullptr : _children[i]->search(k, cmp);
}
void insertNonFull(T k, Cmp cmp)
{
int i = _n - 1;
if (_leaf == true) {
while (i >= 0 && cmp(k, _keys[i])) {
_keys[i + 1] = _keys[i];
i--;
}
_keys[i + 1] = k;
_n = _n + 1;
} else {
while (i >= 0 && cmp(k, _keys[i])) {
i--;
}
if (_children[i + 1]->_n == MaxKeys) {
splitChild(i + 1, *_children[i + 1]);
if (cmp(_keys[i + 1], k)) {
i++;
}
}
_children[i + 1]->insertNonFull(k, cmp);
}
}
void splitChild(int i, BNode& y)
{
NodePtr z = std::make_unique<BNode>(y._leaf);
z->_n = B - 1;
for (unsigned j = 0; j < B - 1; j++) {
z->_keys[j] = y._keys[j + B];
}
if (!y._leaf) {
for (unsigned j = 0; j < B; j++) {
z->_children[j] = std::move(y._children[j + B]);
}
}
y._n = B - 1;
for (int j = _n; j >= i + 1; j--) {
_children[j + 1] = std::move(_children[j]);
}
_children[i + 1] = std::move(z);
for (int j = _n - 1; j >= i; j--) {
_keys[j + 1] = _keys[j];
}
_keys[i] = y._keys[B - 1];
_n = _n + 1;
}
};
NodePtr root = nullptr;
Cmp _cmp{};
public:
BTree(Cmp cmp = {}) : _cmp(std::move(cmp)) {}
BNode const* search(T k) const {
return root ? root->search(k, _cmp) : nullptr;
}
bool search_bool(T k) const { return search(k) != nullptr; }
void insert(T k)
{
if (!root) {
root = std::make_unique<BNode>(true);
root->_keys[0] = k;
root->_n = 1;
} else {
if (root->_n == MaxKeys) {
NodePtr s = std::make_unique<BNode>(false);
s->splitChild(0, *root);
s->_children[0] = std::move(root);
int i = 0;
if (_cmp(s->_keys[0], k)) {
i++;
}
s->_children[i]->insertNonFull(k, _cmp);
root = std::move(s);
} else {
root->insertNonFull(k, _cmp);
}
}
}
};
int main()
{
using Asc = std::less<>;
using Desc = std::greater<>;
BTree<double, 8, Asc> b8;
BTree<int, 9, Desc> b9;
BTree<unsigned, 10, Asc> b10;
BTree<size_t, 11, Desc> b11;
BTree<double, 12, Asc> b12;
BTree<int, 13, Desc> b13;
BTree<unsigned, 14, Asc> b14;
BTree<size_t, 15, Desc> b15;
BTree<int, 16> b16; // default is ascending
for (int i = 0; i < 100'000; ++i) {
b8.insert(rand() % 10000);
b9.insert(rand() % 10000);
b10.insert(rand() % 10000);
b11.insert(rand() % 10000);
b12.insert(rand() % 10000);
b13.insert(rand() % 10000);
b14.insert(rand() % 10000);
b15.insert(rand() % 10000);
b16.insert(rand() % 10000);
}
{
struct NC {
int v;
bool operator==(NC const& o) const { return v == o.v; }
};
struct CMP {
bool operator()(NC const& a, NC const& b) const { return a.v < b.v; }
};
BTree<NC, 8, CMP> b8;
b8.insert({42});
bool ok = b8.search_bool({42});
}
}

Related

I tried to implement the RSA encryption algorithm using cpp, but it takes too long and I don't know if it will work

My code is here
//RSA.cpp
#include <iostream>
#include <cstring>
#include <ctime>
#include <random>
#include <vector>
#include <string>
#include "BigNum.hpp"
using namespace std;
BigNum mygcd(BigNum a, BigNum b)
{
while(a != b)
{
if(a>b)
{
a = a - b;
}
else
{
b = b - a;
}
}
return a;
}
BigNum prime(int n)
{
vector<BigNum> ans;
ans.push_back(BigNum(2));
ans.push_back(BigNum(3));
for (int i = 0; i < n; i++)
{
BigNum addend = 1;
for (auto j : ans)
{
addend = addend * j;
}
ans.push_back(addend + 1);
}
return ans[ans.size() - 1];
}
BigNum exgcd(BigNum a, BigNum b, BigNum &x, BigNum &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
BigNum g = exgcd(b, a - (a / b) * b, x, y);
BigNum t;
t = x;
x = y;
y = t - a / b * y;
return g;
}
BigNum niyuan(BigNum a, BigNum b)
{
BigNum x, y;
BigNum aa = exgcd(a, b, x, y);
return (x + b) - ((x + b) / b) * b;
}
vector<BigNum> yinshu(BigNum n)
{
vector<BigNum> ans;
int a = 2;
while (n / 2 > a)
{
if (n % a == 0)
{
ans.push_back(a);
}
a++;
}
return ans;
}
vector<int> ToBit(BigNum obj){
vector<int> r;
while (obj != 0){
r.push_back( (obj - (obj / 2) * 2 == 0) ? 0 : 1 );
obj = obj / 2;
}
return r;
}
BigNum jiami(BigNum e, int i, BigNum n)
{
BigNum addend = i;
BigNum result = 1;
vector<int>bitE = ToBit(e);
int now = 0;
while (now != bitE.size())
{
if (bitE[now])
{
result = addend * result;
result = result - (result / n) * n;
}
addend = addend * addend;
now = now + 1;
}
return result;
}
BigNum jiemi(BigNum d, BigNum i, BigNum n)
{
BigNum addend = i;
BigNum result = 1;
vector<int>bitD = ToBit(d);
int now = 0;
while (now != bitD.size())
{
if (bitD[now])
{
result = addend * result;
result = result - (result / n) * n;
}
addend = addend * addend;
now = now + 1;
}
return result;
}
int main()
{
srand(time(0));
BigNum p = prime(rand() % 20 + 1);
srand(time(0));
BigNum q = prime(rand() % 20 + 1);
BigNum N = p * q;
BigNum r = (p - 1) * (q - 1);
sss:
srand(time(0));
BigNum e = random() + 2;
if (mygcd(e, r) - BigNum(1) > 0)
goto sss;
vector<BigNum> yinshus = yinshu(r);
BigNum d = BigNum(niyuan(e, r));
cout << "Alice send(" << N << ',' << e << ")to Bob" << endl;
cout << "Please input your massage:";
string m;
cin >> m;
vector<int> message;
for (auto i : m)
{
message.push_back((int)i);
}
vector<BigNum> miwen;
for (auto i : message)
{
miwen.push_back(jiami(e, i, N));
}
cout << "coded text:";
for (auto i : miwen)
{
cout << i << " ";
}
vector<BigNum> minwen;
for (auto i : miwen)
{
minwen.push_back(jiemi(d, i, N));
}
cout << "明文:";
for (auto i : minwen)
{
cout << i << " ";
}
cout << endl;
}
I used a self-defined data structure called BigNum in order to store some large integers without them overflowing.
//BigNum.hpp
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
class BigNum
{
private:
int a[999];
int len;
public:
BigNum()
{
len = 1;
memset(a, 0, sizeof(a));
}
BigNum(const int);
BigNum(const char *);
BigNum(const BigNum &);
BigNum &operator=(const BigNum &);
friend istream &operator>>(istream &, BigNum &);
friend ostream &operator<<(ostream &, BigNum &);
BigNum operator+(const BigNum &) const;
BigNum operator-(const BigNum &) const;
BigNum operator*(const BigNum &) const;
BigNum operator/(const int &) const;
BigNum operator/(const BigNum &) const;
BigNum operator^(const int &) const;
int operator%(const int &) const;
bool operator>(const BigNum &T) const;
bool operator>(const int &t) const;
bool operator<(const BigNum &) const;
bool operator<=(const BigNum &) const;
bool operator>=(const BigNum &) const;
bool operator==(const BigNum &) const;
bool operator!=(const BigNum &) const;
void print();
};
BigNum::BigNum(const int b)
{
int c, d = b;
len = 0;
memset(a, 0, sizeof(a));
while (d > MAXN)
{
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const char *s)
{
int t, k, index, l, i;
memset(a, 0, sizeof(a));
l = strlen(s);
len = l / DLEN;
if (l % DLEN)
len++;
index = 0;
for (i = l - 1; i >= 0; i -= DLEN)
{
t = 0;
k = i - DLEN + 1;
if (k < 0)
k = 0;
for (int j = k; j <= i; j++)
t = t * 10 + s[j] - '0';
a[index++] = t;
}
}
BigNum::BigNum(const BigNum &T) : len(T.len)
{
int i;
memset(a, 0, sizeof(a));
for (i = 0; i < len; i++)
a[i] = T.a[i];
}
BigNum &BigNum::operator=(const BigNum &n)
{
int i;
len = n.len;
memset(a, 0, sizeof(a));
for (i = 0; i < len; i++)
a[i] = n.a[i];
return *this;
}
istream &operator>>(istream &in, BigNum &b)
{
char ch[MAXSIZE * 4];
int i = -1;
in >> ch;
int l = strlen(ch);
int count = 0, sum = 0;
for (i = l - 1; i >= 0;)
{
sum = 0;
int t = 1;
for (int j = 0; j < 4 && i >= 0; j++, i--, t *= 10)
{
sum += (ch[i] - '0') * t;
}
b.a[count] = sum;
count++;
}
b.len = count++;
return in;
}
ostream &operator<<(ostream &out, BigNum &b)
{
int i;
cout << b.a[b.len - 1];
for (i = b.len - 2; i >= 0; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << b.a[i];
}
return out;
}
BigNum BigNum::operator+(const BigNum &T) const
{
BigNum t(*this);
int i, big;
big = T.len > len ? T.len : len;
for (i = 0; i < big; i++)
{
t.a[i] += T.a[i];
if (t.a[i] > MAXN)
{
t.a[i + 1]++;
t.a[i] -= MAXN + 1;
}
}
if (t.a[big] != 0)
t.len = big + 1;
else
t.len = big;
return t;
}
BigNum BigNum::operator-(const BigNum &T) const
{
int i, j, big;
bool flag;
BigNum t1, t2;
if (*this > T)
{
t1 = *this;
t2 = T;
flag = 0;
}
else
{
t1 = T;
t2 = *this;
flag = 1;
}
big = t1.len;
for (i = 0; i < big; i++)
{
if (t1.a[i] < t2.a[i])
{
j = i + 1;
while (t1.a[j] == 0)
j++;
t1.a[j--]--;
while (j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + 1 - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while (t1.a[t1.len - 1] == 0 && t1.len > 1)
{
t1.len--;
big--;
}
if (flag)
t1.a[big - 1] = 0 - t1.a[big - 1];
return t1;
}
BigNum BigNum::operator*(const BigNum &T) const
{
BigNum ret;
int i, j, up;
int temp, temp1;
for (i = 0; i < len; i++)
{
up = 0;
for (j = 0; j < T.len; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if (temp > MAXN)
{
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else
{
up = 0;
ret.a[i + j] = temp;
}
}
if (up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while (ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
BigNum BigNum::operator/(const int &b) const
{
BigNum ret;
int i, down = 0;
for (i = len - 1; i >= 0; i--)
{
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
}
ret.len = len;
while (ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
int BigNum::operator%(const int &b) const
{
int i, d = 0;
for (i = len - 1; i >= 0; i--)
{
d = ((d * (MAXN + 1)) % b + a[i]) % b;
}
return d;
}
BigNum BigNum::operator^(const int &n) const
{
BigNum t, ret(1);
int i;
if (n < 0)
exit(-1);
if (n == 0)
return 1;
if (n == 1)
return *this;
int m = n;
while (m > 1)
{
t = *this;
for (i = 1; i << 1 <= m; i <<= 1)
{
t = t * t;
}
m -= i;
ret = ret * t;
if (m == 1)
ret = ret * (*this);
}
return ret;
}
bool BigNum::operator>(const BigNum &T) const
{
int ln;
if (len > T.len)
return true;
else if (len == T.len)
{
ln = len - 1;
while (a[ln] == T.a[ln] && ln >= 0)
ln--;
if (ln >= 0 && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigNum::operator>(const int &t) const
{
BigNum b(t);
return *this > b;
}
void BigNum::print()
{
int i;
cout << a[len - 1];
for (i = len - 2; i >= 0; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << a[i];
}
cout << endl;
}
bool BigNum::operator<(const BigNum &obj) const
{
for (int i = 0; i < len; i++)
{
if (a[i] < obj.a[i])
return true;
if (a[i] > obj.a[i])
return false;
}
return false;
}
bool BigNum::operator<=(const BigNum &obj) const
{
for (int i = 0; i < len; i++)
{
if (a[i] < obj.a[i])
return true;
if (a[i] > obj.a[i])
return false;
}
return true;
}
bool BigNum::operator>=(const BigNum &obj) const
{
for (int i = 0; i < len; i++)
{
if (a[i] > obj.a[i])
return true;
if (a[i] < obj.a[i])
return false;
}
return true;
}
bool BigNum::operator==(const BigNum &obj) const
{
for (int i = 0; i < len; i++)
{
if (a[i] != obj.a[i])
return false;
}
return true;
}
bool BigNum::operator!=(const BigNum &obj) const
{
for (int i = 0; i < len; i++)
{
if (a[i] != obj.a[i])
return true;
}
return false;
}
BigNum BigNum::operator/(const BigNum &op2) const
{
BigNum temp(*this);
if (op2 == 0)
{
cout << "ERROR!!";
for (int i = 0; i < len; i++)
temp.a[i] = 0;
}
else if (*this < op2)
{
for (int i = 0; i < len; i++)
temp.a[i] = 0;
}
else if (*this == op2)
{
temp.a[len - 1] = 1;
}
else if (op2 == 1)
{
temp = *this;
}
else if (op2 == 2)
{
int from = 0;
for (int i = 0; i < len; i++)
{
if (temp.a[i] != 0)
{
from = i;
break;
}
}
int carry = 0;
for (int i = from; i < len; i++)
{
if (temp.a[i] & 1)
{
if (carry == 1)
temp.a[i] = (temp.a[i] + 10) / 2;
else
temp.a[i] = temp.a[i] / 2;
carry = 1;
}
else
{
if (carry == 1)
temp.a[i] = (temp.a[i] + 10) / 2;
else
temp.a[i] = temp.a[i] / 2;
carry = 0;
}
}
}
else
{
BigNum begin(1), end("500000000000000000000000000000"); // 500000000000000000000000000000
while (begin < end)
{
BigNum mid = (begin + end) / 2;
BigNum res = mid * op2;
if (res == 0 || res >= *this)
end = mid;
else
begin = mid + 1;
}
temp = begin;
if (temp == 1)
return 0;
int tmp = len - 1;
while (temp.a[tmp] == 0)
{
temp.a[tmp] = 9;
tmp++;
}
temp.a[tmp]--;
return temp;
}
return temp;
}
When I run it, sometimes I get the error "malloc(): corrupted top size", sometimes it will run and then nothing happens, when I debug it, I find that the problem is in the "mygcd" function, the algorithm I use is too slow for a huge The algorithm I'm using is too slow for the huge number, but I don't know how to change it. I'm not sure where in the two files something is going wrong that I don't know about, and I can't guarantee that all the algorithms I'm using are correct and appropriate. Can anyone help me? Thanks a lot.
My system is Ubuntu 22.04.1LTS
gcc version 11.2.0
Now I deleted all the Chinese comments
After applying all of the changes suggested in the comments:
Remove using namespace std;
Remove superfluous BigNum::operator= and BigNum::BigNum(BigNum&)
Only implement fully BigNum::operator< and BigNum::operator==, and the other relational operators are based on those two operators
Make BigNum::operator << have the second argument as a const reference to BigNum.
the compiler here warns of j not being initialized.
BigNum BigNum::operator*(const BigNum &T) const //两个大数之间的相乘运算
{
BigNum ret;
int i, j, up;
//...
ret.len = i + j;
//...
}
You see the results of the run, with the Address Sanitizer showing that things are not working right.
When j is initialized to 0, then the program runs without error, at least the main you provided runs without error.
Edit:
On further inspection, you are accessing arr out-of-bounds. After changing the a to std::array<int, 999> a; and used at() within BigNum::operator *, a std::out_of_range exception is thrown:
Here is the updated code.
Also, please name your variables with meaningful names, not one letter names like a. Naming your variables properly will aid in understanding the code a lot better.

Finding unique entries of vector and deleting entries outside of interval [cMin, cMax]

I already managed to implement most of what I planned to do correctly, but somehow I struggle with the unique and cut method.
The unique method should sort the vector and delete all entries that appear more than once and the original vector should be overwritten with the shortened on. The cut method should delete all entries < cMin or > cMax.
Here is my try so far:
#include <cassert>
#include <iostream>
using std::cout;
using std::endl;
class Vector {
private:
int n;
double* coeff;
public:
Vector(int, double = 0);
~Vector();
Vector(const Vector&);
Vector& operator=(const Vector&);
int size() const;
double& operator()(int);
const double& operator()(int) const;
double max() const;
void sort();
void unique();
void cut(double Cmin, double Cmax);
void print() const;
};
Vector::Vector(int n, double init)
{
assert(n >= 0);
this->n = n;
if (n == 0) {
coeff = (double*)0;
}
else {
coeff = new double[n];
}
for (int i = 0; i < n; i++) {
coeff[i] = init;
}
}
Vector::Vector(const Vector& rhs)
{
n = rhs.n;
if (n > 0) {
coeff = new double[n];
}
else {
coeff = (double*)0;
}
for (int i = 0; i < n; i++) {
coeff[i] = rhs.coeff[i];
}
}
Vector::~Vector()
{
if (n > 0) {
delete[] coeff;
}
}
Vector& Vector::operator=(const Vector& rhs)
{
if (this != &rhs) {
if (n != rhs.n) {
if (n > 0) {
delete[] coeff;
}
n = rhs.n;
if (n > 0) {
coeff = new double[n];
}
else {
coeff = (double*)0;
}
}
for (int i = 0; i < n; i++) {
coeff[i] = rhs.coeff[i];
}
}
return *this;
}
int Vector::size() const
{
return n;
}
double& Vector::operator()(int j)
{
assert(j >= 1 && j <= n);
return coeff[j - 1];
}
const double& Vector::operator()(int j) const
{
assert(j >= 1 && j <= n);
return coeff[j - 1];
}
double Vector::max() const
{
double max = coeff[0];
for (int i = 1; i < n; i++) {
if (coeff[i] > max) {
max = coeff[i];
}
}
return max;
}
void Vector::sort()
{ //bubble-sort
double tmp = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (coeff[j] > coeff[j + 1]) {
tmp = coeff[j];
coeff[j] = coeff[j + 1];
coeff[j + 1] = tmp;
}
}
}
}
void Vector::unique()
{
sort();
int counter = 0;
Vector kopie = *this;
for (int i = 0; i < n; i++) {
if (i == 0 && coeff[i] != coeff[i + 1]) {
counter++;
}
if (i == n - 1 && coeff[i] != coeff[i - 1]) {
counter++;
}
if (i != 0 && i != n - 1 && coeff[i] != coeff[i - 1] && coeff[i] != coeff[i + 1]) {
counter++;
}
}
delete[] coeff;
coeff = new double[counter];
//to be continued...
}
void Vector::cut(double Cmin, double Cmax)
{
sort();
int counter = 0;
int j = 0;
Vector kopie = *this;
for (int i = 0; i < n; i++) {
if (coeff[i] >= Cmin && coeff[i] <= Cmax) {
counter++;
}
}
delete[] coeff;
coeff = new double[counter];
for (int i = 0; i < n; i++) {
if (kopie.coeff[i] >= Cmin && kopie.coeff[i] <= Cmax) {
coeff[j] = kopie.coeff[i];
j++;
if (j == n) {
break;
}
}
}
}
void Vector::print() const
{
for (int i = 0; i < n; i++) {
cout << coeff[i] << " ";
}
}
int main()
{
Vector X(8);
X.print();
cout << endl;
X(1) = 1.;
X(2) = 7.;
X(3) = 2.;
X(4) = 5.;
X(5) = 6.;
X(6) = 5.;
X(7) = 9.;
X(8) = 6.;
X.print();
cout << endl;
X.sort();
X.print();
cout << endl;
//X.unique();
//X.print();
//cout << endl;
X.cut(2, 6);
X.print();
cout << endl;
return 0;
}
For the unique function, rather than checking if it's legal to move the counter forward, I would just check if your current element and the next element aren't the same. If they are, set the next element's pointer to skip over the duplicate element.
Pseduocode:
For(int i = 0; i < n-1; i++) {
if(coef[i] == coef[i+1]) {
//Keep moving next element pointer until not equal. Probably use a while loop
}
}
The simplest solution that I can think of is something like this:
void Vector::unique()
{
size_t counter = 0;
double* copy = new double[n];
copy[counter++] = coeff[0]; // The first element is guaranteed to be unique
// Since coeff is sorted, copy will be sorted as well.
// Therefore, its enough to check only the last element of copy to
// the current element of coeff
for (size_t i = 1; i < n; i++)
{
if (coeff[i] != copy[counter])
{
copy[counter++] = coeff[i];
}
}
// copy already contains the data in the format that you want,
// but the reserved memory size may be larger than necessary.
// Reserve the correct amount of memory and copy the data there
delete[] coeff;
coeff = new double[counter];
std::memcpy(coeff, copy, counter*sizeof(double));
}
For cut() you can use a similar algorithm:
void Vector::cut(double Cmin, double Cmax)
{
size_t counter = 0;
double* copy = new double[n];
for (size_t i = 0; i < n; i++)
{
if (coeff[i] > Cmin && coeff[i] < Cmax)
{
copy[counter++] = coeff[i];
}
}
// Same story with memory size here as well
delete[] coeff;
coeff = new double[counter];
std::memcpy(coeff, copy, counter*sizeof(double));
}
Is there any reason why you can't use the standard library?
void Vector::unique() {
std::sort(coeff, std::next(coeff, n));
auto it = std::unique(coeff, std::next(coeff, n));
double* tmp = new double[n = std::distance(coeff, it)];
std::copy(coeff, it, tmp);
delete[] std::exchange(coeff, tmp);
}
void Vector::cut(double Cmin, double Cmax) {
auto it = std::remove_if(coeff, std::next(coeff, n),
[=] (double d) { return d < Cmin || d > Cmax; });
double* tmp = new double[n = std::distance(coeff, it)];
std::copy(coeff, it, tmp);
delete[] std::exchange(coeff, tmp);
}
To remove duplicates and sort, you can achieve it in three ways
Just using vector, sort + unique
sort( vec.begin(), vec.end() );
vec.erase( unique( vec.begin(), vec.end() ), vec.end() );
Convert to set (manually)
set<int> s;
unsigned size = vec.size();
for( unsigned i = 0; i < size; ++i )
s.insert( vec[i] ); vec.assign( s.begin(), s.end() );
Convert to set (using a constructor)
set<int> s( vec.begin(), vec.end() );
vec.assign( s.begin(), s.end() );
All three has different performance. You can use one depending upon your size and number of duplicates present.
To cut you can use algorithm library
std::remove, std::remove_if
Syntax
template< class ForwardIt, class T >
constexpr ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );
Possible implementation
First version
template< class ForwardIt, class T > ForwardIt remove(ForwardIt first, ForwardIt last, const T& value)
{ first = std::find(first, last, value);
if (first != last)
for(ForwardIt i = first; ++i != last; )
if (!(*i == value))
*first++ = std::move(*i); return first; }
Second version
template<class ForwardIt, class UnaryPredicate> ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p)
{ first = std::find_if(first, last, p);
if (first != last)
for(ForwardIt i = first; ++i != last; )
if (!p(*i)) *first++ = std::move(*i);
return first; }
Examples
The following code removes all spaces from a string by shifting all non-space characters to the left and then erasing the extra. This is an example of erase-remove idiom.
Run this code
#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>  
int main()
{ std::string str1 = "Text with some spaces"; str1.erase(std::remove(str1.begin(), str1.end(), ' '), str1.end());
std::cout << str1 << '\n';  
std::string str2 = "Text\n with\tsome \t whitespaces\n\n"; str2.erase(std::remove_if(str2.begin(), str2.end(), [](unsigned char x)
{return std::isspace(x);}), str2.end());
std::cout << str2 << '\n'; }
Output:
Textwithsomespaces
Textwithsomewhitespaces

Program crashed after end of scope

I was confused from this following codes, it's creating crash that I fully don't understand.
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
struct Store
{
int pos;
char character;
};
vector<struct Store> v_store;
void swap_inside_vector(vector<struct Store> &v_store, int a, int b)
{
struct Store tmp = v_store[b];
v_store[b] = v_store[a];
v_store[a] = tmp;
}
int find_inside_vector(vector<struct Store> &v_store, int pos)
{
for(int i = 0; i < v_store.size(); i++)
{
if(v_store[i].pos == pos)
return i;
}
return -1;
}
void swap_most_right_to_most_left(vector<struct Store> &v_store)
{
struct Store tmp = v_store[v_store.size() - 1];
for(int i = v_store.size(); i > 0; i--)
{
v_store[i] = v_store[i-1];
}
v_store[0] = tmp;
}
void print_char_inside_vector(vector<struct Store> &v) // used for debugging
{
for(int i = 0; i < v.size(); i++)
{
printf("%C", v[i].character);
}
}
int check_vector_original_state(vector<struct Store> &v_store)
{
for(int i = 1; i <= v_store.size(); i++)
{
if(v_store[i-1].pos != i)
return 0;
}
return 1;
}
int main()
{
int n;
char input_str[100];
for(int q = 1; scanf("\n%d", &n), n; q++)
{
scanf("%s", input_str);
vector<struct Store> original_store, tmp_origin_store, v_store;
vector<vector<struct Store> > store_vector;
original_store.clear();
tmp_origin_store.clear();
v_store.clear();
store_vector.clear();
for(int i = 1; i <= strlen(input_str); i++)
{
struct Store s = {.pos = i, .character = input_str[i-1]};
original_store.push_back(s);
}
tmp_origin_store = original_store;
v_store = tmp_origin_store;
for(int i = 1, current_pos = 0; i <= n; i++, current_pos++)
{
int vector_index = find_inside_vector(v_store, tmp_origin_store[current_pos].pos);
//printf("Processing -> [%d], Current Pos -> [%d]\n", i, current_pos);
for(int z = 0; z < (current_pos + 1); z++)
{
if(vector_index == (v_store.size() - 1))
{
swap_most_right_to_most_left(v_store);
vector_index = 0;
}
else
{
swap_inside_vector(v_store, vector_index, vector_index + 1);
vector_index++;
}
//print_char_inside_vector(v_store);
//puts("");
}
//puts("");
store_vector.push_back(v_store);
if(check_vector_original_state(v_store))
{
store_vector.push_back(v_store);
break;
}
if(current_pos == (v_store.size() - 1))
{
tmp_origin_store = v_store;
current_pos = -1;
}
}
// debugging
/*for(int x = 0; x < store_vector.size(); x++)
{
printf("[%d] -> ", x + 1);
print_char_inside_vector(store_vector[x]);
puts("");
}*/
int target_pos;
if(store_vector.size() < n)
{
target_pos = n % store_vector.size();
}
else
{
target_pos = store_vector.size();
}
if(target_pos == 0)
{
printf("%d. ", q);
print_char_inside_vector(store_vector[0]);
puts("");
}
else
{
printf("%d. ", q);
print_char_inside_vector(store_vector[target_pos - 1]);
puts("");
}
}
}
What this program does is accepting input from STDIN, process it, and output it on STDOUT.
My expecting input is
3 ABCD
13 ACM
3 DAEQD
4 FAEQS
Expected output is
1. CABD
2. CAM
3. DAQDE
4. FQASE
My problem is, after inputing fourth input into STDIN, and after output is being shown, crash occured.
C:\Study>h.exe
3 ABCD
1. CABD
13 ACM
2. CAM
3 DAEQD
3. DAQDE
4 FAEQS
4. FQASE
0 [main] h 9092 cygwin_exception::open_stackdumpfile: Dumping stack trace to h.exe.stackdump
From my observation, I think the problem is at the vector, but it's just a guess.
Your code didn't compile :
//struct Store s = { .pos = i, .character = input_str[i - 1] }; // syntax not recongnized
Store s = { i, input_str[i - 1] }; // replaced with this.
After fixing this, the debugging immediatly identified an out of bound problem on a vector, which could lead to memory corruption issue. It's in swap_most_right_to_most_left():
for (int i = v_store.size(); i > 0; i--) { // you start with i=v_store.size()
v_store[i] = v_store[i - 1]; // and you immediately get out of bounds !
}
If you correct this instruction to :
for (int i = v_store.size()-1; i > 0; i--) {
v_store[i] = v_store[i - 1];
}
you'll get the expected output for the given input.

How to implement a max heap

I have the code to build a max heap, but it keeps on returning the same array I give it. I'm sure its a minor error, but I cant seem to figure it out. Any help is appreciated.
Compilable sample code:
#include <iostream>
#include <cmath>
class Heaparr {
public:
Heaparr();
void insert(int da);
int getLeft(int i) { return 2 * i; }
int getRight(int i) { return (2 * i) + 1; }
int getParent(int i) { return i / 2; }
int getMax() { return maxHeap[0]; }
void print();
void reheap(int num);
void makeArray();
void Build_Max_Heap(int maxHeap[], int heap_size);
void Max_Heapify(int heapArray[], int i, int heap_size);
void heapSort(int heapArray[]);
private:
int size;
int* maxHeap;
int index;
int i;
};
Heaparr::Heaparr() {
maxHeap = nullptr;
size = 0;
}
void Heaparr::insert(int da) {
size++;
int* tmp = new int[size];
for (int i = 0; i < size - 1; i++) {
tmp[i] = maxHeap[i];
}
tmp[size - 1] = da;
delete[] maxHeap;
maxHeap = tmp;
}
void Heaparr::heapSort(int maxHeap[]) {
int heap_size = size;
int n = size;
int temp;
Build_Max_Heap(maxHeap, heap_size);
for (int i = n - 1; i >= 1; i--) {
temp = maxHeap[0];
maxHeap[0] = maxHeap[i];
maxHeap[i] = temp;
heap_size = heap_size - 1;
Max_Heapify(maxHeap, 0, heap_size);
}
for (int i = 0; i < 8; i++) {
std::cout << maxHeap[i] << std::endl;
}
}
void Heaparr::Build_Max_Heap(int maxHeap[], int heap_size) {
int n = size;
for (int i = floor((n - 1) / 2); i >= 0; i--) {
Max_Heapify(maxHeap, i, heap_size);
}
return;
}
void Heaparr::Max_Heapify(int heapArray[], int i, int heap_size) {
// int n = size;
int largest = 0;
int l = getLeft(i);
int r = getRight(i);
if ((l <= heap_size) && (heapArray[l] > heapArray[i])) {
largest = l;
} else {
largest = i;
}
if ((r <= heap_size) && (heapArray[r] > heapArray[largest])) {
largest = r;
}
int temp;
if (largest != i) {
temp = heapArray[i];
heapArray[i] = heapArray[largest];
heapArray[largest] = temp;
Max_Heapify(heapArray, largest, heap_size);
}
return;
}
int main(int argc, char* argv[]) {
int hArray[8] = {5, 99, 32, 4, 1, 12, 15, 8};
Heaparr t;
t.heapSort(hArray);
for (auto v : hArray) {
std::cout << v << ", ";
}
std::cout << std::endl;
}
I made some fixed to the code (i try not to changed much the original code):
The getLeft, getRight and getParent formulas were wrong (ex: when i == 0 children must be 1 and 2 and with your code are 0 and 1. The return type was also wrong, should be int (array index).
Do you receive in all methods a int[] except in insert and the member variable that are double[], changed all to int[], if you need changed back all to double
Using std::swap for swap values in the array.
Adding the length of the array to heapSort (inside the method this info is lost, need to be passed by parameter).
Notes:
I dont see where you use the member variable maxHeap, because all methods except getMax and insert use the array passed by parameter and not the member variable (perhaps you should initialized in the constructor or in heapSort method.
Try to use std::vector instead of C Array
Code:
#include <iostream>
#include <cmath>
class Heaparr {
public:
Heaparr();
void insert(int da);
int getLeft(int i) { return 2 * i + 1; }
int getRight(int i) { return 2 * i + 2; }
int getParent(int i) { return (i - 1) / 2; }
int getMax() { return maxHeap[0]; }
void print();
void reheap(int num);
void makeArray();
void Build_Max_Heap(int heapArray[], int heap_size);
void Max_Heapify(int heapArray[], int i, int heap_size);
void heapSort(int heapArray[], int heap_size);
private:
int size;
int* maxHeap;
int index;
int i;
};
Heaparr::Heaparr() {
maxHeap = nullptr;
size = 0;
}
void Heaparr::insert(int da) {
size++;
int* tmp = new int[size];
for (int i = 0; i < size - 1; i++) {
tmp[i] = maxHeap[i];
}
tmp[size - 1] = da;
delete[] maxHeap;
maxHeap = tmp;
}
void Heaparr::heapSort(int heapArray[], int heap_size) {
size = heap_size;
int n = size;
Build_Max_Heap(heapArray, heap_size);
for (int i = n - 1; i >= 1; i--) {
std::swap(heapArray[0], heapArray[i]);
heap_size = heap_size - 1;
Max_Heapify(heapArray, 0, heap_size);
}
}
void Heaparr::Build_Max_Heap(int heapArray[], int heap_size) {
int n = size;
for (int i = floor((n - 1) / 2); i >= 0; i--) {
Max_Heapify(heapArray, i, heap_size);
}
return;
}
void Heaparr::Max_Heapify(int heapArray[], int i, int heap_size) {
// int n = size;
int largest = 0;
int l = getLeft(i);
int r = getRight(i);
if ((l < heap_size) && (heapArray[l] < heapArray[i])) {
largest = l;
} else {
largest = i;
}
if ((r < heap_size) && (heapArray[r] < heapArray[largest])) {
largest = r;
}
if (largest != i) {
std::swap(heapArray[i], heapArray[largest]);
Max_Heapify(heapArray, largest, heap_size);
}
return;
}
int main(int argc, char* argv[]) {
int hArray[8] = {5, 99, 32, 4, 1, 12, 15, 8};
Heaparr t;
t.heapSort(hArray, sizeof(hArray)/sizeof(hArray[0]));
for (auto v : hArray) {
std::cout << v << ", ";
}
std::cout << std::endl;
return 0;
}
Output:
99, 32, 15, 12, 8, 5, 4, 1,
Tested in GCC 4.9.0 with C++11
If you're willing to consider alternative implementations, then here is one:
#define MIN_TYPE 0
#define MAX_TYPE ~0
template<int TYPE,typename ITEM>
class Heap
{
public:
Heap(int iMaxNumOfItems);
virtual ~Heap();
public:
bool AddItem(ITEM* pItem);
bool GetBest(ITEM** pItem);
protected:
int BestOfTwo(int i,int j);
void SwapItems(int i,int j);
protected:
ITEM** m_aItems;
int m_iMaxNumOfItems;
int m_iCurrNumOfItems;
};
template<int TYPE,typename ITEM>
Heap<TYPE,ITEM>::Heap(int iMaxNumOfItems)
{
m_iCurrNumOfItems = 0;
m_iMaxNumOfItems = iMaxNumOfItems;
m_aItems = new ITEM*[m_iMaxNumOfItems];
if (!m_aItems)
throw "Insufficient Memory";
}
template<int TYPE,typename ITEM>
Heap<TYPE,ITEM>::~Heap()
{
delete[] m_aItems;
}
template<int TYPE,typename ITEM>
bool Heap<TYPE,ITEM>::AddItem(ITEM* pItem)
{
if (m_iCurrNumOfItems == m_iMaxNumOfItems)
return false;
m_aItems[m_iCurrNumOfItems] = pItem;
for (int i=m_iCurrNumOfItems,j=(i+1)/2-1; j>=0; i=j,j=(i+1)/2-1)
{
if (BestOfTwo(i,j) == i)
SwapItems(i,j);
else
break;
}
m_iCurrNumOfItems++;
return true;
}
template<int TYPE,typename ITEM>
bool Heap<TYPE,ITEM>::GetBest(ITEM** pItem)
{
if (m_iCurrNumOfItems == 0)
return false;
m_iCurrNumOfItems--;
*pItem = m_aItems[0];
m_aItems[0] = m_aItems[m_iCurrNumOfItems];
for (int i=0,j=(i+1)*2-1; j<m_iCurrNumOfItems; i=j,j=(i+1)*2-1)
{
if (j+1 < m_iCurrNumOfItems)
j = BestOfTwo(j,j+1);
if (BestOfTwo(i,j) == j)
SwapItems(i,j);
else
break;
}
return true;
}
template<int TYPE,typename ITEM>
int Heap<TYPE,ITEM>::BestOfTwo(int i,int j)
{
switch (TYPE)
{
case MIN_TYPE: return *m_aItems[i]<*m_aItems[j]? i:j;
case MAX_TYPE: return *m_aItems[i]>*m_aItems[j]? i:j;
}
throw "Illegal Type";
}
template<int TYPE,typename ITEM>
void Heap<TYPE,ITEM>::SwapItems(int i,int j)
{
ITEM* pItem = m_aItems[i];
m_aItems[i] = m_aItems[j];
m_aItems[j] = pItem;
}
And here is a usage example:
typedef int ITEM;
#define SIZE 1000
#define RANGE 100
void test()
{
ITEM* pItem;
ITEM aArray[SIZE];
Heap<MIN_TYPE,ITEM> cHeap(SIZE);
srand((unsigned int)time(NULL));
for (int i=0; i<SIZE; i++)
{
aArray[i] = rand()%RANGE;
cHeap.AddItem(aArray+i);
}
for (int i=0; i<SIZE; i++)
{
cHeap.GetBest(&pItem);
printf("%d\n",*pItem);
}
}
Description:
This class stores up to N items of type T
It allows adding an item or extracting the best item
Supported operations are accomplished at O(log(n)), where n is the current number of items
Remarks:
T is determined at declaration and N is determined at initialization
The meaning of "best", either minimal or maximal, is determined at declaration
In order to support Heap<MIN,T> and Heap<MAX,T>, one of the following options must be viable:
bool operator<(T,T) and bool operator>(T,T)
bool T::operator<(T) and bool T::operator>(T)
T::operator P(), where P is a type, for which, one of the above options is viable

Algorithm that builds heap

I am trying to implement build_max_heap function that creates the heap( as it is written in Cormen's "introduction do algorithms" ). But I am getting strange error and i could not localize it. My program successfully give random numbers to table, show them but after build_max_heap() I am getting strange numbers, that are probably because somewhere my program reached something out of the table, but I can not find this error. I will be glad for any help.
For example I get the table:
0 13 18 0 22 15 24 19 5 23
And my output is:
24 7 5844920 5 22 15 18 19 0 23
My code:
#include <iostream>
#include <ctime>
#include <stdlib.h>
const int n = 12; // the length of my table, i will onyl use indexes 1...n-1
struct heap
{
int *tab;
int heap_size;
};
void complete_with_random(heap &heap2)
{
srand(time(NULL));
for (int i = 1; i <= heap2.heap_size; i++)
{
heap2.tab[i] = rand() % 25;
}
heap2.tab[0] = 0;
}
void show(heap &heap2)
{
for (int i = 1; i < heap2.heap_size; i++)
{
std::cout << heap2.tab[i] << " ";
}
}
int parent(int i)
{
return i / 2;
}
int left(int i)
{
return 2 * i;
}
int right(int i)
{
return 2 * i + 1;
}
void max_heapify(heap &heap2, int i)
{
if (i >= heap2.heap_size || i == 0)
{
return;
}
int l = left(i);
int r = right(i);
int largest;
if (l <= heap2.heap_size || heap2.tab[l] > heap2.tab[i])
{
largest = l;
}
else
{
largest = i;
}
if (r <= heap2.heap_size || heap2.tab[r] > heap2.tab[i])
{
largest = r;
}
if (largest != i)
{
std::swap(heap2.tab[i], heap2.tab[largest]);
max_heapify(heap2, largest);
}
}
void build_max_heap(heap &heap2)
{
for (int i = heap2.heap_size / 2; i >= 1; i--)
{
max_heapify(heap2, i);
}
}
int main()
{
heap heap1;
heap1.tab = new int[n];
heap1.heap_size = n - 1;
complete_with_random(heap1);
show(heap1);
std::cout << std::endl;
build_max_heap(heap1);
show(heap1);
}
Indeed, the table is accessed with out-of-bounds indexes.
if (l <= heap2.heap_size || heap2.tab[l] > heap2.tab[i])
^^
I think you meant && in this condition.
The same for the next branch with r.
In case you're still having problems, below is my own implementation that you might use for reference. It was also based on Cormen et al. book, so it's using more or less the same terminology. It may have arbitrary types for the actual container, the comparison and the swap functions. It provides a public queue-like interface, including key incrementing.
Because it's part of a larger software collection, it's using a few entities that are not defined here, but I hope the algorithms are still clear. CHECK is only an assertion mechanism, you can ignore it. You may also ignore the swap member and just use std::swap.
Some parts of the code are using 1-based offsets, others 0-based, and conversion is necessary. The comments above each method indicate this.
template <
typename T,
typename ARRAY = array <T>,
typename COMP = fun::lt,
typename SWAP = fun::swap
>
class binary_heap_base
{
protected:
ARRAY a;
size_t heap_size;
SWAP swap_def;
SWAP* swap;
// 1-based
size_t parent(const size_t n) { return n / 2; }
size_t left (const size_t n) { return n * 2; }
size_t right (const size_t n) { return n * 2 + 1; }
// 1-based
void heapify(const size_t n = 1)
{
T& x = a[n - 1];
size_t l = left(n);
size_t r = right(n);
size_t select =
(l <= heap_size && COMP()(x, a[l - 1])) ?
l : n;
if (r <= heap_size && COMP()(a[select - 1], a[r - 1]))
select = r;
if (select != n)
{
(*swap)(x, a[select - 1]);
heapify(select);
}
}
// 1-based
void build()
{
heap_size = a.length();
for (size_t n = heap_size / 2; n > 0; n--)
heapify(n);
}
// 1-based
size_t advance(const size_t k)
{
size_t n = k;
while (n > 1)
{
size_t pn = parent(n);
T& p = a[pn - 1];
T& x = a[n - 1];
if (!COMP()(p, x)) break;
(*swap)(p, x);
n = pn;
}
return n;
}
public:
binary_heap_base() { init(); set_swap(); }
binary_heap_base(SWAP& s) { init(); set_swap(s); }
binary_heap_base(const ARRAY& a) { init(a); set_swap(); }
binary_heap_base(const ARRAY& a, SWAP& s) { init(a); set_swap(s); }
void init() { a.init(); build(); }
void init(const ARRAY& a) { this->a = a; build(); }
void set_swap() { swap = &swap_def; }
void set_swap(SWAP& s) { swap = &s; }
bool empty() { return heap_size == 0; }
size_t size() { return heap_size; }
size_t length() { return heap_size; }
void reserve(const size_t len) { a.reserve(len); }
const T& top()
{
CHECK (heap_size != 0, eshape());
return a[0];
}
T pop()
{
CHECK (heap_size != 0, eshape());
T x = a[0];
(*swap)(a[0], a[heap_size - 1]);
heap_size--;
heapify();
return x;
}
// 0-based
size_t up(size_t n, const T& x)
{
CHECK (n < heap_size, erange());
CHECK (!COMP()(x, a[n]), ecomp());
a[n] = x;
return advance(n + 1) - 1;
}
// 0-based
size_t push(const T& x)
{
if (heap_size == a.length())
a.push_back(x);
else
a[heap_size] = x;
return advance(++heap_size) - 1;
}
};