Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Logical expressions


1. Logical expressions

2. Expression trees

3. Expression trees
Programs (and expressions) are (usually) written in text form.

To process the text, the text is converted to a tree from, usually with the root at the top.

It can be useful to visualize how an expression is represented in tree form.

4. Logical expression
Consider the following (fully parenthesized) logical expression.
( X & Y ) | ( ( ! X ) & ( ! Y ) )

Draw this expression as a tree diagram (with the root at the top). Do this in three ways (for binary operators).

5. Infix tree and expression
Expression tree for (X & Y) | ((! X) & (! Y))Here is the infix tree and expression for this logical expression.

Note: Unary operators (with only one operand) such as minus "-" are usually written before the operand.

6. Postfix tree and expression
Expression tree for (X & Y) | ((! X) & (! Y))Here is the postfix tree and expression for this logical expression.

7. Prefix tree and expression
Expression tree for (X & Y) | ((! X) & (! Y))Here is the prefix tree and expression for this logical expression.

8. Observations
Note the following about all the trees and expressions.

9. Expression questions
Here are some questions about expression trees involving arithmetic operators.

Consider an arithmetic expression consisting of 3 (different) arithmetic operators and 4 (different) operands (e.g., integer literals or variables representing integer values). For the examples use here, the following are used. Note: In general, there may be operators and operands that repeat, but for clarity here, the operators and operands used are different.

Note: The operators and operands used here may change from time to time but the concepts remain the same.

10. Ways to parenthesize
Then there are 5 different ways in which to write a well formed arithmetic expression such that in the infix representation of the arithmetic expression the following hold. Note: The ways are given numbers 1 to 5 but those numbers have no relevance other than identifying each way .

11. Catalan numbers
The number of ways to parenthesize related to Catalan numbers.

Expressions using parentheses: (math and computer programs)
((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

Expression trees: (computer science processing)

Expression treesHere is the first few terms in the sequence of Catalan numbers.
   1 1    2 2    3 5    4 14    5 2    6 132    7 429    8 1430

They are named after Eugène Catalan (1814-1894), Here are the 5 ways of parenthesizing an expression of 3 binary (arithmetic) operators and 4 (integer) operands. Let us look at an expression tree for each and the ways in which each expression can be written in prefix, infix (as above), and postfix notation.

12. Orderings
Starting at the root (top) node, there are three orderings of concern. Note: Each branch is either a leaf (operand) or a node that can be considered a sub-tree or tree where that node is the root (of the sub-tree).

13. Way 1
Expression tree questionPrefix ordering:

( / c ( - b ( + m k ) ) )

Infix ordering:

c / ( b - ( m + k ) )

Postfix ordering:

c b m k + - /

14. Way 2
Expression tree questionPrefix ordering:

( / c ( + ( - b m ) k ) )

Infix ordering:

c / ( ( b - m ) + k )

Postfix ordering:

c b m - k + /

15. Way 3
Expression tree questionPrefix ordering:

( - ( / c b ) ( + m k ) )

Infix ordering:

( c / b ) - ( m + k )

Postfix ordering:

c b / m k + -

16. Way 4
Expression tree questionPrefix ordering:

( + ( / c ( - b m ) ) k )

Infix ordering:

( c / ( b - m ) ) + k

Postfix ordering:

c b m - / k +

17. Way 5
Expression tree questionPrefix ordering:

( + ( - ( / c b ) m ) k )

Infix ordering:

( ( c / b ) - m ) + k

Postfix ordering:

c b / m - k +

18. End of page

19. Multiple choice questions for this page