L5 1 2 Apr - Tree Part 1
L5 1 2 Apr - Tree Part 1
Stacks
Trees
What is a graph?
Nodes
Edges
- Directed
- Undirected
What is a graph?
Nodes
Edges
- Directed
- Undirected
Cycles
What about a Tree?
A tree is defined as a graph
without any undirected cycles (i.e.,
cycles that would come about if
we replaced directed edges with
undirected edges) nor
any unconnected parts.
Trees with Special Cases?
A tree with no nodes is a valid tree called
the "null" (or "empty") tree
2. A B C E
3. A B D E
4. B C E
Answer A: This graph has no undirected loops (no edges at all), so it's
a valid tree
1. A B C D E
B: Even though this graph is visualized in a counterintuitive
way, it has no undirected loops, so it's a valid tree
2. A B C E
C: This graph has no undirected loops, so it's a valid tree
2. A B C E
3. A B D E
4. B C E
Answer A: We explicitly defined the empty graph as a valid rooted
binary tree
In-order - (L V R)
Post-order - (L R V )
Level-order - In Levels
Traversing A Rooted Binary Tree?
Pre-order - (V L R)
In-order - (L V R)
Post-order - (L R V )
Level-order - In Levels
Pre-Order (V L R)
In-Order (L V R)
Post-Order (L R V)
Think 4 mins – (NO TALKING)
In what order will the nodes of the following tree be visited in a pre-order traversal?
Take a vote
1. 7 3 4 1 8 5 9 6 2 0
2. 0 1 2 3 4 5 6 7 8 9
3. 0 1 3 7 4 2 5 8 6 9
4. 7 3 1 4 0 5 8 2 6 9
Answer
1. 7 3 4 1 8 5 9 6 2 0
2. 0 1 2 3 4 5 6 7 8 9
3. 0 1 3 7 4 2 5 8 6 9
4. 7 3 1 4 0 5 8 2 6 9
Think 4 mins – (NO TALKING)
Label the correct orders
A. 7 3 4 1 8 5 9 6 2 0
B. 0 1 2 3 4 5 6 7 8 9
C. 0 1 3 7 4 2 5 8 6 9
D. 7 3 1 4 0 5 8 2 6 9
Take a vote
A. 7 3 4 1 8 5 9 6 2 0
B. 0 1 2 3 4 5 6 7 8 9
C. 0 1 3 7 4 2 5 8 6 9
D. 7 3 1 4 0 5 8 2 6 9
B. 0 1 2 3 4 5 6 7 8 9
C. 0 1 3 7 4 2 5 8 6 9
D. 7 3 1 4 0 5 8 2 6 9
1. O(1)
2. O(n)
3. O(log n)
4. O(n log n)
Take a vote
What is the worst-case time complexity of performing a pre-order, in-
order, post-order, or level-order traversal on a rooted binary tree with n nodes?
(All four traversals have the same worst-case time complexity)
1. O(1)
4. O(n log n)
Binary Search Trees
What is a A Binary Search Tree?
2. A B D
3. A
4. D
5. A D
Answer
1. A B
2. A B D
3. A
A: This is a valid BST
1. O(1)
2. O(n)
3. O(log n)
4. O(n log n)
Take a vote
1. O(1)
4. O(n log n)
Find Algorithm for BST-
1. Start at the root.
5.If you ever try to traverse left/right but no such child exists, fail!
Find Algorithm for BST-
1. Start at the root.
5.If you ever try to traverse left/right but no such child exists, fail!
Think 5 mins – (NO TALKING)
Write a function sortedPrint(Node* node) that recursively prints the labels of all
nodes in the subtree rooted at node in ascending sorted order. Print a single
label per line.
You can assume that the Binary Search Tree structure is valid.
HINT: Does one of the rooted binary tree traversal algorithms (pre-order, in-
order, and post-order) seem useful in this context?
Sample Output:
Apple
Cherry
Imbe
Lemon
Mango
Nectarine
Pair 3 mins – (TALKING)
Write a function sortedPrint(Node* node) that recursively prints the labels of all
nodes in the subtree rooted at node in ascending sorted order. Print a single
label per line.
You can assume that the Binary Search Tree structure is valid.
HINT: Does one of the rooted binary tree traversal algorithms (pre-order, in-
order, and post-order) seem useful in this context?
Sample Output:
Apple
Cherry
Imbe
Lemon
Mango
Nectarine
Share 3 mins – (TALKING)
Write a function sortedPrint(Node* node) that recursively prints the labels of all
nodes in the subtree rooted at node in ascending sorted order. Print a single
label per line.
You can assume that the Binary Search Tree structure is valid.
HINT: Does one of the rooted binary tree traversal algorithms (pre-order, in-
order, and post-order) seem useful in this context?
Sample Output:
Apple
Cherry
Imbe
Lemon
Mango
Nectarine
Solution
Sample Output:
Apple
Cherry
Imbe
Lemon
Mango
Nectarine
Thank you.