1 / 34

Agenda

Agenda. Lecture Content: Tree Traversals Decision Trees Game Trees Quiz. Tree Traversals. Tree Traversals. Ordered rooted trees are often used to store information. N eed procedures for visiting each vertex of an ordered rooted trees to access data.

jmaher
Download Presentation

Agenda

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. Agenda • Lecture Content: • Tree Traversals • Decision Trees • Game Trees • Quiz

  2. Tree Traversals

  3. Tree Traversals • Ordered rooted trees are often used to store information. • Need procedures for visiting each vertex of an ordered rooted trees to access data. • Describe universal address system for tree • Describe several important algorithms for visiting all the vertices of an ordered rooted tree.

  4. Universal Address System • Procedures for traversing all vertices of an ordered rooted tree rely on the orderings of children • The children of an internal vertex are shown from left to right • Label all vertices recursively:

  5. Example: Universal Address System of an Ordered Rooted Tree Lexicographic ordering:

  6. Traversal Algorithms • Procedures for systematically visiting every vertex of an ordered rooted tree. • Preorder traversal • Inorder traversal • Postorder traversal Implemented recursively, based on some ordering

  7. Preorder Traversal Definition • Let Tbe an ordered rooted tree with root r. If T consists only ofr, then ris the preorder traversal ofT. • Otherwise, suppose that T1, T2, …, Tn are the subtrees at r from left to right in T. The preorder traversalbegins by visiting r. it continues by traversing T1 in preorder, then T2 in preorder, and so on, until Tn is traversed in preorder. • Preorder means root first, then visit subtrees left to right

  8. Example: Tree T

  9. Example: Preorder Traversal of T

  10. Recursive Algorithm: Preorder Traversal

  11. Inorder Traversal Definition • Let Tbe an ordered rooted tree with root r. If T consists only ofr, then ris the inorder traversal ofT. • Otherwise, suppose that T1, T2, …, Tn are the subtrees atr from left to right in T. The inorder traversal begins by visiting T1in inorder, then visiting r. It continues by traversing T2 in inorder, then T3 in inorder, …, and finally Tn is in inorder. Inorder means root second : - Traverse left subtree, root, then traverse right subtree

  12. Example: Inorder Traversal of T

  13. Recursive Algorithm: Inorder Traversal

  14. Postorder Traversal Definition • Let Tbe an ordered rooted tree with root r. If T consists only ofr, then ris the postorder traversal of T. • Otherwise, suppose that T1, T2, …, Tn are the subtrees atr from left to right. The postorder traversal begins by visiting T1in postorder, then T2 in postorder, …, then Tnis in postorder, and ends by visiting r. Postorder means root last : - Traverse left subtree, traverse right subtree, and lastly visit root

  15. Example: Postorder Traversal of T

  16. Recursive Algorithm: Postorder Traversal

  17. Exercise • Preorder, Inorder, Postorder ?

  18. Other Easy Ways • Solve the traversal visually (not using computer program) • Draw a curve around the ordered rooted tree starting at the root, moving along the edges.

  19. Other Easy Ways • Preorder: listing each vertex the first time this curves passes it. • Inorder: listing a leaf the first time the curve passes it and listing each internal vertex the second time the curve passes it. • Postorder: listing a vertex the last time it is passed on the way back up to its parent

  20. Other Easy Ways • Preorder: abdheijcfgk • Inorder: hdbiejafckg • Postorder: hdijebfkgca

  21. Representing Expression Inorder traversal produces the original expression

  22. Preorder Traversal  Prefix Form (Polish Notation)

  23. Postorder Traversal  Postfix Form (Reverse Polish Notation)

  24. Decision Trees

  25. Decision Trees • Rooted trees can be used to model problems in which a series of decisions leads to a solution. • A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree.

  26. Example:Algorithm for choosing a restaurant

  27. Example: Five-Coins Puzzle • Suppose there are 4 coins, all with the same weight, and a counterfeit coin that is either heavier or lighter than the others. • How many weightings are necessary using a balance scale to determine which of the five coins is the counterfeit one?

  28. Algorithm • There are three possibilities for each weighting on a balance scale: • The two pans can have equal weight • The first pan can be heavier, or • the second pan can be heavier.  3-ary tree

  29. Decision Three for Five-Coins Puzzle Worst-Case scenario for number of weighing = height of decision tree

  30. Minimum Time for Sorting • Order the elements of three distinct element a1, a2, a3 • The worst-case time to sort = the height of a decision tree that solves the sorting problem

  31. Minimum Time for Sorting • Sorting 3-items: 3! = 6 outcomes • Sorting 4-items: 4! = 24 outcomes

  32. Game Trees

  33. Game Trees • Trees can be used to analyze certain types of games such as tic-tac-toe, chess, etc, in which players alternate moves. • Used in the development of computer programs for game-playing strategies.

  34. Example

More Related