Other versions: 1.0

Change picture:

Choose file:

binary tree

  1. Using tuple
  2. Using list
  3. Using class

1 Using tuple

 Top
routine  BottomUpTree(item:int,  depth:any)=>tuple<int,any,any>
{
     if(  depth  >  0){
          i  =  item  +  item;
          depth  =  depth  -  1;
          return(  item,  BottomUpTree(i-1,  depth),  BottomUpTree(i,  depth));
     }
     return(  item,  null,  null  );
}

routine  ItemCheck((item:int,  left:any,  right:any))=>int
{
     if(  left  !=  null  )return  item  +  ItemCheck(left)  -  ItemCheck(right);
     return  item;
}

routine  main(N  =  10)
{
     mindepth  =  4;
     maxdepth  =  mindepth  +  2;
     if(  maxdepth  <  N)  maxdepth  =  N;

     stretchdepth  =  maxdepth  +  1;
     stretchtree  =  BottomUpTree(0,  stretchdepth);

     stdio.printf("stretch tree of depth %d\t check: %d\n",  stretchdepth,  ItemCheck(stretchtree));

     longlivedtree  =  BottomUpTree(0,  maxdepth);

     for(  depth  =  mindepth  :  2  :  maxdepth  ){
          iterations  =  2  **  (maxdepth  -  depth  +  mindepth);
          check  =  0;
          for(  i  =  1  :  iterations  )
               check  =  check  +  ItemCheck(BottomUpTree(1,  depth))  +  ItemCheck(BottomUpTree(-1,  depth));

          stdio.printf("%d\t trees of depth %d\t check: %d\n",  iterations*2,  depth,  check);
     }
     stdio.printf("long lived tree of depth %d\t check: %d\n",  maxdepth,  ItemCheck(longlivedtree));
#{
#}

}


2 Using list

 Top
routine  BottomUpTree(item,  depth)
{
     if(  depth  >  0){
          i  =  item  +  item;
          depth  =  depth  -  1;
          left  =  BottomUpTree(i-1,  depth);
          right  =  BottomUpTree(i,  depth);
          return{  item,  left,  right  };
     }
     return{  item  };
}

routine  ItemCheck(tree)
{
     #stdio.println( stdlib.about(tree) );
     if(  tree.size()  ==  3){
          return  tree[0]  +  ItemCheck(tree[1])  -  ItemCheck(tree[2]);
     }
     return  tree[0];
}

routine  main(N  =  10)
{
     mindepth  =  4;
     maxdepth  =  mindepth  +  2;
     if(  maxdepth  <  N)  maxdepth  =  N;

     stretchdepth  =  maxdepth  +  1;
     stretchtree  =  BottomUpTree(0,  stretchdepth);

     stdio.printf("stretch tree of depth %d\t check: %d\n",  stretchdepth,  ItemCheck(stretchtree));

     longlivedtree  =  BottomUpTree(0,  maxdepth);

     for(  depth  =  mindepth  :  2  :  maxdepth  ){
          iterations  =  2  **  (maxdepth  -  depth  +  mindepth);
          check  =  0;
          for(  i  =  1  :  iterations  )
               check  =  check  +  ItemCheck(BottomUpTree(1,  depth))  +  ItemCheck(BottomUpTree(-1,  depth));

          stdio.printf("%d\t trees of depth %d\t check: %d\n",  iterations*2,  depth,  check);
     }
     stdio.printf("long lived tree of depth %d\t check: %d\n",  maxdepth,  ItemCheck(longlivedtree));
#{
#}

}


3 Using class

 Top
final  class  Node
{
     my  item  =  0;
     my  left  :  Node;
     my  right  :  Node;
}

routine  BottomUpTree(item,  depth)
{
     if(  depth  >  0){
          i  =  item  +  item;
          depth  =  depth  -  1;
          left  =  BottomUpTree(i-1,  depth);
          right  =  BottomUpTree(i,  depth);
          return  Node{  item,  left,  right  };
     }
     return  Node{  item  };
}

routine  ItemCheck(tree)
{
     #stdio.println( stdlib.about(tree) );
     if(  tree.left  !=  nil  ){
          return  tree.item  +  ItemCheck(tree.left)  -  ItemCheck(tree.right);
     }
     return  tree.item;
}

routine  main(N  =  10)
{
     mindepth  =  4;
     maxdepth  =  mindepth  +  2;
     if(  maxdepth  <  N)  maxdepth  =  N;

     stretchdepth  =  maxdepth  +  1;
     stretchtree  =  BottomUpTree(0,  stretchdepth);

     stdio.printf("stretch tree of depth %d\t check: %d\n",  stretchdepth,  ItemCheck(stretchtree));

     longlivedtree  =  BottomUpTree(0,  maxdepth);

     for(  depth  =  mindepth  :  2  :  maxdepth  ){
          iterations  =  2  **  (maxdepth  -  depth  +  mindepth);
          check  =  0;
          for(  i  =  1  :  iterations  )
               check  =  check  +  ItemCheck(BottomUpTree(1,  depth))  +  ItemCheck(BottomUpTree(-1,  depth));

          stdio.printf("%d\t trees of depth %d\t check: %d\n",  iterations*2,  depth,  check);
     }
     stdio.printf("long lived tree of depth %d\t check: %d\n",  maxdepth,  ItemCheck(longlivedtree));
#{
#}

}

view count 908 times
created at 2009-02-20, 16:25 GMT
modified at 2009-09-17, 02:19 GMT

12 3
456789 10
111213141516 17
181920212223 24
2526272829 30 31

fu: Many thanks (Jul.04,04:29)

klabim: fixed Hi, great, now my test works now :- ). (Jun.30,17:51)

Nightwalker: Few suggestions (Jul.03,14:37)

This site is powered by Dao
Copyright (C) 2009,2010, daovm.net.
Webmaster: admin@daovm.net