PasteRack.org
Paste # 47680
2018-03-13 15:40:23

Forked from paste # 61384.

Fork as a new paste.

Paste viewed 226 times.


Embed:

tree max

  1. #lang racket
  2. (define t '(10 (5(4(2 ()()) (26()())) (21(15()()) (23 ()()))) (11(6()())(13()()))))
  3.  
  4. (define (leaf? t) (null? t))
  5. (define (tree-left-branch t) (cadr t))
  6. (define (tree-right-branch t) (caddr t))
  7.  
  8. (define (max/when x y)
  9.   (if (and x y)
  10.       (max x y)
  11.       (if x x y)))
  12.  
  13. (define (tree-fn t)
  14.   (if (leaf? t)
  15.       #f
  16.       (let ((val-1 (tree-fn (tree-left-branch t)))
  17.             (val-2 (tree-fn (tree-right-branch t))))
  18.         (max/when val-1 val-2))))
  19.  
  20. (tree-fn t)

=>

#f