You are on page 1of 5

Ex.No: IMPLEMENT AN EXPRESSION TREE.

PRODUCE ITS
Date: PRE-ORDER, IN-ORDER, & POST- ORDER TRAVERSALS
AIM:
To develop a program to implement an expression tree, that produce its pre-order, in-
order and post-order traversals.
ALGORITHM:
1. Start.
2. Create a binary tree with the nodes partitioned into three disjoint subsets. The irst subset
consists o a single element called the root o the tree.the other two subsets are
themselves binary trees called the let and right sub trees o the original tree.! let or
right sub tree can be empty."ach element o a binary tree is called node o the tree.
#. ! common operation in the binary tree is to traverse that is to pass through the tree
enumerating each nodes once.The traversal o the binary tree can be done in three ways
they are preorder,inorder,postorder traversal.
$. To traverse a nonempty binary tree preorder we perorm the ollowing three operations%as
depth-irst order&
o 'isit the root
o Traverse the let subtree in preorder
o Traverse the right subtree in preorder
1. To traverse a nonempty binary tree inorder we perorm the ollowing three
operations%as symmetric order&
o Traverse the let subtree in inorder
o 'isit the root
o Traverse the right subtree in inorder
2. To traverse a nonempty binary tree postorder we perorm the ollowing three operations
o Traverse the let subtree in postorder
o Traverse the right subtree in postorder
o 'isit the root
#. Stop.
PROGRAM:
(include)stdio.h*
(include)conio.h*
(include)ctype.h*
(include)alloc.h*
(deine si+e 2,
typede struct node
-
char data.
struct node /let.
struct node /right.
0
btree. /* ta!" to#e t$e o%e#a&' &o'e o( t$e t#ee */
btree /stac12si+e3.
int top.
void main%&
-
btree /root.
char exp24,3. /* ex% to#e %ot()x ex%#e)o& */
btree /create%char exp24,3&.
void inorder%btree /root&.
void preorder%btree /root&.
void postorder%btree /root&.
clrscr%&.
print%56n enter the postix espression76n8&.
scan%59s8,exp&.
top:-1. /* I&)t)a*)+e t$e ta!" */
root:create%exp&.
print%56n the tree is created.6n8&.
print%56n ;norder 2raversal76n6n8&.
inorder%root&.
print%56n preorder 2raversal76n6n8&.
preorder%root&.
print%56n postorder 2raversal76n6n8&.
postorder%root&.
getch%&.
0
btree /create%char exp23&
-
btree /temp.
int pos.
char ch.
void push%btree/&.
btree /pop%&.
pos:,.
ch:exp2pos3.
while%ch<:=6,=&
- /* !#eate &e, &o'e */
temp:%%btree/&malloc%si+eo%btree&&&.
temp-*let:temp-*right:>?@@.
temp-*data:ch.
i%isalpha%ch&&
push%temp&.
else i%ch::=A=BBch::=-CBBch::=/=BBch::=D=&
-
temp-*right:pop%&.
temp-*let:pop%&.
push%temp&.
0
else
print%56n ;nvalid char espression6n8&.
posAA.
ch:exp2pos3.
0
temp:pop%&.
return %temp&.
0
void push%btree /node&
-
i%topA1*:si+e&
print%5error7stac1 is ull6n8&.
topAA.
stac12top3:node.
0
btree/ pop%&
-
btree /node.
i%top::-1&
print%56n error7stac1 is empty..6n8&.
node:stac12top3.
top--.
return%node&.
0
void inorder%btree /root&
-
btree /temp.
temp:root.
i%temp<:>?@@&
-
inorder%temp-*let&.
print%59c8,temp-*data&.
inorder%temp-*right&.
0
0
void preorder%btree /root&
-
btree /temp.
temp:root.
i%temp<:>?@@&
-
print%59c8,temp-*data&.
preorder%temp-*let&.
preorder%temp-*right&.
0
0
void postorder%btree /root&
-
btree /temp.
temp:root.
i%temp<:>?@@&
-
postorder%temp-*let&.
postorder%temp-*right&.
print%59c8,temp-*data&.
0
0
OUTPUT:
"nter the postix expression7
abAcd-/
The tree is createdE..
;norder traversal7
aAb/c-d
Freorder traversal7
/Aab-cd
Fostorder traversal7
abAcd-/
RESULT:
Thus a C program has been executed successully to implement an expression tree.

You might also like