Functions in scheme/racket.
Working on a few functions using a binary search tree. I have already defined helper functions to be:
;; returns value of node
(define (value node)
(if (null? node) '()
(car node)))
;; returns left subtree of node
(define (left node)
(if (null? node) '()
(cadr node)))
;; returns right subtree of node
(define (right node)
(if (null? node) '()
(caddr node)))
and I am trying to write a function size that takes a tree as a parameter and returns the number of non-null nodes in the given tree
It seems you’re very close. Try this (untested):
Though, personally, I would much rather prefer to use
#fas the null value, rather than'(). In that case, usenotinstead ofnull?in the first line.