80 likes | 278 Views
From a parse tree to a syntax tree: We have looked at a grammar for arithmetic expressions, and saw how to construct the parse trees for specific expressions. A syntax tree for an expression can be constructed from the parse tree, and is an alternative description of the expression.
E N D
From a parse tree to a syntax tree: We have looked at a grammar for arithmetic expressions, and saw how to construct the parse trees for specific expressions. A syntax tree for an expression can be constructed from the parse tree, and is an alternative description of the expression. The nodes in the parse tree containing non-terminal symbols, and the nodes containing parentheses are “squeezed out” of the tree. In a syntax tree the operands appear in the leaf nodes, and the operators appear in the non-leaf nodes.
Example: (a + b) * c <E> <T> <T> * <F> ( <E> ) c <E> + <T> a b
The syntax tree for (a + b) * c * + c a b A pre-order traversal yields: * + a b c A post-order traversal yields: a b + c *
Example: a + b * c <E> <E> + <T> <T> <T> * <F> <F> <F> c a b
The syntax tree for a + b * c + * a b c A pre-order traversal yields: + a * b c A post-order traversal yields: a b c * +
Assume the data values on the nodes of a tree can be ordered. A binary search tree T is a binary tree that is either empty, or 1. Each data item in the left subtree of T is less than or equal to the data item on the root of T 2. Each data item in the right subtree of T is greater than or equal to the data item on the root of T. 3. The left subtree and right subtree of T are also binary search trees. An in-order traversal of a binary search tree displays the data values on the nodes in increasing order
M R D T C J S V U W Z An in-order traversal: C D J M S T U V W Z