traversals
Download
Skip this Video
Download Presentation
Traversals

Loading in 2 Seconds...

play fullscreen
1 / 18

Traversals - PowerPoint PPT Presentation


  • 134 Views
  • Uploaded on

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. Inorder(tree t). if t = nil

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Traversals' - rudolf


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
traversals
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
inorder tree t
Inorder(tree t)
  • if t = nil
  • return
  • inorder(t.left)
  • visit(t) // e.g., print it
  • inorder(t.right)
inorder infix

5

2

1

3

8

6

10

7

9

Inorder (Infix)

1 2 3 5 6 7 8 9 10

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

pre order prefix

5

2

1

3

8

6

10

7

9

Pre-order (Prefix)

5 2 1 3 8 6 7 10 9

post order postfix

5

2

1

3

8

6

10

7

9

Post-order (Postfix)

1 3 2 7 6 9 10 8 5

post order

+

*

1

3

-

6

10

Post-order

1 3 * + 6 10 -

depth first search dfs
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
depth first search dfs1

5

2

1

3

8

6

10

7

9

Depth-first search (DFS)

DFS: visit all descendents before siblings

5 2 1 3 8 6 7 10 9

dfs tree t
DFS(tree t)
  • q  new stack
  • push(q, t)
  • while (not empty(q))
  • curr  pop(q)
  • visit curr // e.g., print curr.datum
  • push(q, curr.left)
  • push(q, curr.right)

This version for binary trees only!

breadth first search bfs

5

2

1

3

8

6

10

7

9

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

BFS: visit all siblings before their descendents

5 2 8 1 3 6 10 7 9

bfs tree t
BFS(tree t)
  • q  new queue
  • enqueue(q, t)
  • while (not empty(q))
  • curr  dequeue(q)
  • visit curr // e.g., print curr.datum
  • enqueue(q, curr.left)
  • enqueue(q, curr.right)

This version for binary trees only!

d fs tree t
DFS(tree t)
  • q  new stack
  • push(q, t)
  • while (not empty(q))
  • curr  pop(q)
  • visit curr // e.g., print curr.datum
  • push(q, curr.left)
  • push(q, curr.right)

This version for binary trees only!

dfs tree t1
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)

graphs vs trees
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?)
avl adelson velskii landis
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
slide17

5

3

8

4

10

1

5

Not AVL Tree

3

4

1

AVL Tree

avl trees
AVL Trees
  • Trees often need to be “rotated” when deleting or inserting to keep AVL balance
  • Nice link on this process
  • Sample AVL code
ad