Guest User

Untitled

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. (define (value node) (if (< (length node) 1) '() (car node)))
  2. (define (left node) (if (< (length node) 2) '() (cadr node)))
  3. (define (right node) (if (< (length node) 3) '() (caddr node)))
  4.  
  5. (define cache '())
  6.  
  7. (define (path parent)
  8. (if (not (assoc parent cache))
  9. (set! cache (cons (list parent (calc-path parent)) cache)))
  10. (cadr (assoc parent cache)))
  11.  
  12. (define (calc-path parent)
  13. (if (or (null? (left parent)) (null? (right parent)))
  14. (list (value parent))
  15. (cons (value parent)
  16. (max-path (path (left parent)) (path (right parent))))))
  17.  
  18. (define (max-path left right)
  19. (if (> (apply + left) (apply + right))
  20. left
  21. right))
  22.  
  23. (letrec ((d1 '(6)) (d2 '(7)) (d3 '(1)) (d4 '(2))
  24. (c1 (list 2 d1 d2)) (c2 (list 4 d2 d3)) (c3 (list 3 d3 d4))
  25. (b1 (list 5 c1 c2)) (b2 (list 9 c2 c3))
  26. (root (list 1 b1 b2)))
  27. (path root))
Add Comment
Please, Sign In to add comment