1 /* 2 Redistribution and use in source and binary forms, with or without 3 modification, are permitted provided that the following conditions are met: 4 5 * Redistributions of source code must retain the above copyright 6 notice, this list of conditions and the following disclaimer. 7 8 * Redistributions in binary form must reproduce the above copyright 9 notice, this list of conditions and the following disclaimer in the 10 documentation and/or other materials provided with the distribution. 11 12 * Neither the name of "The Computer Language Benchmarks Game" nor the 13 name of "The Computer Language Shootout Benchmarks" nor the names of 14 its contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* The Computer Language Shootout Benchmarks 31 http://shootout.alioth.debian.org/ 32 33 contributed by Kevin Carson 34 compilation: 35 gcc -O3 -fomit-frame-pointer -funroll-loops -static binary-trees.c -lm 36 icc -O3 -ip -unroll -static binary-trees.c -lm 37 */ 38 39 #include <math.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 43 44 typedef struct tn { 45 struct tn* left; 46 struct tn* right; 47 long item; 48 } treeNode; 49 50 51 treeNode* NewTreeNode(treeNode* left, treeNode* right, long item) 52 { 53 treeNode* new; 54 55 new = (treeNode*)malloc(sizeof(treeNode)); 56 57 new->left = left; 58 new->right = right; 59 new->item = item; 60 61 return new; 62 } /* NewTreeNode() */ 63 64 65 long ItemCheck(treeNode* tree) 66 { 67 if (tree->left == NULL) 68 return tree->item; 69 else 70 return tree->item + ItemCheck(tree->left) - ItemCheck(tree->right); 71 } /* ItemCheck() */ 72 73 74 treeNode* BottomUpTree(long item, unsigned depth) 75 { 76 if (depth > 0) 77 return NewTreeNode 78 ( 79 BottomUpTree(2 * item - 1, depth - 1), 80 BottomUpTree(2 * item, depth - 1), 81 item 82 ); 83 else 84 return NewTreeNode(NULL, NULL, item); 85 } /* BottomUpTree() */ 86 87 88 void DeleteTree(treeNode* tree) 89 { 90 if (tree->left != NULL) 91 { 92 DeleteTree(tree->left); 93 DeleteTree(tree->right); 94 } 95 96 free(tree); 97 } /* DeleteTree() */ 98 99 100 int main(int argc, char* argv[]) 101 { 102 unsigned N, depth, minDepth, maxDepth, stretchDepth; 103 treeNode *stretchTree, *longLivedTree, *tempTree; 104 105 N = atol(argv[1]); 106 107 minDepth = 4; 108 109 if ((minDepth + 2) > N) 110 maxDepth = minDepth + 2; 111 else 112 maxDepth = N; 113 114 stretchDepth = maxDepth + 1; 115 116 stretchTree = BottomUpTree(0, stretchDepth); 117 printf 118 ( 119 "stretch tree of depth %u\t check: %li\n", 120 stretchDepth, 121 ItemCheck(stretchTree) 122 ); 123 124 DeleteTree(stretchTree); 125 126 longLivedTree = BottomUpTree(0, maxDepth); 127 128 for (depth = minDepth; depth <= maxDepth; depth += 2) 129 { 130 long i, iterations, check; 131 132 iterations = pow(2, maxDepth - depth + minDepth); 133 134 check = 0; 135 136 for (i = 1; i <= iterations; i++) 137 { 138 tempTree = BottomUpTree(i, depth); 139 check += ItemCheck(tempTree); 140 DeleteTree(tempTree); 141 142 tempTree = BottomUpTree(-i, depth); 143 check += ItemCheck(tempTree); 144 DeleteTree(tempTree); 145 } /* for(i = 1...) */ 146 147 printf 148 ( 149 "%li\t trees of depth %u\t check: %li\n", 150 iterations * 2, 151 depth, 152 check 153 ); 154 } /* for(depth = minDepth...) */ 155 156 printf 157 ( 158 "long lived tree of depth %u\t check: %li\n", 159 maxDepth, 160 ItemCheck(longLivedTree) 161 ); 162 163 return 0; 164 } /* main() */ 165