ECS110 Lecture Notes for Wednesday, November 1, 1995

Professor Rogaway conducting

Lecture 14

Scribe of the day: Daniel Brown


Last time: Trees
Today:	-Tree Traversals
	-Disjoint Sets

Tree Traversals
For every node (in a binary tree) that has both pointer to each of its children filled, each node is visited every time. ex. A / \ B C The first traversal goes from A to B, the second from B to A, the third from A to C, the third from C to A. A is visited a total of three times. So for this example, A / \ B C / \ / D E F / G The order of nodes visited is as follows: ABDDDBEGGGEEBACFFFCCA if at nodes that have no children or that have only one child, the function checks to see whether both children are present. So the node is still visited three times. Note that every node (according to the resulting string) is visited three times. II. There are three main types of tree traversal: Preorder, Inorder, and Post-order Visit a node the Order of traversal nth time the function gets to the node Preorder n = 1 VLR Inorder n = 2 LVR Postorder n = 3 LRV Breadth-first level1, level2, ..., leveln III. With the given tree used in the example above, A / \ B C / \ / D E F \ G The Inorder traversal visits each node in the order DBGEAFC The Postorder traversal visits each node in the order DGEBFCA The Preorder traversal visits each node in the order ABDEGCF The Breadth-first traversal visits each node in the order ABCDEFG ex. + / \ * / / \ / \ 3 - x y / \ z 2.5 Here is another example tree. So there is no confusion, the root node is a plus sign. It's left child is the Multiplication operator, the right child a divide operator. The third level has nodes of 3, minus operator, x, and y in that order from left to right. And the fifth level has the operands z and 2.5, which are children of the minus operator. The Postorder traversal gives: 3 z 2.5 - * x y / + The Inorder traversal gives: 3 * z - 2.5 + x / y There is only one problem here. The Inorder result is grammatically incorrect. 3 and z will be multiplied first, because * operator has precedence over the - operator. But according to tree, this is not what the formula should do. So, IF AN OPERATOR NODE HAS A PARENT OPERATOR NODE OF HIGHER PRECEDENCE THAN IT, you need PARENTHESES around the node and its operands when put into regular notation. This is crucial when writing the implementation of the algorithm. Note: Postorder traversal puts a tree containing sequences of operations into Postfix notation, which was discussed in earlier lectures. Inorder traversal puts the same sequence into its Infix notation. In fact, postfix and infix notations should convert easily into the tree formations as well. Implementations for Tree Traversal InorderTraverse(r) if (r==0) return; if (r <- left) InorderTraverse (r -> left); visit (r); if (r <- right) InorderTraverse (r -> right); PreorderTraverse(r) if (r==0) return; visit (r); if (r <- left) PreorderTraverse (r -> left); if (r <- right) PreorderTraverse (r -> right); PostorderTraverse(r) if (r == 0) return; if (r <- left) PostorderTraverse (r -> left); if (r <- right) PostorderTraverse (r -> right); visit (r); Here is the Iterative representation of Preorder Traversal. It uses iteration instead of recursion as above. Stack S; if (r == 0) return; s.push (r); while !s.empty () v <- s.pop (); visit (v); s.push (v -> right); s.push (v -> left); Here is a representation for a Bread-First Traversal, yet another type of Traversal, which goes traverses the tree level by level going down. BFTraversal (r) Queue q; if (r == 0) return; queue q; q.enqueue (r); while !q.empty () v <- q.dequeue; visit (v); q.enqueue (v -> left); q.enqueue (v -> right); For a tree, A / \ B C / \ / D E F / G The result of Bread Traversal is ABCDEFG
Disjoint Sets Disjoint sets are sets of elements in each set has no element contained by any other set ADT Disjoint Set //Data A set of element; a partitioning of those elements into disjoint subsets; A designated canonical (leader, identifying) element of each partition; (ie, a mapping from Elements S to Elements S; f:S -> S) //Operations Makeset (Element e) // Adds {e} into the set of elements Find (Element i) //returns canonical element of set containing i Union (Element s1, Element s2) //Unions the sets with canonical elements s1 and s2 and chooses a new canonical element for the new set