binary-trees C++ g++ #2 program
source code
/* The Computer Language Benchmarks Game
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
*
* Contributed by Jon Harrop
* Modified by Alex Mizrahi
* *reset*
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
struct Node {
Node *l, *r;
Node() : l(0), r(0) {}
Node(Node *l2, Node *r2) : l(l2), r(r2) {}
~Node() { delete l; delete r; }
int check() const {
if (l)
return l->check() + 1 + r->check();
else return 1;
}
};
Node *make(int d) {
if (d == 0) return new Node();
return new Node(make(d-1), make(d-1));
}
int main(int argc, char *argv[]) {
int min_depth = 4,
max_depth = std::max(min_depth+2,
(argc == 2 ? atoi(argv[1]) : 10)),
stretch_depth = max_depth+1;
{
Node *c = make(stretch_depth);
std::cout << "stretch tree of depth " << stretch_depth << "\t "
<< "check: " << c->check() << std::endl;
delete c;
}
Node *long_lived_tree=make(max_depth);
for (int d=min_depth; d<=max_depth; d+=2) {
int iterations = 1 << (max_depth - d + min_depth), c=0;
for (int i=1; i<=iterations; ++i) {
Node *a = make(d);
c += a->check();
delete a;
}
std::cout << iterations << "\t trees of depth " << d << "\t "
<< "check: " << c << std::endl;
}
std::cout << "long lived tree of depth " << max_depth << "\t "
<< "check: " << (long_lived_tree->check()) << "\n";
delete long_lived_tree;
return 0;
}
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Mon, 04 May 2020 17:19:51 GMT
MAKE:
/usr/bin/g++ -c -pipe -O3 -fomit-frame-pointer -march=core2 binarytrees.gpp-2.c++ -o binarytrees.gpp-2.c++.o && \
/usr/bin/g++ binarytrees.gpp-2.c++.o -o binarytrees.gpp-2.gpp_run
rm binarytrees.gpp-2.c++
5.65s to complete and log all make actions
COMMAND LINE:
./binarytrees.gpp-2.gpp_run 21
PROGRAM OUTPUT:
stretch tree of depth 22 check: 8388607
2097152 trees of depth 4 check: 65011712
524288 trees of depth 6 check: 66584576
131072 trees of depth 8 check: 66977792
32768 trees of depth 10 check: 67076096
8192 trees of depth 12 check: 67100672
2048 trees of depth 14 check: 67106816
512 trees of depth 16 check: 67108352
128 trees of depth 18 check: 67108736
32 trees of depth 20 check: 67108832
long lived tree of depth 21 check: 4194303