Lecture 11 ADT Trees
- Questions
- Why doesn’t increment and double do infinite recursion if there is no base case? 11:40am
- Trees
- Recursive description
- A tree has a root label and a list of branches
- Each branch is a tree
- Tree with zero branches is a leaf (no lines going downward)
- Relative description (family trees)
- Each location in a tree is called a node (a circle)
- Each node has a label that can be any value
- One node can be the parent/child of another
- The top node is the root node
- Path
- series of node and edges connecting the tree
- Implementing the Tree Abstraction
- a tree has a root label and a list of branches
- Each branch is a tree
- Branches must be trees
- Use tree constructor when creating branches
- Functions
- double function
- applying a function to all the values in the tree
- Tree Processing
- Creating Trees
- Putting a recursive function into a list comprehension
- tree Processing Uses Recursion
- List of leaf labels for each branch
- still gives back a list of leaves
- Tree Representation
- can use print_tree
- draw() ← at code.cs61a.org
- Example: Counting Paths
- Memoization
- Recursive Computation of the Fibonacci Sequence
- A lot of the recursion is repeated
- We can store recursion values to be used later
- Cache → dictionary that stores our values
- keys are the function calls
- store results in dictionary
3.