


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Level-Order Traversal. Visit top-to-bottom, left-to-right (like reading in English): DBFACEG
Depth-First Traversals. Traverse deep nodes ( A , C , E , G ) before shallow ones ( D , B , F ). Note: “Traversing” a node is different than “visiting” a node. 3 types: Preorder , Inorder , Postorder.
3
Preorder Traversal. “Visit” a node, then traverse its children.
4
preOrder(BSTNode x) { if (x == null) return; print(x.key) preOrder(x.left) preOrder(x.right) }
Inorder Traversal. Traverse left child, “visit”, then traverse right child.
inOrder(BSTNode x) { if (x == null) return; inOrder(x.left) print(x.key) inOrder(x.right) }
Postorder Traversal. Traverse left, traverse right, then “visit.”
postOrder(BSTNode x) { if (x == null) return; postOrder(x.left) postOrder(x.right) print(x.key) }
First, trace a path around the graph from the top going counter-clockwise. Preorder. “Visit” when passing the left. Inorder. “Visit” when passing the bottom. Postorder. “Visit” when passing the right.
9
Tree. Consists of a set of nodes and a set of edges that connect those nodes. Invariant. There is exactly one path between any two nodes.
13
Graph. Consists of a set of nodes and a set of zero or more edges. Each edge connects any two nodes. Not all nodes need to be connected.
Simple Graph. A graph with no self-loops and no parallel edges. Unless otherwise stated, all graphs in this course are simple graphs.
Self-loop
Parallel
1
2
3
4
5
6
7 8
0 s
t
1
2
3
4
5
6
7 8
0 s
0 F - 1 F - 2 F - 3 F - 4 F - 5 F - 6 F - 7 F - 8 F - (^) Order of dfs returns:
Start by calling dfs(0).