File: binary_tree.c

package info (click to toggle)
metalang99 1.13.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,428 kB
  • sloc: ansic: 6,748; python: 53; sh: 48; makefile: 18
file content (25 lines) | stat: -rw-r--r-- 683 bytes parent folder | download | duplicates (3)
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
// Sums all nodes of a binary tree, recursively.

#include <metalang99.h>

#define leaf(x)              ML99_choice(v(leaf), x)
#define node(lhs, data, rhs) ML99_choice(v(node), lhs, data, rhs)

#define sumTree(tree)                     ML99_match(tree, v(sumTree_))
#define sumTree_leaf_IMPL(x)              v(x)
#define sumTree_node_IMPL(lhs, data, rhs) ML99_add3(sumTree(v(lhs)), v(data), sumTree(v(rhs)))

/*
 *         4
 *        / \
 *       /   \
 *      /     \
 *     2       6
 *    / \     / \
 *   1   3   5   7
 */
#define TREE node(node(leaf(v(1)), v(2), leaf(v(3))), v(4), node(leaf(v(5)), v(6), leaf(v(7))))

ML99_ASSERT_EQ(sumTree(TREE), v(28));

int main(void) {}