## Etude On A Binary Tree

### April 25, 2017

We have today another in our occasional series of exercises on binary trees; the input tree need not necessarily be ordered or balanced:

Given a binary tree containing integers, find the sum of all nodes at an even distance from the root, less the sum of all nodes at an odd distance from the root.

For instance, given the binary tree shown below, the requested sum is 1 – 2 – 3 + 4 + 5 + 6 + 7 – 8 – 9 – 10 – 11 – 12 – 13 – 14 – 15 = -74:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

(I’m not an artist; you’ll have to imagine the lines connecting the various levels.)

Your task is to write a program to compute alternate sums and differences of the nodes of a binary tree. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

In Python.

Got up with this solution. Printing the tree could use some work though..

C11:

#include <stdio.h>

#include <stdlib.h>

typedef struct btree {

int val;

struct btree *l, *r;

} btree_s;

btree_s *branch(int val, btree_s *l, btree_s *r);

btree_s *leaf(int val);

void free_btree_s(btree_s *bt);

int etude(btree_s *bt);

int main(int argc, char **argv) {

btree_s *bt = branch(1,

branch(2,

branch(4,

leaf(8),

leaf(9)),

branch(5,

leaf(10),

leaf(11))),

branch(3,

branch(6,

leaf(12),

leaf(13)),

branch(7,

leaf(14),

leaf(15))));

printf("The etude is %d.\n", etude(bt));

free_btree_s(bt);

exit(0);

}

btree_s *branch(int val, btree_s *l, btree_s *r) {

btree_s *bt = malloc(sizeof(btree_s));

if (bt == NULL) {

fprintf(stderr, "memory allocation error\n");

exit(1);

}

bt->val = val;

bt->l = l;

bt->r = r;

return bt;

}

btree_s *leaf(int val) {

return branch(val, NULL, NULL);

}

void free_btree_s(btree_s *bt) {

if (bt == NULL) {

return;

}

free_btree_s(bt->l);

free_btree_s(bt->r);

free((void*)bt);

}

int etude(btree_s *bt) {

if (bt == NULL) {

return 0;

}

`return bt->val - (etude(bt->l) + etude(bt->r));`

}

Forth:

Here’s a slightly different recursive solution in Haskell. Uses an accumulating parameter so one of the two recursive calls is in tail position:

OK, here’s a fully tail recursive version, using a continuation function. Despite the superficial resemblance to the above, the tree is traversed in the opposite order – the second occurrences of scan1 and scan0 in the RHS are partial applications.

[\code]

scan0 (T n left right) k m = scan1 left (scan1 right k) (m+n)

scan0 _ k m = k m

scan1 (T n left right) k m = scan0 left (scan0 right k) (m-n)

scan1 _ k m = k m

main = print(scan0 t (\x->x) 0)

[/code]

Ooops:

MUMPS V1:

tree(1)=

tree(1,”left”)=2

tree(1,”right”)=3

tree(2)=

tree(2,”left”)=4

tree(2,”right”)=5

tree(3)=

tree(3,”left”)=6

tree(3,”right”)=7

tree(4)=

tree(4,”left”)=8

tree(4,”right”)=9

tree(5)=

tree(5,”left”)=10

tree(5,”right”)=11

tree(6)=

tree(6,”left”)=12

tree(6,”right”)=13

tree(7)=

tree(7,”left”)=14

tree(7,”right”)=15

tree(8)=

tree(9)=

tree(10)=

tree(11)=

tree(12)=

tree(13)=

tree(14)=

tree(15)=

MCL> type tree

tree ;New routine

;

n sum

s sum=0

d process(1,0,.sum)

w !,”Sum = “,sum

q

;

process (node,level,sum) ;

n dir

s sum=sum+$s(level#2=0:node,1:-node)

f dir=”left”,”right” d

. i $d(tree(node,dir)) d

. . d process(tree(node,dir),level+1,.sum)

q

MCL> d ^tree

Sum = -74