Infix and Postfix Expressions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Infix and Postfix Expressions

• Infix Expression
• an <operator> is preceded and succeeded by an <operand>
• e.g. A+B
• Postfix Expression
• an <operator> is succeeded by both the <operand>.
• e.g. AB+
• One of the applications of Stack is in the conversion of arithmetic expressions in high-level
programming languages into machine readable form.
• As our computer system can only understand and work on a binary language, it assumes that
an arithmetic operation can take place in two operands only e.g., A+B, C*D,D/A etc.
• But in our usual form an arithmetic expression may consist of more than one operator and
two operands e.g. (A+B)*C(D/(J+D)).

Infix and Postfix Expressions
Input: a stream of input tokens containing operand and operators
Output: PostFix expression for the input expression.
1. Push “(“onto Stack, and add “)” to the end of X.
2. Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which has the same precedence
as or higher precedence than operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a left parenthesis is
encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]
7. END.
Infix and Postfix Expressions
Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator.
Trees
Tree Structure

Right
Tree Terminologies
More terminologies
Perfect Binary Tree
Complete Binary Tree
Binary Tree

• Parent: If N is any node in T that has left successor S1 and right successor S2, then N is called the
parent of S1 and S2. Correspondingly, S1 and S2 are called the left child and the right child of N.
Every node other than the root node has a parent.
• Sibling: S1 and S2 are said to be siblings. In other words, all nodes that are at the same level and
share the same parent are called siblings (brothers).
• Level number: Every node in the binary tree is assigned a level number. The root node is defined to
be at level 0. The left and right child of the root node have a level number 1. Similarly, every node
is at one level higher than its parents.
• Leaf node: A node that has no children.
• Degree: Degree of a node is equal to the number of children that a node has.
Binary Tree

• In-degree of a node is the number of edges arriving at that node.


• Out-degree of a node is the number of edges leaving that node.
• Edge: It is the line connecting a node N to any of its successors
• Path: A sequence of consecutive edges is called a path.
• Depth: The depth of a node N is given as the length of the path from the root to the node N. The
depth of the root node is zero.
• Height: It is the total number of nodes on the path from the root node to the deepest node in the
tree. A tree with only a root node has a height of 1.
• A binary tree of height h has at least h nodes and at most 2h – 1 nodes. This is because every level
will have at least one node and can have at most 2 nodes.
• The height of a binary tree with n nodes is at least log2(n+1) and at most n.
Forests

• A forest is a disjoint union of trees. A set of disjoint trees (or forest) is obtained by deleting the
root and the edges connecting the root node to nodes at level 1.
• Every node of a tree is the root of some sub-tree. Therefore, all the sub-trees immediately below a
node form a forest.
• A forest can also be defined as an ordered set of zero or more general trees.
• While a general tree must have a root, a forest on the other hand may be empty because by
definition it is a set, and sets can be empty.
• We can convert a forest into a tree by adding a single node as the root node of the tree.
Binary Trees - Key Terms
• Similar binary trees: Given two binary trees T and T’ are said to be similar if
both these trees have the same structure.
TREE T
TREE T”
A F

B C G H

D I

E J

• Copies of binary trees: Two binary trees T and T’ are said to be copies if they
have similar structure and same content at the corresponding nodes.
TREE T
TREE T”
A A

B C B C

E
D E D
Extended Binary Trees
• A binary tree T is said to be an extended binary tree (or a 2-tree) if each node in
the tree has either no child or exactly two children.
• In an extended binary tree nodes that have two children are called internal nodes
and nodes that have no child or zero children are called external nodes. In the
figure internal nodes are represented using a circle and external nodes are
represented using squares.
• To convert a binary tree into an extended tree, every empty sub-tree is replaced
by a new node. The original nodes in the tree are the internal nodes and the new
nodes added are called the external nodes.

Extended binary tree

Binary tree
Linear Representation
Linear Representation of Binary Trees
• Sequential representation of trees is done using a single or one dimensional array.
Though, it is the simplest technique for memory representation, it is very
inefficient as it requires a lot of memory space.
• A sequential binary tree follows the rules given below:
• One dimensional array called TREE is used.
• The root of the tree will be stored in the first location. That is, TREE[1] will store
the data of the root element.
• The children of a node K will be stored in location (2*K) and (2*K+1).
• The maximum size of the array TREE is given as (2h-1), where h is the height of the
tree.
• An empty tree or sub-tree is specified using NULL. If TREE[1] = NULL, then the tree
is empty. 20

15 35

3
12
17 21 9

3 4
16 18
6 5
Linked Representation
Linked Representation of Binary Trees
• In computer’s memory, a binary tree can be maintained either using a
linked representation or using sequential representation.
• In linked representation of binary tree, every node will have three parts:
the data element, a pointer to the left node and a pointer to the right
node. So in C, the binary tree is built with a node type given as below.
struct node
{ 1

struct node* left; 2 3

int data; 4 5 6 7

struct node* right;


X 8 X X 9 X X 10 X X 11 X X 12 X

};
Expression Trees

• Binary trees are widely used to store algebraic expressions. For


example, consider the algebraic expression Exp given as:
Exp = (a – b ) + ( c * d)
• This expression can be represented using a binary tree as shown
in figure
+

- *

a b c d
Tournament Trees
• In a tournament tree (also called a selection tree), each external
node represents a player and each internal node represents the
winner of the match played between the players represented by its
children nodes.
• These tournament trees are also called winner trees because they
are being used to record the winner at each level.
• We can also have a loser tree that records the loser at each level.
a

a e

a
d e g

a b c d e f g h
Binary Tree Operations
Initialize
Empty?
Height
Traversing a Binary Tree

• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.

• There are three different algorithms for tree traversals, which differ in
the order in which the nodes are visited.

• These algorithms are:

ü Pre-order algorithm

ü In-order algorithm

ü Post-order algorithm
Traversal
Preorder
Pre-order Algorithm

• To traverse a non-empty binary tree in preorder, the following


operations are performed recursively at each node.
• The algorithm starts with the root node of the tree and continues
by:
ü Visiting the root node
ü Traversing the left subtree A

ü Traversing the right subtree B C

D E

A, B, D, C, E, F, G, H and I
F

H I
Post and Inorder
In-order Algorithm
• To traverse a non-empty binary tree in in-order, the following operations are
performed recursively at each node.
• The algorithm starts with the root node of the tree and continues by,
ü Traversing the left subtree
ü Visiting the root node
A
ü Traversing the right subtree
B C

D E

B, D, A, E, H, G, I, F and C
G

H I
Post-order Algorithm
• To traverse a non-empty binary tree in post-order, the following
operations are performed recursively at each node.
• The algorithm starts with the root node of the tree and continues
by,
ü Traversing the left subtree
ü Traversing the right subtree A

ü Visiting the root node B C

D E

D, B, H, I, G, F, E, C and A G

H I
Applications of Trees
• Trees are used to store simple as well as complex data. Here simple
means an int value, char value and complex data (structure).
• Trees are often used for implementing other types of data structures like
hash tables, sets, and maps.
• A self-balancing tree, Red-black tree is used in kernel scheduling to
preempt massively multi-processor computer operating system use.
• Another variation of tree, B-trees are used to store tree structures on
disc. They are used to index a large number of records.
• B-trees are also used for secondary indexes in databases, where the index
facilitates a select operation to answer some range criteria.
• Trees are used for compiler construction.
• Trees are also used in database design.
• Trees are used in file system directories.
• Trees are also widely used for information storage and retrieval in symbol
tables.

You might also like