Lesson 2 Flashcards
What is parse tree?
Any compiler or interpreter builds a parse tree out of what expressions you type to do the compilation or interpretation
What are the uses of trees?
- To represent a hierarchy
- Binary search tree
- Parse tree
What is the difference between nodes and trees?
Node and trees are synonyms
What is the runtime of doing work in a tree?
Θ(N) where N is the size of the tree. If we have to look at every item.
What is the base case in this code?
(define (treemap fn tree)
(make-tree (fn (datum tree))
(map (lambda (child) (treemap fn child))
(children tree) )))
Base case is inside the map. If this is an empty list, return the empty list.
Vertical base case: leaf of the node because children of tree is empty
Horizontal base case: when we run out of siblings in the children of the node
What is a tree data stucture?
Abstract data type
Runtime of binary search tree
Θ(lg N)
When to use mutual recursion?
Use mutual recursion when dealing with 2 dimensional data structure. Use trick of using map to handle one of the dimensions.
(define (deep-map fn lol)
(if (list? lol)
(map (lambda (element) (deep-map fn element)) lol)
(fn lol)))
lol can have arbitrary depths.
What does this code do?
if it is a leaf node, do something about the datum. if it is a branch node, do something about the list of children.
List of lists can be represented as a tree. True or false?
eg ((Matt, Lee), (John, Lennon), (Alan, Turing))
True. Lol is another 2 dimensional data structure which can be represented as a tree.
what is a deep list?
A deep list is a list of lists where the leaf nodes are datum with no children and the branch nodes have children but no datum.
how is binary search tree special trees?
They have 2 children. Trees can have n children.
DFS
(define (depth-first-search tree)
(print (datum tree))
(for-each depth-first-search (children tree)))
BFS
(define (breadth-first-search tree)
(bfs-iter (list tree)))
(define (bfs iter queue)
(if (null? queue)
‘done
(let ((task (car queue)))
(print (datum task))
(bfs-iter (append (cdr queue) (children task))))))
what does list tree do?
it makes the tree into a queue. Queue is just a list.
BFS application
searching for answers, we want to find the quickest way to leaf of the node bc then you win the chess game
eg: chess game, we want to find solution. each node make one decision, each possible move will be a child node. we run BFS in a fixed amount of time because there are too many solutions that it would take forever.
minimal possible path to the leaf.