For Dr. Racket/Scheme Programmers #lang eopl
Write a procedure number-leaves that siege a bintree, and produces a bintree enjoy the peculiar, ate the space of the leaves are numbered starting from 0. For sample,
(number-leaves
(interior-node ‘ foo
(interior-node ‘ cork
(leaf 26)
(leaf 12))
(interior-node ‘ baz
(leaf 11)
(interior-node ‘ quux
(leaf 117)
(leaf 14))
should return
(foo
(cork 0 1)
(baz
2
(quux 3 4)))
load-relative “31.scm”)
(explain (number-leaves tree)
(explain (survey tree against)
(if (leaf? tree)
(cons (+ against 1) (leaf against))
(let* ((node-data (contents-of tree))
(lson-result (survey (lson tree) against))
(counter-after-lson (car lson-result))
(result-lson (cdr lson-result))
(rson-result (survey (rson tree) against-after-lson))
(result-against (car rson-result))
(result-rson (cdr rson-result))
(result-tree (interior-node node-data result-lson result-rson)))
(cons result-against result-tree))))
(cdr (survey tree 0)))