| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#include <math.h> |
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
|
| 45 |
|
| 46 |
typedef struct tn { |
| 47 |
struct tn* left; |
| 48 |
struct tn* right; |
| 49 |
long item; |
| 50 |
} treeNode; |
| 51 |
|
| 52 |
|
| 53 |
treeNode* NewTreeNode(treeNode* left, treeNode* right, long item) |
| 54 |
{ |
| 55 |
treeNode* new; |
| 56 |
|
| 57 |
new = (treeNode*)malloc(sizeof(treeNode)); |
| 58 |
|
| 59 |
new->left = left; |
| 60 |
new->right = right; |
| 61 |
new->item = item; |
| 62 |
|
| 63 |
return new; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
long ItemCheck(treeNode* tree) |
| 68 |
{ |
| 69 |
if (tree->left == NULL) |
| 70 |
return tree->item; |
| 71 |
else |
| 72 |
return tree->item + ItemCheck(tree->left) - ItemCheck(tree->right); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
treeNode* BottomUpTree(long item, unsigned depth) |
| 77 |
{ |
| 78 |
if (depth > 0) |
| 79 |
return NewTreeNode |
| 80 |
( |
| 81 |
BottomUpTree(2 * item - 1, depth - 1), |
| 82 |
BottomUpTree(2 * item, depth - 1), |
| 83 |
item |
| 84 |
); |
| 85 |
else |
| 86 |
return NewTreeNode(NULL, NULL, item); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
void DeleteTree(treeNode* tree) |
| 91 |
{ |
| 92 |
if (tree->left != NULL) |
| 93 |
{ |
| 94 |
DeleteTree(tree->left); |
| 95 |
DeleteTree(tree->right); |
| 96 |
} |
| 97 |
|
| 98 |
free(tree); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
int main(int argc, char* argv[]) |
| 103 |
{ |
| 104 |
unsigned N, depth, minDepth, maxDepth, stretchDepth; |
| 105 |
treeNode *stretchTree, *longLivedTree, *tempTree; |
| 106 |
|
| 107 |
N = atol(argv[1]); |
| 108 |
|
| 109 |
minDepth = 4; |
| 110 |
|
| 111 |
if ((minDepth + 2) > N) |
| 112 |
maxDepth = minDepth + 2; |
| 113 |
else |
| 114 |
maxDepth = N; |
| 115 |
|
| 116 |
stretchDepth = maxDepth + 1; |
| 117 |
|
| 118 |
stretchTree = BottomUpTree(0, stretchDepth); |
| 119 |
printf |
| 120 |
( |
| 121 |
"stretch tree of depth %u\t check: %li\n", |
| 122 |
stretchDepth, |
| 123 |
ItemCheck(stretchTree) |
| 124 |
); |
| 125 |
|
| 126 |
DeleteTree(stretchTree); |
| 127 |
|
| 128 |
longLivedTree = BottomUpTree(0, maxDepth); |
| 129 |
|
| 130 |
for (depth = minDepth; depth <= maxDepth; depth += 2) |
| 131 |
{ |
| 132 |
long i, iterations, check; |
| 133 |
|
| 134 |
iterations = pow(2, maxDepth - depth + minDepth); |
| 135 |
|
| 136 |
check = 0; |
| 137 |
|
| 138 |
for (i = 1; i <= iterations; i++) |
| 139 |
{ |
| 140 |
tempTree = BottomUpTree(i, depth); |
| 141 |
check += ItemCheck(tempTree); |
| 142 |
DeleteTree(tempTree); |
| 143 |
|
| 144 |
tempTree = BottomUpTree(-i, depth); |
| 145 |
check += ItemCheck(tempTree); |
| 146 |
DeleteTree(tempTree); |
| 147 |
} |
| 148 |
|
| 149 |
printf |
| 150 |
( |
| 151 |
"%li\t trees of depth %u\t check: %li\n", |
| 152 |
iterations * 2, |
| 153 |
depth, |
| 154 |
check |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
printf |
| 159 |
( |
| 160 |
"long lived tree of depth %u\t check: %li\n", |
| 161 |
maxDepth, |
| 162 |
ItemCheck(longLivedTree) |
| 163 |
); |
| 164 |
|
| 165 |
return 0; |
| 166 |
} |