Section 5.5 Binary Tree A binary tree is a rooted tree in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a binary tree, each vertex may have 0, 1, or 2 children. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents of L, and the edges connecting these vertices. Right subtree The right subtree of a vertex V on a binary tree is the graph formed by the right child R of V, the descendents of R, and the edges connecting these vertices. Expression Tree We will represent an arithmetic expression as a binary tree with the operations as internal vertices. In this representation we let the root denote the final operation done in the expression, and we place the left operand as its left child and the right operand as its right child. If necessary, these process is repeated on these operands. The binary tree created by this process is called an expression tree. Visiting a vertex Processing the data at a vertex is usually called a visiting a vertex (labeled). Traversal A search procedure that visits each vertex of a graph exactly once is called a traversal of the graph. Preorder traversal We will consider a traversal of a binary tree characterized by visiting a parent before its children and a left children before a right child. Listing the vertices in the order they are visited is called a preorder listing. Polish Notation or prefix form The operation sign precedes the operands. When a preorder traversal is performed on an expression tree, the resulting listing of operations and operands is called the prefix form or Polish notation for the expression. Polish Notation Evaluation An expression in Polish notation is evaluated according to the following rule: Scan from left to right until coming to an operation sign, say T, that is followed by two successive numbers, say a and b. Evaluate T a b as a T b, and replace T a b by this value in the expression. Repeat this process until the entire expression is evaluated. Reverse Polish Notation or postfix form The operation sign follows the operands. By using a traversal called postorder, we can obtain the reverse Polish notation for an expression. Reverse Polish Notation Evaluation An expression in Reverse Polish notation is evaluated according to the following rule: Scan from left to right until coming to an operation sign, we look for two numbers say a and b immediately followed by an operation sign T. Evaluate a b T as a T b, and replace a b T by this value in the expression. Repeat this process until the entire expression is evaluated.
Postorder Traversal The postorder traversal is characterized by visiting children before the parent and a left child before a right child. Inorder Traversal The inorder traversal is characterized by visiting a left child before the parent and a right child after the parent. Problem 1. Construct an expression tree for each expression. a) (4 + 2) (6 8) b) (((6 3) 2) + 7)/((5 1) 4 + 8) Problem 2. In the following binary trees, a) Find the right subtree of vertex E, and the left subtree of vertex D. b) Give the preorder listing of vertices.
c) Give the postorder listing of vertices. d) Give the inorder listing of vertices. Problem 3. Find the polish notation for the expressions in a) Problem 1 (a) b) Problem 1 (b)
Problem 4. Find the reverse Polish notation for the expression in a) Problem 1 (a) b) Problem 1 (b) Problem 5. Evaluate the Polish notation expressions. a) + + 4 3 6 2 8 b) + + 3 4 1 2 3 / 4 2
Problem 6. Evaluate the reverse Polish notation expressions. a) 5 6 4 2 2 / + b) 3 4 + 1 2 4 2 / 3 + Problem 7. Construct an expression tree for the Polish notation expression + B D F + A C E.. Problem 8. Construct an expression tree for the Reverse Polish notation expression ED A+BC F +. Homework: Read Section 5.5, do 1-53 (odd).