95
CS24 Week 8 Lecture 1 Kyle Dewey Tuesday, August 12, 14

CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

CS24 Week 8 Lecture 1Kyle Dewey

Tuesday, August 12, 14

Page 2: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Overview

• Tree terminology

• Tree traversals

• Implementation (if time)

Tuesday, August 12, 14

Page 3: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Terminology

Tuesday, August 12, 14

Page 4: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Node• The most basic component of a tree - the

squares

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 5: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Edge• The connections between nodes - the

arrows

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 6: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Parent / Child• A parent is the predecessor of a node

• A child is the successor of a node

• Not all nodes have parents

• Not all nodes have children

7

3 12

(child of 7) (child of 7)

(parent of 3 and 12)

Tuesday, August 12, 14

Page 7: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Leaf / Terminal Node

• A node without any children

7

3 12

Tuesday, August 12, 14

Page 8: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Leaf / Terminal Node

• A node without any children

7

3 12

(leaf) (leaf)

Tuesday, August 12, 14

Page 9: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Internal Node

• A node with at least one child

7

3 12

Tuesday, August 12, 14

Page 10: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Internal Node

• A node with at least one child

7

3 12

(internal node)

Tuesday, August 12, 14

Page 11: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Root Node

• Node without any parent

• Often drawn as the topmost node

7

3 12

Tuesday, August 12, 14

Page 12: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Height and Depth

• Height: The number of edges on the longest path from a node to a leaf

• Depth: the number of edges between a node and the root node

Tuesday, August 12, 14

Page 13: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Height and Depth

http://stackoverflow.com/questions/2603692/what-is-the-difference-between-tree-depth-and-height

Tuesday, August 12, 14

Page 14: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Level• All the nodes of a tree which have the

same depth

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 15: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Level• All the nodes of a tree which have the

same depth

7

3 12

0 4 10 15

level 0

level 1

level 2Tuesday, August 12, 14

Page 16: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

k-ary Tree

• A tree where each node can have between 0 and k children

• What is k for a binary tree?

Tuesday, August 12, 14

Page 17: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

k-ary Tree

• A tree where each node can have between 0 and k children

• What is k for a binary tree? - 2

Tuesday, August 12, 14

Page 18: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Full k-ary Tree

• All nodes have either 0 or k children

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 19: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Complete k-ary Tree• Like a full k-ary tree, except the last level is

permitted to be missing nodes, but only on the right

7

3 12

0 4

Tuesday, August 12, 14

Page 20: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Complete k-ary Tree

7

3 12

0 4

ok

• Like a full k-ary tree, except the last level is permitted to be missing nodes, but only on the right

Tuesday, August 12, 14

Page 21: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Complete k-ary Tree

7

3 12

0 10

• Like a full k-ary tree, except the last level is permitted to be missing nodes, but only on the right

Tuesday, August 12, 14

Page 22: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Complete k-ary Tree

7

3 12

0 10

not ok

• Like a full k-ary tree, except the last level is permitted to be missing nodes, but only on the right

Tuesday, August 12, 14

Page 23: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Balanced Tree• For all nodes, the height of the left and

right subtrees differ by no more than one

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 24: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Balanced Tree• For all nodes, the height of the left and

right subtrees differ by no more than one

7

3 12

0 4 10 15

ok

Tuesday, August 12, 14

Page 25: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Balanced Tree• For all nodes, the height of the left and

right subtrees differ by no more than one

7

3 12

0 15

Tuesday, August 12, 14

Page 26: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Balanced Tree• For all nodes, the height of the left and

right subtrees differ by no more than one

7

3 12

0 15

ok

Tuesday, August 12, 14

Page 27: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Balanced Tree• For all nodes, the height of the left and

right subtrees differ by no more than one

7

12

10 15

Tuesday, August 12, 14

Page 28: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Balanced Tree• For all nodes, the height of the left and

right subtrees differ by no more than one

7

12

10 15

not ok

Tuesday, August 12, 14

Page 29: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Subtree

• Nearly synonymous with node

• We recursively defined the tree to be either a node with an element and two children, or an empty tree (NULL)

• Generally refers to some subcomponent of a larger tree, including recursive subcomponents

Tuesday, August 12, 14

Page 30: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Subtree Example

7

3 12

0 4 10 15

Can refer to 3 and

its children

Cannot just refer

to 3Tuesday, August 12, 14

Page 31: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Traversals

Tuesday, August 12, 14

Page 32: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Traversals

• For many tree-related problems, the order in which nodes are processed can have a huge impact

• Two basic kinds: breadth-first search and depth-first search

Tuesday, August 12, 14

Page 33: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Breath-First Search (BFS)

• Tree is traversed as if nodes were words on a page (top to bottom, left to right)

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 34: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Breath-First Search (BFS)

• Tree is traversed as if nodes were words on a page (top to bottom, left to right)

7

3 12

0 4 10 15

1

2 3

4 5 6 7

Tuesday, August 12, 14

Page 35: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS• Question: how might we implement BFS?

• Hint: you’ll need a data structure you’ve implemented before

7

3 12

0 4 10 15

1

2 3

4 5 6 7

Tuesday, August 12, 14

Page 36: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

• Idea: put nodes on a queue

• Visit nodes according to the queue order

• When we are done with a node, put its children onto the end of the queue

Tuesday, August 12, 14

Page 37: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: <<empty>>

Tuesday, August 12, 14

Page 38: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 7Put root on the queue first (this is the node, not just the

number)

Tuesday, August 12, 14

Page 39: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 7Now dequeue

Tuesday, August 12, 14

Page 40: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 7Now dequeue

1

Tuesday, August 12, 14

Page 41: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: <<empty>>Now dequeue

1

Tuesday, August 12, 14

Page 42: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: <<empty>>Now put on the child nodes

1

Tuesday, August 12, 14

Page 43: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 3, 12Now put on the child nodes

1

Tuesday, August 12, 14

Page 44: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 3, 12Repeat

1

Tuesday, August 12, 14

Page 45: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 3, 12

1

2

Tuesday, August 12, 14

Page 46: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 12

1

2

Tuesday, August 12, 14

Page 47: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 12, 0, 4

1

2

Tuesday, August 12, 14

Page 48: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 12, 0, 4

1

2

Tuesday, August 12, 14

Page 49: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 12, 0, 4

1

2 3

Tuesday, August 12, 14

Page 50: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 0, 4

1

2 3

Tuesday, August 12, 14

Page 51: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 0, 4, 10, 15

1

2 3

Tuesday, August 12, 14

Page 52: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 0, 4, 10, 15

1

2 3

Tuesday, August 12, 14

Page 53: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 0, 4, 10, 15

1

2 3

4

Tuesday, August 12, 14

Page 54: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 4, 10, 15

1

2 3

4

Tuesday, August 12, 14

Page 55: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 4, 10, 15

1

2 3

4 5

Tuesday, August 12, 14

Page 56: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 10, 15

1

2 3

4 5

Tuesday, August 12, 14

Page 57: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 10, 15

1

2 3

4 5 6

Tuesday, August 12, 14

Page 58: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 15

1

2 3

4 5 6

Tuesday, August 12, 14

Page 59: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: 15

1

2 3

4 5 6 7

Tuesday, August 12, 14

Page 60: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing BFS

7

3 12

0 4 10 15

root

Queue: <<empty>>

1

2 3

4 5 6 7

Tuesday, August 12, 14

Page 61: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Depth-First Search (DFS)

• Favor going down towards the left first

7

3 12

0 4 10 15

1

2

3 4

5

6 7

Tuesday, August 12, 14

Page 62: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS• Question: how might we implement DFS?

• Hint: you’ll need a data structure you’ve implemented before

7

3 12

0 4 10 15

1

2

3 4

5

6 7

Tuesday, August 12, 14

Page 63: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

• Idea: put nodes on a stack

• Visit nodes according to the stack order

• When we are done with a node, push its children onto the top of the stack

Tuesday, August 12, 14

Page 64: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: <<empty>>

Tuesday, August 12, 14

Page 65: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 7

Tuesday, August 12, 14

Page 66: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 7

Tuesday, August 12, 14

Page 67: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 7

1

Tuesday, August 12, 14

Page 68: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: <<empty>>

1

Tuesday, August 12, 14

Page 69: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 3, 12

1

Tuesday, August 12, 14

Page 70: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 3, 12

1

Tuesday, August 12, 14

Page 71: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 3, 12

1

2

Tuesday, August 12, 14

Page 72: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 12

1

2

Tuesday, August 12, 14

Page 73: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 0, 4, 12

1

2

Tuesday, August 12, 14

Page 74: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 0, 4, 12

1

2

Tuesday, August 12, 14

Page 75: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 0, 4, 12

1

2

3

Tuesday, August 12, 14

Page 76: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 4, 12

1

2

3

Tuesday, August 12, 14

Page 77: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 4, 12

1

2

3 4

Tuesday, August 12, 14

Page 78: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 12

1

2

3 4

Tuesday, August 12, 14

Page 79: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 12

1

2

3 4

5

Tuesday, August 12, 14

Page 80: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: <<empty>>

1

2

3 4

5

Tuesday, August 12, 14

Page 81: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 10, 15

1

2

3 4

5

Tuesday, August 12, 14

Page 82: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 10, 15

1

2

3 4

5

Tuesday, August 12, 14

Page 83: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 10, 15

1

2

3 4

5

6

Tuesday, August 12, 14

Page 84: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 15

1

2

3 4

5

6

Tuesday, August 12, 14

Page 85: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: 15

1

2

3 4

5

6 7

Tuesday, August 12, 14

Page 86: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Implementing DFS

7

3 12

0 4 10 15

root

Stack: <<empty>>

1

2

3 4

5

6 7

Tuesday, August 12, 14

Page 87: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

On Using Stacks

• We can cut out the explicit stack by using the call stack implicitly via recursion

void traverse(Node* current) { if (current != NULL) { traverse(current->getLeft()); traverse(current->getRight()); }}

Tuesday, August 12, 14

Page 88: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Specific Kinds of DFS Traversals

• Depending on when we process the current node, there are three general kinds of DFS traversals:

• Pre-order: process current first

• In-order: process current between left and right

• Post-order: process current after left and right

Tuesday, August 12, 14

Page 89: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Pre-Order Traversal

void traverse(Node* current) { if (current != NULL) { process(current); traverse(current->getLeft()); traverse(current->getRight()); }}

Tuesday, August 12, 14

Page 90: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

In-Order Traversal

void traverse(Node* current) { if (current != NULL) { traverse(current->getLeft()); process(current); traverse(current->getRight()); }}

Tuesday, August 12, 14

Page 91: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Post-Order Traversal

void traverse(Node* current) { if (current != NULL) { traverse(current->getLeft()); traverse(current->getRight()); process(current); }}

Tuesday, August 12, 14

Page 92: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Using Traversals• Say we want to print out the contents of a

binary search tree in sorted order

• What kind of traversal should we use?

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 93: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Using Traversals• Say we want to print out the contents of a

binary search tree in sorted order

• What kind of traversal should we use? - in-order

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 94: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Using Traversals• Say we want to delete a binary search tree

• Which traversal is best?

7

3 12

0 4 10 15

Tuesday, August 12, 14

Page 95: CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A child is the successor of a node • Not all nodes have parents • Not all nodes have

Using Traversals• Say we want to delete a binary search tree

• Which traversal is best? - post-order

7

3 12

0 4 10 15

Tuesday, August 12, 14