Advertisement
Guest User

Function_Tree

a guest
Nov 28th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1.  39 void foreach(itemt const *root, void (*func)(int)){
  2.  40         itemt const *cur = root;
  3.  41         func(cur->value);
  4.  42         if (cur->left){
  5.  43                 foreach(cur->left, func);
  6.  44         }
  7.  45         if (cur->right){
  8.  46                 foreach(cur->right, func);
  9.  47         }
  10.  48 }
  11.  49
  12.  50 itemt* map(itemt const *root, int (*func)(int)){
  13.  51           itemt const *cur = root;
  14.  52           itemt  *new_list = tree_new(func(cur->value));
  15.  53           if (cur->left){
  16.  54                   new_list->left = map(cur->left, func);
  17.  55                  }
  18.  56           if (cur->right){
  19.  57                   new_list->right = map(cur->right, func);
  20.  58           }
  21.  59           return new_list;
  22.  60   }
  23.  61
  24.  62 int foldl(itemt const *root, int (*func)(int, int), int acc){
  25.  63         itemt const *cur = root;
  26.  64         if (cur->left){        
  27.  65                 acc = foldl(cur->left, func, acc);
  28.  66         }      
  29.  67         if (cur->right){
  30.  68                 acc = foldl(cur->right, func, acc);
  31.  69         }      
  32.  70         acc = func(cur->value, acc);
  33.  71         return acc;
  34.  72 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement