Friday, March 30 Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.
Postorder traversal (recursive definition) If T consists only of r, then r is in the postorder traversal of T. Otherwise, let T 1, T 2, T n, be the subtrees at r from left to right. The preorder traversal begins by traversing T 1 in postorder, then T 2 in postorder, and so on, until T n has been traversed in postorder,, and ends by visiting r. That is, the root of a tree/subtree is visited after all the vertices of its subtrees are visited. Each subtree is traversed by traversing its subtrees from left to right, and then visiting its root.
Write the postorder traversal for this ordered tree.
Inorder traversal If T consists only of r, then r is in the inorder traversal of T. Otherwise, let T 1, T 2, T n, be the subtrees at r from left to right. The inorder traversal begins by traversing T 1 in inorder, then visiting r, then traversing T 2 in inorder, and so on, until T n has been traversed in inorder. That is, the root of a tree/subtree is visited after all the vertices of its left subtree are visited, but before the vertices of its other subtrees are visited. Each subtree is traversed by first traversing its left subtree, then visiting its root, then traversing its other subtrees from left to right.
Prefix notation, postfix notation Consider the algebraic expression x y/2 x 3 y When we write such an expression, we naturally include the bracketing symbols, such as parentheses, for purposes of clarity and to avoid the ambiguity that may result if we omit such symbols. Without such grouping symbols, the reader might not understand that we were trying to represent this operation: ( x y)/(2x 3y) Prefix (aka Polish) notation and postfix (aka reverse Polish) notation are two methods for representing an algebraic expression in a manner that does not require bracketing symbols, and which is unambiguous. Both prefix and postfix notation can be derived from the binary tree that represents the operation. Prefix notation is derived by preorder traversal; postfix notation is derived by postorder traversal.
EXAMPLE Draw the binary tree for z y representations. ( ) / ( 5 + (7z 3y) ) and use it to write the prefix and postfix
SPANNING TREES Definition Let G be a connected simple graph and let T be a subgraph of G. T is a spanning tree of G if T is a tree containing every vertex of G. Typically, a graph will have many possible spanning trees. One way to derive a spanning tree is to successively delete edges from G in a way that removes all cycles, but does not disconnect G.
EXAMPLE Find a spanning tree for this graph, by deleting edges until all cycles have been removed.
Depth-first search, breadth-first search Depth-first search and breadth-first search are two systematic methods for construction of a spanning tree. Both of these algorithms begin with the arbitrary selection of a vertex to serve as the root of the spanning tree. Depth first search After selecting a vertex to serve as the root, form a path starting at this root and successively adding vertices and edges, in such a way that each new edge of the path is incident with the last vertex in the path, and with another vertex not already in the path. If we are able to construct such a path that uses every vertex, then that path is the spanning tree. If the path does not use every vertex, then more vertices and edges need to be added. Move back to the next to the last vertex of the path, and, if possible, form a new path starting at this vertex, passing through vertices that were not already visited. If this cannot be done, move back another vertex in the path, and try again. Repeat this procedure, always backtracking from the last vertex visited, forming new paths that are as long as possible until no more edges can be added.
Referring to the graph in the previous example, use depth-first search to find a spanning tree rooted at a, then use depth-first search to find a spanning tree rooted at g.
Breadth-first search Again, begin by arbitrarily selecting a vertex to serve as the root. Next, add all edges incident to this root. These are the Level 1 vertices. Next, for each vertex added at Level 1, visited in order, add each edge incident to that vertex, so long as it does not produce a simple circuit. Now, arbitrarily order the children of each vertex added at Level 1; these are the vertices of Level 2. Visit, in order, each Level 2 vertex, adding every incident edge. Repeat the procedure as needed, expanding to new levels, until all vertices of G have been added to the tree.
Referring to the graph in the previous example, use breadth-first search to find a spanning tree rooted at a, then use breadth-first search to find a spanning tree rooted at d.