| OLD | NEW | 
 | (Empty) | 
|    1 /* The Computer Language Benchmarks Game |  | 
|    2  * http://benchmarksgame.alioth.debian.org/ |  | 
|    3  |  | 
|    4    contributed by Kevin Carson |  | 
|    5    compilation: |  | 
|    6        gcc -O3 -fomit-frame-pointer -funroll-loops -static binary-trees.c -lm |  | 
|    7        icc -O3 -ip -unroll -static binary-trees.c -lm |  | 
|    8 */ |  | 
|    9  |  | 
|   10 #include <malloc.h> |  | 
|   11 #include <math.h> |  | 
|   12 #include <stdio.h> |  | 
|   13 #include <stdlib.h> |  | 
|   14  |  | 
|   15  |  | 
|   16 typedef struct tn { |  | 
|   17     struct tn*    left; |  | 
|   18     struct tn*    right; |  | 
|   19     long          item; |  | 
|   20 } treeNode; |  | 
|   21  |  | 
|   22  |  | 
|   23 treeNode* NewTreeNode(treeNode* left, treeNode* right, long item) |  | 
|   24 { |  | 
|   25     treeNode*    new; |  | 
|   26  |  | 
|   27     new = (treeNode*)malloc(sizeof(treeNode)); |  | 
|   28  |  | 
|   29     new->left = left; |  | 
|   30     new->right = right; |  | 
|   31     new->item = item; |  | 
|   32  |  | 
|   33     return new; |  | 
|   34 } /* NewTreeNode() */ |  | 
|   35  |  | 
|   36  |  | 
|   37 long ItemCheck(treeNode* tree) |  | 
|   38 { |  | 
|   39     if (tree->left == NULL) |  | 
|   40         return tree->item; |  | 
|   41     else |  | 
|   42         return tree->item + ItemCheck(tree->left) - ItemCheck(tree->right); |  | 
|   43 } /* ItemCheck() */ |  | 
|   44  |  | 
|   45  |  | 
|   46 treeNode* BottomUpTree(long item, unsigned depth) |  | 
|   47 { |  | 
|   48     if (depth > 0) |  | 
|   49         return NewTreeNode |  | 
|   50         ( |  | 
|   51             BottomUpTree(2 * item - 1, depth - 1), |  | 
|   52             BottomUpTree(2 * item, depth - 1), |  | 
|   53             item |  | 
|   54         ); |  | 
|   55     else |  | 
|   56         return NewTreeNode(NULL, NULL, item); |  | 
|   57 } /* BottomUpTree() */ |  | 
|   58  |  | 
|   59  |  | 
|   60 void DeleteTree(treeNode* tree) |  | 
|   61 { |  | 
|   62     if (tree->left != NULL) |  | 
|   63     { |  | 
|   64         DeleteTree(tree->left); |  | 
|   65         DeleteTree(tree->right); |  | 
|   66     } |  | 
|   67  |  | 
|   68     free(tree); |  | 
|   69 } /* DeleteTree() */ |  | 
|   70  |  | 
|   71  |  | 
|   72 int main(int argc, char* argv[]) |  | 
|   73 { |  | 
|   74     unsigned   N, depth, minDepth, maxDepth, stretchDepth; |  | 
|   75     treeNode   *stretchTree, *longLivedTree, *tempTree; |  | 
|   76  |  | 
|   77     N = atol(argv[1]); |  | 
|   78  |  | 
|   79     minDepth = 4; |  | 
|   80  |  | 
|   81     if ((minDepth + 2) > N) |  | 
|   82         maxDepth = minDepth + 2; |  | 
|   83     else |  | 
|   84         maxDepth = N; |  | 
|   85  |  | 
|   86     stretchDepth = maxDepth + 1; |  | 
|   87  |  | 
|   88     stretchTree = BottomUpTree(0, stretchDepth); |  | 
|   89     printf |  | 
|   90     ( |  | 
|   91         "stretch tree of depth %u\t check: %li\n", |  | 
|   92         stretchDepth, |  | 
|   93         ItemCheck(stretchTree) |  | 
|   94     ); |  | 
|   95  |  | 
|   96     DeleteTree(stretchTree); |  | 
|   97  |  | 
|   98     longLivedTree = BottomUpTree(0, maxDepth); |  | 
|   99  |  | 
|  100     for (depth = minDepth; depth <= maxDepth; depth += 2) |  | 
|  101     { |  | 
|  102         long    i, iterations, check; |  | 
|  103  |  | 
|  104         iterations = pow(2, maxDepth - depth + minDepth); |  | 
|  105  |  | 
|  106         check = 0; |  | 
|  107  |  | 
|  108         for (i = 1; i <= iterations; i++) |  | 
|  109         { |  | 
|  110             tempTree = BottomUpTree(i, depth); |  | 
|  111             check += ItemCheck(tempTree); |  | 
|  112             DeleteTree(tempTree); |  | 
|  113  |  | 
|  114             tempTree = BottomUpTree(-i, depth); |  | 
|  115             check += ItemCheck(tempTree); |  | 
|  116             DeleteTree(tempTree); |  | 
|  117         } /* for(i = 1...) */ |  | 
|  118  |  | 
|  119         printf |  | 
|  120         ( |  | 
|  121             "%li\t trees of depth %u\t check: %li\n", |  | 
|  122             iterations * 2, |  | 
|  123             depth, |  | 
|  124             check |  | 
|  125         ); |  | 
|  126     } /* for(depth = minDepth...) */ |  | 
|  127  |  | 
|  128     printf |  | 
|  129     ( |  | 
|  130         "long lived tree of depth %u\t check: %li\n", |  | 
|  131         maxDepth, |  | 
|  132         ItemCheck(longLivedTree) |  | 
|  133     ); |  | 
|  134  |  | 
|  135     return 0; |  | 
|  136 } /* main() */ |  | 
|  137  |  | 
| OLD | NEW |