Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun flatten-children-at-depth (tree depth)
- (cond ((minusp depth) tree)
- ((zerop depth) (if (tree-leaf-p tree)
- tree
- (tree-make-tree (tree-label tree)
- (mapcan (lambda (child)
- (if (tree-leaf-p child)
- (list child)
- (copy-list (tree-children child))))
- (tree-children tree)))))
- (t (if (tree-leaf-p tree)
- tree
- (tree-make-tree (tree-label tree)
- (mapcar (lambda (child)
- (flatten-children-at-depth child (- depth 1)))
- (tree-children tree)))))))
- (defun flatten-deepest-children (tree)
- (flatten-children-at-depth tree (- (depth tree) 3)))
- (flatten-deepest-children (tree-make-tree nil (list (tree-make-leaf 'a) (tree-make-tree nil (list (tree-make-leaf 'b))) (tree-make-leaf 'c))))
- ;; -> (a b c)
- (flatten-deepest-children
- ;; (a (b (c) d) e ((f g) h i) j)
- (tree-make-tree nil (list (tree-make-leaf 'a)
- (tree-make-tree nil (list (tree-make-leaf 'b)
- (tree-make-tree nil (list (tree-make-leaf 'c)))
- (tree-make-leaf 'd)))
- (tree-make-leaf 'e)
- (tree-make-tree nil (list (tree-make-tree nil (list (tree-make-leaf 'f)
- (tree-make-leaf 'g)))
- (tree-make-leaf 'h)
- (tree-make-leaf 'i)))
- (tree-make-leaf 'j))))
- ;; --> (a (b c d) e (f g h i) j)
- (flatten-deepest-children
- ;; (a (b (c) d) e ((f (e a d) g) h i) j)
- (tree-make-tree nil (list (tree-make-leaf 'a)
- (tree-make-tree nil (list (tree-make-leaf 'b)
- (tree-make-tree nil (list (tree-make-leaf 'c)))
- (tree-make-leaf 'd)))
- (tree-make-leaf 'e)
- (tree-make-tree nil (list (tree-make-tree nil (list (tree-make-leaf 'f)
- (tree-make-tree nil (list (tree-make-leaf 'e)
- (tree-make-leaf 'a)
- (tree-make-leaf 'd)))
- (tree-make-leaf 'g)))
- (tree-make-leaf 'h)
- (tree-make-leaf 'i)))
- (tree-make-leaf 'j))))
- ;; --> (a (b (c) d) e ((f e a d g) h i) j)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement