Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root,...

18
Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order: left, right, root General graph traversals (searches) Depth-first search Breadth-first search

Transcript of Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root,...

Page 1: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Traversals

A systematic method to visit all nodes in a tree Binary tree traversals:

Pre-order: root, left, right In-order: left, root, right Post-order: left, right, root

General graph traversals (searches) Depth-first search Breadth-first search

Page 2: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Inorder(tree t)

1.if t = nil

2. return

3.inorder(t.left)

4.visit(t) // e.g., print it

5.inorder(t.right)

Page 3: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Inorder (Infix)

1 2 3 5 6 7 8 9 10

(a BST will always work well with in-order traversal)

5

2

1 3

8

6 10

7 9

Page 4: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Pre-order (Prefix)

5 2 1 3 8 6 7 10 9

5

2

1 3

8

6 10

7 9

Page 5: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Post-order (Postfix)

1 3 2 7 6 9 10 8 5

5

2

1 3

8

6 10

7 9

Page 6: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Post-order

1 3 * + 6 10 -

+

*

1 3

-

6 10

Page 7: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Depth-first search (DFS)((חיפוש לעומק

DFS: Search one subtree completely before other Pre-order traversal is an example of a DFS:

Visit root, left subtree (all the way), visit right subtree We can do it in other order:

Visit root, right subtree, left subtree

Page 8: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Depth-first search (DFS)

DFS: visit all descendents before siblings

5

2

1 3

8

6 10

7 95 2 1 3 8 6 7 10 9

Page 9: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

DFS(tree t)1. q new stack

2. push(q, t)

3. while (not empty(q))

4. curr pop(q)

5. visit curr // e.g., print curr.datum

6. push(q, curr.left)

7. push(q, curr.right)

This version for binary trees only!

Page 10: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Breadth-first search (BFS))חיפוש לרוחב(

BFS: visit all siblings before their descendents

5

2

1 3

8

6 10

7 95 2 8 1 3 6 10 7 9

Page 11: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

BFS(tree t)1. q new queue

2. enqueue(q, t)

3. while (not empty(q))

4. curr dequeue(q)

5. visit curr // e.g., print curr.datum

6. enqueue(q, curr.left)

7. enqueue(q, curr.right)

This version for binary trees only!

Page 12: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

DFS(tree t)1. q new stack

2. push(q, t)

3. while (not empty(q))

4. curr pop(q)

5. visit curr // e.g., print curr.datum

6. push(q, curr.left)

7. push(q, curr.right)

This version for binary trees only!

Page 13: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

DFS(tree t)Void Graph::dfs (Vertex v)

{v.visted = true;

for each w adjacent to v

if (!w.visited)

dfs(w);

}

This version for any type of trees (graph)

Page 14: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Graphs vs. Trees Graphs don’t have any root Graphs can be directed or undirected

Trees only grow down (upside-down) (Why do trees grow upside down for

Computer scientists???) Graphs can have cycles, trees can’t

(why?)

Page 15: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

DFS Example on Graphsourcevertex

Page 16: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

AVL (Adelson, Velskii, Landis) Balance the tree (not our targil) The left and right branches of the tree can only

differ by one level Ensures log N depth (much better for

searching) Takes log N to add or delete

Page 17: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

5

3 8

1 4 10

5

3

1 4

AVL Tree

Not AVL Tree

Page 18: Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

AVL Trees Trees often need to be “rotated” when deleting

or inserting to keep AVL balance Nice link on this process Sample AVL code