How to continue evaluating a binary tree in scheme
I need to evaluate a scheme function that inputs a number and a binary
tree and it outputs the data expression in the binary tree that is of the
same depth as the number. For example the root of a tree is 1 and the root
of the sub trees are 2 and so on.
This is what I have so far and I keep getting the error message Error in
null?: expected a list; got '1'. (This is another method of solving a
problem that I had asked earlier about) Could you explain this using the
terms that I have already used as I am new to scheme programming.
Thank you
(define fetch-exp (ă (n bt)
(cond [(not (deep-enough? n bt)) ¤#f]
[(one? n) (root bt)]
[(deep-enough? n (left-tree bt))
(fetch-exp (left-tree bt) (sub1 n))]
[(deep-enough? n (right-tree bt))
(fetch-exp (right-tree bt) sub1 n)]
[else ¤#f])))
(define deep-enough?
(ă (n bt)
(cond [(> (tree-depth bt) n) ¤#t]
[(equal? (tree-depth bt) n) ¤#t]
[else ¤#f])))
No comments:
Post a Comment