CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A...
Transcript of CS24 Week 8 Lecture 1 - UCSB · Parent / Child • A parent is the predecessor of a node • A...
![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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/33.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/35.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/36.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/37.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/38.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/39.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/40.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/41.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/42.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/43.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/44.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/45.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/46.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/47.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/48.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/49.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/50.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/51.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/52.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/53.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/54.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/55.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/56.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/57.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/58.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/59.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/60.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/61.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/62.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/63.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/64.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/65.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/66.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/67.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/68.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/69.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/70.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/71.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/72.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/73.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/74.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/75.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/76.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/77.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/78.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/79.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/80.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/81.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/82.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/83.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/84.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/85.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/86.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/87.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/88.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/89.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/90.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/91.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/92.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/93.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/94.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050407/5f84bc6b23e3dc7fa90a497d/html5/thumbnails/95.jpg)
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