Tree Traversal Orderings, Study notes of English

It is about 150 miles long and 70 miles wide, comprising 8,722 square miles. The Delaware River is the largest river in the State, and defines the State's ...

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

shyrman
shyrman 🇺🇸

4.2

(6)

239 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tree Traversal Orderings
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
A C
B
D
E
F
G
Depth-First Traversals
Preorder Traversal.
“Visit” a node, then traverse its children.
4
A C
B
D
E
F
G
preOrder(BSTNode x) {
if (x == null) return;
print(x.key)
preOrder(x.left)
preOrder(x.right)
}
D B A C F E G
Depth-First Traversals
Inorder Traversal.
Traverse left child, “visit”, then traverse
right child.
5
A C
B
D
E
F
G
inOrder(BSTNode x) {
if (x == null) return;
inOrder(x.left)
print(x.key)
inOrder(x.right)
}
A B C D E F G
Depth-First Traversals
Postorder Traversal.
Traverse left, traverse right, then “visit.
6
A C
B
D
E
F
G
postOrder(BSTNode x) {
if (x == null) return;
postOrder(x.left)
postOrder(x.right)
print(x.key)
}
Q
pf3
pf4

Partial preview of the text

Download Tree Traversal Orderings and more Study notes English in PDF only on Docsity!

Tree Traversal Orderings

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

A C

B

D

E

F

G

Depth-First Traversals

Preorder Traversal. “Visit” a node, then traverse its children.

4

A C

B

D

E

F

G

preOrder(BSTNode x) { if (x == null) return; print(x.key) preOrder(x.left) preOrder(x.right) }

D B A C F E G

Depth-First Traversals

Inorder Traversal. Traverse left child, “visit”, then traverse right child.

A C

B

D

E

F

G

inOrder(BSTNode x) { if (x == null) return; inOrder(x.left) print(x.key) inOrder(x.right) }

A B C D E F G

Depth-First Traversals

Postorder Traversal. Traverse left, traverse right, then “visit.”

A C

B

D

E

F

G

postOrder(BSTNode x) { if (x == null) return; postOrder(x.left) postOrder(x.right) print(x.key) }

Q

Depth-First Traversals: Visual Trick (for humans)

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

A C

B

D

E

F

G

A C B E G F D

Alternate Tree Definition

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 Definition

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 Definition

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

connected(s, t):

● Mark s.

● Does s == t? If so, return true.

● Otherwise, if connected(v, t) for any unmarked neighbor v of s, return true.

● Return false.

s-t Connectivity

1

2

3

4

5

6

7 8

0 s

t

DepthFirstPaths Demo

Goal: Find a path from s to every other reachable vertex, visiting each vertex at

most once. dfs(v) is as follows:

● Mark v.

● For each unmarked adjacent vertex w:

○ set edgeTo[w] = v.

○ dfs(w)

1

2

3

4

5

6

7 8

0 s

marked edgeTo

0 F - 1 F - 2 F - 3 F - 4 F - 5 F - 6 F - 7 F - 8 F - (^) Order of dfs returns:

Order of dfs calls: 0

Start by calling dfs(0).