Contents    Page-10    Prev    Next    Page+10    Index   

Binary Tree Recursion Pattern in Java

type myfun (Object tree) {
         if (interior? tree)
         return combine(myfun(left tree),
         myfun(right tree));
         else if (test tree) // leaf
         return baseanswer(tree);
         else return safeanswer; }


(public static int addnums(Object tree) {
    if ( consp(tree) )  // is this a Cons?
        return addnums(first((Cons)tree))
            +  addnums(rest((Cons)tree));
    else if ( numberp(tree) )
        return (int) (Integer) tree;
    else return 0; }