binary-tree-cpp.nut 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // The Computer Language Benchmarks Game
  2. // http://benchmarksgame.alioth.debian.org/
  3. // contributed by Mike Pall
  4. array_t BottomUpTree(int_t item, int_t depth){
  5. //array_t ary = [];//array(3);
  6. //ary.clear();
  7. //ary.push_back(item);
  8. if (depth > 0){
  9. int_t i = item + item;
  10. --depth;
  11. //ary.push_back(BottomUpTree(i-1, depth));
  12. //ary.push_back(BottomUpTree(i, depth));
  13. return [ item, BottomUpTree(i-1, depth), BottomUpTree(i, depth) ];
  14. }
  15. //return ary;
  16. return [ item ];
  17. }
  18. int_t ItemCheck(array_t tree){
  19. //if (tree.get(1, false)) return tree[0] + ItemCheck(tree[1]) - ItemCheck(tree[2])
  20. if (tree.len() > 1) return tree[0] + ItemCheck(tree[1]) - ItemCheck(tree[2])
  21. return tree[0]
  22. }
  23. int_t main()
  24. {
  25. double_t start = os.clock()
  26. //check_delayed_release_hooks(false);
  27. int_t N = vargv.get(1, 14).tointeger();
  28. int_t mindepth = 4
  29. int_t maxdepth = mindepth + 2
  30. if (maxdepth < N) maxdepth = N
  31. {
  32. int_t stretchdepth = maxdepth + 1
  33. array_t stretchtree = BottomUpTree(0, stretchdepth)
  34. printf("stretch tree of depth %d\t check: %d\n",
  35. stretchdepth, ItemCheck(stretchtree));
  36. }
  37. array_t longlivedtree = BottomUpTree(0, maxdepth)
  38. for(int_t depth=mindepth; depth <= maxdepth; depth += 2){
  39. int_t iterations = math.pow(2, (maxdepth - depth + mindepth)).tointeger()
  40. int_t check = 0
  41. for(int_t i=0; i < iterations; ++i){
  42. check += (ItemCheck(BottomUpTree(1, depth))) +
  43. (ItemCheck(BottomUpTree(-1, depth)));
  44. }
  45. printf("%d\t trees of depth %d\t check: %d\n",
  46. iterations*2, depth, check);
  47. }
  48. printf("long lived tree of depth %d\t check: %d\n",
  49. maxdepth, ItemCheck(longlivedtree));
  50. printf("binary-tree \t%d \t%f\n", N, os.clock()-start);
  51. return 0;
  52. }
  53. main();