CS 46B - Lecture 25
Pre-class reading
Tree Traversal
- Visit all elements
- Different orderings are possible
- Inorder traversal
- Visit left subtree
- Visit root
- Visit right subtree
- In BST, yields nodes in sorted order
Preorder/Postorder
- Preorder
- Visit root
- For each child c: Visit c
- Postorder
- For each child c: Visit c
- Visit root
- Work for general tree (not just binary)
Lecture 25 Clicker Question 1
What are inorder, preorder, and postorder traversals of the tree
F
/ \
D R
\
E
- DEFR, FEDR, EDRF
- EDFR, FDER, EDRF
- DEFR, FDER, DERF
- Something else
Postorder Traversal of Expression Tree

Directory Operations
- Remove a directory and all its children

- Must operate on leaves first
- Postorder
Directory Operations
- Copy a directory and all its children

- Must work on root first
- Preorder
Lecture 25 Clicker Question 2
Suppose you have two directory trees and you want to find out whether they contain the same subdirectories, each of which contains the same files. Should you traverse using
- inorder
- preorder
- postorder
- preorder or postorder

Visitor Pattern
- What do you want to do when you visit a node
- Print it?
- Delete or copy files?
- Generic interface
public interface Visitor
{
void visit(Object data);
}
-
private static void preorder(Node n, Visitor v)
{
if (n == null) { return; }
v.visit(n.data);
for (Node c : n.children) { preorder(c, v); }
}
public void preorder(Visitor v) { preorder(root, v); }
Example: Count Short Names
public static void main(String[] args)
{
BinarySearchTree bst = . . .;
class ShortNameCounter implements Visitor
{
public int counter = 0;
public void visit(Object data)
{
if (data.toString().length() <= 5) { counter++; }
}
}
ShortNameCounter v = new ShortNameCounter();
bst.inorder(v);
System.out.println("Short names: " + v.counter);
}
Iteration
Depth-First Search

Breadth-First Search

Lecture 25 Clicker Question 3
Suppose you have a binary tree of height n > 1. Can its depth-first traversal ever be the same as its breadth-first traversal?
- No, never
- Yes, if it's not a binary search tree
- Yes, if it has n elements
- Yes, if the height is 2

Tree Iterators
Tree Iterators
class BreadthFirstIterator
{
private Queue<Node> q;
public BreadthFirstIterator(Node root)
{
q = new LinkedList<Node>();
if (root != null) { q.add(root); }
}
public boolean hasNext() { return q.size() > 0; }
public Object next()
{
Node n = q.remove();
for (Node c : n.children) { q.add(c); }
return n.data;
}
}
A Look Back on CS46B
- Interfaces (and a bit of inheritance)
- Input/Output
- Recursion
- Sorting and searching
- Big-Oh
- Data structures: lists, stacks, queues, sets, maps
- Linked lists
- Trees
What Really Matters
- Algorithms: recursion, pseudocode, big-oh
- Data structures: lists, stacks, queues, sets, maps
- Linked structures
Next Semester
- CS146: Algorithms and data structures
- Sorting (again)
- Trees, graphs
- More formal big-oh
- CS151: Object-oriented design
- Inheritance and interfaces (again)
- Design patterns
- Group project
I Wish This Slide Wasn't Here

- California Master Plan for Higher Education, 1960
- Three-tier system: UC, CSU, Community Colleges
- Top third of high school graduates entitled to CSU admission
- Tuition-free education with fees for dormitories, recreational facilities, etc.
- Reality
- Tuition now covers about 50% of the cost of running the CSU
- And you don't get as much for it as you used to
- Minimal lab assistants, graders, unpaid tutors, no mentors
- 80% of undergraduate CS SJSU classes taught by adjuncts
What Really Matters
- Learn to teach yourself
- Network and collaborate
- Write lots of code