Wednesday, 14 November 2012

Week 10: Tree and Depth

It is really hard for me to visualize the tree diagram; however, I think it's a bit easier to understand by writing the things down step by step.

There is a few important functions being used in this tree diagram section.

1. structure of genetic tree
(define-struct gtree (label left right))
2. the branches - defining the leaves
(define leaf1 (make-gtree "ACTG" empty empty))
3. defining the small trees
(define tree1 (make-gtree "AAA" leaf1 leaf2)) 
4.  to "harvest" the tree (knowing the branches). This is an example of recursion.

(define (harvest G)
  (cond
    [(not (gtree? G)) empty]
    [else
     (append
      (list (gtree-label G))
      (harvest (gtree-left G))
      (harvest (gtree-right G)))]))
It as well took me a while to understand the function "depth".
However, it is easier to interpret if you think it as counting for different layers.


(define (depth L)
  (cond
    [(not (list? L)) 0]
    [else (add1 (apply max (map depth L)))]))
For example,
(check-expect (depth (list 1 (list 2) 3))  2)
list 1 and 3 --> 1
list 2 --> 1
total = 2

Recursion is not a really hard chapter, yet, it takes one lots of time to understand or figure out what's happening, it is just complicated sometimes and one can get confused or lost very easily.

No comments:

Post a Comment