...

Text file src/golang.org/x/exp/shootout/binary-tree.c

Documentation: golang.org/x/exp/shootout

     1// +build ignore
     2
     3/*
     4Redistribution and use in source and binary forms, with or without
     5modification, are permitted provided that the following conditions are met:
     6
     7    * Redistributions of source code must retain the above copyright
     8    notice, this list of conditions and the following disclaimer.
     9
    10    * Redistributions in binary form must reproduce the above copyright
    11    notice, this list of conditions and the following disclaimer in the
    12    documentation and/or other materials provided with the distribution.
    13
    14    * Neither the name of "The Computer Language Benchmarks Game" nor the
    15    name of "The Computer Language Shootout Benchmarks" nor the names of
    16    its contributors may be used to endorse or promote products derived
    17    from this software without specific prior written permission.
    18
    19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    22ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    26INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    27CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    28ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    29POSSIBILITY OF SUCH DAMAGE.
    30*/
    31
    32/* The Computer Language Shootout Benchmarks
    33   http://shootout.alioth.debian.org/
    34
    35   contributed by Kevin Carson
    36   compilation:
    37       gcc -O3 -fomit-frame-pointer -funroll-loops -static binary-trees.c -lm
    38       icc -O3 -ip -unroll -static binary-trees.c -lm
    39*/
    40
    41#include <math.h>
    42#include <stdio.h>
    43#include <stdlib.h>
    44
    45
    46typedef struct tn {
    47    struct tn*    left;
    48    struct tn*    right;
    49    long          item;
    50} treeNode;
    51
    52
    53treeNode* 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} /* NewTreeNode() */
    65
    66
    67long 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} /* ItemCheck() */
    74
    75
    76treeNode* 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} /* BottomUpTree() */
    88
    89
    90void DeleteTree(treeNode* tree)
    91{
    92    if (tree->left != NULL)
    93    {
    94        DeleteTree(tree->left);
    95        DeleteTree(tree->right);
    96    }
    97
    98    free(tree);
    99} /* DeleteTree() */
   100
   101
   102int 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        } /* for(i = 1...) */
   148
   149        printf
   150        (
   151            "%li\t trees of depth %u\t check: %li\n",
   152            iterations * 2,
   153            depth,
   154            check
   155        );
   156    } /* for(depth = minDepth...) */
   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} /* main() */

View as plain text