Skip to main content

Expression Trees – Infix to Postfix

· One min read
Strider

Hi, today I'm going to do something with expression trees. An expression tree, is a binary search tree, with which one can represent arithmetic expressions. Here each node is either an operator or a number.

An expression tree is often used to change infix operations, i.e. the way we compute in Postfix. Some computers work with Postfix, e.g. the HP 15C. With expression trees, the parentheses are omitted.

Suppose we want to represent this calculation: (23)+4(2 * 3) + 4 , as a tree. The tree then looks like this.

dia.png

A tree, you can traverse with Inorder, Preorder and with Postorder.

Inorder: (23)+4(2 * 3) + 4 = Infix
Preorder: +2 3 4+ * 2\ 3\ 4 = Prefix
Postorder: 2 34 +2\ 3 * 4\ + = Postfix

There is really nothing more to say. I hope you were able to understand Expressiontrees 😄.