120 likes | 129 Views
Learn about tree manipulation and reimplementing pairs with examples and definitions from the Structure and Interpretation of Computer Programs (SICP) course. Explore various operations such as enumeration, mapping, and reversing trees. Enhance your programming skills with tree manipulation techniques.
E N D
6.001 SICP – October 7 6001-Trees Trevor Darrell trevor@csail.mit.edu 32-D512 Office Hour: W 11 6.001 web page: http://sicp.csail.mit.edu/ section web page: http://www.csail.mit.edu/~trevor/6001/ • Trees • Re-implement pairs? 6.001 SICP
6 2 4 8 Trees children or subtrees root • Type Definition: • Tree<C> = List<Tree<C>> | Leaf<C> • Leaf<C> = C • Example Operations: • leaf? • enum-leaves, map-tree, tree-ref, ... 4 2 6 8 6.001 SICP
Example: enumerate leaves Flatten tree to list of leaves: (enumerate-leaves (list 1 (list 3 4 5)(list 2 30))) ==> (1 3 4 5 2 30) (define (e-l x) (cond ((null? x) nil) ((not (pair? x)) (list x)) (else (append (e-l (car x)) (e-l (cdr x)))))) 6.001 SICP
Example: map-tree Write a procedure, (map-tree op tree), So if tree has value (((1 2) 3) (4 (5 6)) 7 (8 9 10)) then (map-tree inc tree) has value (((2 3) 4) (5 (6 7)) 8 (9 10 11)) You can use leaf? and map. 6.001 SICP
map-tree (define map-tree (lambda (op tree) (if (leaf? tree) (op tree) (map ??? tree)))) 6.001 SICP
map-tree (define map-tree (lambda (op tree) (if (leaf? tree) (op tree) (map (lambda (subtree) (map-tree op subtree)) tree)))) 6.001 SICP
tree-ref Given (((1 2) 3) (4 (5 6)) 7 (8 9 10)) To select the element 9 out of it, we use something like (second (fourth tree)) Instead let’s define tree-ref so that we can use (tree-ref (list 3 1) tree) 6.001 SICP
tree-ref (define tree-ref (lambda (index tree) (if (null? index) tree (tree-ref (cdr index) (list-ref tree (car index)))))) 6.001 SICP
Example: deep-reverse Reverse the order of a tree: (deep-reverse (list 1 (list 2 3) (list 4 5 6))) ==> ((6 5 4) (3 2) 1) (define (reverse lst) (if (null? lst) nil (append (reverse (cdr lst)) (list (car lst)))) (define (deep-reverse tree) (if (not (pair? tree)) tree (map deep-reverse (reverse tree))) ) 6.001 SICP
General tree-manip (define (tree-manip tree init leaf first rest accum) (cond ((null? tree) init) ((not (pair? tree)) (leaf tree)) (else (accum (tree-manip (first tree) init leaf first rest accum) (tree-manip (rest tree) init leaf first rest accum))))) Given : (define test-tree (list 1 (list 2 (list 3 (list 4) 5) 6) 7)) You can write expressions using tree-manip on test-tree will subsume many of the specific function we’ve just written, e.g.,: • Flatten a tree • Deep-reverse a tree • Sum up the values of the leaves of the tree • Take the product of the even-valued leaves of the tree • Create a new tree, which keeps the odd-valued leaves of the original tree within the same tree structure, but completely removes even-valued leaves. 6.001 SICP
And now for something completely different…. define a new pair abstraction! The names and procedures cons, car and cdr just fell into a black hole—I can’t use them to define adjoin, first, and rest! But I can define my-cons, a HOP that does: (my-cons 1 2) procedure ((my-cons 1 2) #t) 1 ((my-cons 1 2) #f) 2 (define my-cons (lambda (a b) (lambda (p) (if p a b)))) (define adjoin mycons) (define (first p) (p #t)) (define (rest p) (p #f))) 6.001 SICP
Remember… • calendar… 6.001 SICP