340 likes | 349 Views
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.
E N D
Agenda • Lecture Content: • Tree Traversals • Decision Trees • Game Trees • Quiz
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.
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:
Example: Universal Address System of an Ordered Rooted Tree Lexicographic ordering:
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
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
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
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
Exercise • Preorder, Inorder, Postorder ?
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.
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
Other Easy Ways • Preorder: abdheijcfgk • Inorder: hdbiejafckg • Postorder: hdijebfkgca
Representing Expression Inorder traversal produces the original expression
Postorder Traversal Postfix Form (Reverse Polish Notation)
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.
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?
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
Decision Three for Five-Coins Puzzle Worst-Case scenario for number of weighing = height of decision tree
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
Minimum Time for Sorting • Sorting 3-items: 3! = 6 outcomes • Sorting 4-items: 4! = 24 outcomes
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.