1 / 17

Chapter 4. Trees: Part 1

Chapter 4. Trees: Part 1. Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals. Theory and Terminology. Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path. A Tree? No. 9. 10.

yetty
Télécharger la présentation

Chapter 4. Trees: Part 1

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4. Trees: Part 1 • Section 4.1 • Theory and Terminology • Preorder, Postorder and Levelorder Traversals

  2. Theory and Terminology • Definition: A tree is a connected graph with no cycles • Consequences: • Between any two vertices, there is exactly one unique path

  3. A Tree? No 9 10 11 12 5 6 7 8 2 3 4 1

  4. A Tree? Yes 9 10 11 12 5 6 7 8 2 3 4 1

  5. Theory and Terminology 1 root Edge 2 3 4 Parent of 5, 6 Child of 1, sibling of 2, 3 5 6 7 8 Leaf Height = 1, depth = 2 9 10 11 12 Descendent of 1, 2, 6 Vertex/node Leaf Height of the tree is the height of the root

  6. Preorder Traversal • Visit vertex, then visit child vertices (recursive definition) • How to implement preorder traversal • Depth-first search • Begin at root • Visit vertex on arrival • Implementation may be recursive, stack-based, or nested loop

  7. Preorder Traversal 1 root 1 root (1) (2) 2 3 4 2 3 4 5 6 7 8 5 6 7 8 1 root 1 root 2 3 4 (3) 2 3 4 (4) 5 6 7 8 5 6 7 8

  8. Preorder Traversal 1 root 1 root (5) (6) 2 3 4 2 3 4 5 6 7 8 5 6 7 8 1 root 1 root (7) 2 3 4 2 3 4 (8) 5 6 7 8 5 6 7 8

  9. Postorder Traversal • Visit child vertices, then visit vertex (recursive definition) • How to implement the postorder traversal • Depth-first search • Begin at root • Visit vertex on departure • Implementation may be recursive, stack-based, or nested loop

  10. Postorder Traversal 1 root 1 root (1) 2 3 4 (2) 2 3 4 5 6 7 8 5 6 7 8 1 root 1 root 2 3 4 (3) 2 3 4 (4) 5 6 7 8 5 6 7 8

  11. Postorder Traversal 1 root 1 root (5) (6) 2 3 4 2 3 4 5 6 7 8 5 6 7 8 1 root 1 root (7) 2 3 4 (8) 2 3 4 5 6 7 8 5 6 7 8

  12. Postorder Traversal 1 root 1 root (9) 2 3 4 (10) 2 3 4 5 6 7 8 5 6 7 8 1 root 1 root 2 3 4 (11) (12) 2 3 4 5 6 7 8 5 6 7 8

  13. Postorder Traversal 1 root 1 root (13) 2 3 4 (14) 2 3 4 5 6 7 8 5 6 7 8 1 root 1 root 2 3 4 2 3 4 (15) (16) 5 6 7 8 5 6 7 8

  14. Level-order Traversal • Visit all vertices in level, starting with level 0 and increasing • How to implement levelorder traversal • Breadth-first search • Begin at root • Visit vertex on departure • Only practical implementation is queue-based

  15. Levelorder Traversal 1 root 1 root (1) 2 3 4 (2) 2 3 4 5 6 7 8 5 6 7 8 1 root 1 root (3) 2 3 4 2 3 4 (4) 5 6 7 8 5 6 7 8

  16. Levelorder Traversal 1 root 1 root (5) (6) 2 3 4 2 3 4 5 6 7 8 5 6 7 8 1 root 1 root (7) 2 3 4 2 3 4 (8) 5 6 7 8 5 6 7 8

  17. Traversal Orderings • Preorder: depth-first search (recursion or stack-based), visit on arrival • Postorder: depth-first search (recursion or stack-based), visit on departure • Levelorder: breadth-first search (queue-based), visit on departure

More Related