Etude On A Binary Tree
April 25, 2017
As usual, we represent a binary tree as a triple of the value of a node and its two children. Thus, the sample problem is represented as:
(define xs '(1 (2 (4 (8 () ()) (9 () ())) (5 (10 () ()) (11 () ()))) (3 (6 (12 () ()) (13 () ())) (7 (14 () ()) (15 () ())))))
It might help to indent that expression like this:
(1 (2 (4 ( 8 () ()) ( 9 () ())) (5 (10 () ()) (11 () ())) (3 (6 (12 () ()) (13 () ())) (7 (14 () ()) (15 () ())))
Like all functions on binary trees, this is easy if you think recursively:
(define (f xs) (if (null? xs) 0 (- (car xs) (f (cadr xs)) (f (caddr xs)))))
And here is the solution of our sample problem:
> (f xs) -74
You can run the program at http://ideone.com/rGqCjv.
I’m amused by some of the discussion at Career Cup. The top-voted solution has a for
loop inside a while
loop, and uses a queue to keep track of which nodes remain to be visited; it’s not immediately clear how the program works. The second-highest-rated solution uses global variables to keep track of the odd-level and even-level sums. Most of the solutions use recursion, but make it hard to keep track of what is going on. The clearest solution, similar to ours but in C, is down-voted to -1; if I was Amazon, that’s the only solution I would accept.
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