Return Path in Tree
eharetea

Return the path of a given element within a tree.

Return the path of a given element within a tree.

Date Created:Tuesday October 14th, 2008 08:45 PM
Date Modified:Tuesday October 14th, 2008 08:48 PM

(define (return-path element tree)
   (if (eq? element (datum tree))
       (cons (datum tree) '())
       (let ((test (return-path-forest element (children tree))))
         (if (not (null? test))
             (cons (datum tree) test)
             '()))))

(define (return-path-forest element forest)
   (if (null? forest)
       '()
       (let ((test (return-path element (car forest))))
         (if (not (null? test))
             test
             (return-path-forest element (cdr forest))))))

(define my-tree
     (make-tree 'USA
      (list
       (make-tree 'MI
         (list
           (make-tree 'Ann-Arbor '())
           (make-tree 'Detroit '())
         )
       )
       (make-tree 'CA
         (list
           (make-tree 'Los-Angeles '())
           (make-tree 'Berkeley
              (list
                 (make-tree 'UC-Berkeley '())
              )
           )
           (make-tree 'San-Diego '())
         )
       )
      )
     )
)


;; usage: (return-path 'detroit my-tree)



Downloads:
Download: return.scm 1 KB

Please login or Click Here to register for downloads
Creative Commons License
Return Path in Tree by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com