Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion

The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep some rules and they can’t differentiate between operators and parenthesis easily. The Prefix and Postfix expressions are quite understandable for the computers. For the infix to postfix conversion, we use stack data structure because

it works on Last in First out principle.

Algorithm: -

Step 1: Firstly, we push “(“ into the stack and also we add “)” to the end of the given input expression.

Step 2: Then we scan the input expression from left to right and we repeat step 3 to 6 for each element of the input expression until the stack is empty.

Step 3: If we encounter an operand then we just add it to output expression.

Step 4: If we encounter the left or opening parenthesis the we push it into the stack.

Step 5: If we encounter an operator, then:

a. We repeatedly check the precedence of incoming operator with the top of stack if the precedence of it is higher than the top of the stack then we simply add it into the stack else we pop the top of stack and add it into the output expression then again check the incoming operator precedence with the new of the top of stack.

b. If the precedence of both operators (incoming operator and the top of the stack) are same then use associativity rule.

Step 6: If we encounter the right or closing parenthesis, then:

a. We repeatedly pop the operator from the stack and add it to the output expression until the left parenthesis is encountered.

b. Then remove the left parenthesis from the stack.

Example: -

                The Input / Infix Expression:    A + ( ( B + C ) + ( D + E ) * F ) / G )

Infix ExpressionStackPostfix Expression
A(A
+( +A
(( + (A
(( + ( (A
B( + ( (A B
+( + ( ( +A B
C( + ( ( +A B C
)( + (A B C +
+( + ( +A B C+
(( + ( + (A B C +
D( + ( + (A B C + D
+( + ( + ( +A B C + D
E( + ( + ( +A B C + D E
)( + ( +A B C + D E +
*( + ( + *A B C + D E +
F( + ( + *A B C + D E + F
)( +A B C + D E + F * +
/( + /A B C + D E + F * +
G( + /A B C + D E + F * + G
)EMPTYA B C + D E + F * + G / +

The Output / Postfix Expression:     A B C + D E + F * + G / +

C- Program to covert infix expression to postfix expression:

 #include<stdio.h>
 #include<string.h>
 #include<math.h>
 #include<stdlib.h>
 #define BLANK ' '
 #define TAB '\t'
 #define MAX 50
 void push(long int element);
 long int pop();
 void infix_to_postfix();
 long int postfixEval();
 int precedence(char element);
 int isEmpty();
 int whiteSpace( char );
 char infix[ MAX ], postfix[ MAX ];
 long int stack[MAX];
 int top;
 int main()
 {
         long int value;
         top = -1;
         printf("Enter infix : ");
         gets( infix );
         infix_to_postfix();
         printf("Postfix : %s\n",postfix);
         value = postfixEval();
         printf("Value of expression : %ld\n",value);
         return 0;
 }/*End of main()*/
 void infix_to_postfix()
 {
         unsigned int i, p = 0;
         char next;
         char element;
         for(i = 0; i<strlen(infix); i++)
         {
                 element = infix[i];
                 if(!whiteSpace(element))
                 {
                         switch(element)
                         {
                         case '(':
                                 push(element);
                                 break;
                         case ')':
                                 while((next = pop()) != '(' )
                                         postfix[p++]  =  next;
                                 break;
                         case '+':
                         case '-':
                         case '*':
                         case '/':
                         case '%':
                         case '^':
                                 while( !isEmpty( ) &&  precedence(stack[top] ) >= precedence( element ) )
                                         postfix[p++] = pop();
                                 push( element );
                                 break;
                         default:  /* if an operand comes */
                              postfix[p++] = element;
                         }
                 }
         }
         while( !isEmpty( ) )
                 postfix[p++] = pop();
         postfix[p] = '\0'; /* End postfix with'\0' to make it a string  */
 }/*End of infix_to_postfix()*/
 /* This function returns the precedence of the operator */
 int precedence(char element)
 {
         switch(element)
         {
         case '(':
                 return 0;
         case '+':
         case '-':
                 return 1;
         case '*':
         case '/':
         case '%':
                 return 2;
         case '^':
                 return 3;
         default :
                 return 0;
         }
 }
 /* End of precedence() */
 void push(long int element)
 {
         if( top > MAX )
         {
                 printf("Stack overflow\n");
                 exit(1);
         }
         stack[ ++top ] = element;
 }
 /* End of push() */
 long int pop()
 {
         if( isEmpty() )
         {
                 printf(" Stack underflow\n ");
                 exit(1);
         }
         return (stack[ top-- ]);
 }
 /* End of pop() */
 int isEmpty()
 {
         if(top == -1)
                 return 1;
         else
                 return 0;
 }
 /* End of isEmpty() */
 int whiteSpace(char element)
 {
         if( element == BLANK || element == TAB )
                 return 1;
         else
                 return 0;
 }
 /* End of whiteSpace() */
 long int postfixEval()
 {
         long int a, b, temp, result;
         unsigned int i;
         for( i = 0; i<strlen(postfix); i++ )
         {
                 if( postfix[i] <= '9' && postfix[i] >= '0' )
                         push( postfix[i]-'0' );
                 else
                 {
                         a = pop();
                         b = pop();
                         switch( postfix[i] )
                         {
                         case '+':
                                 temp = b+a;
                      break;
                         case '-':
                                 temp = b-a;
                                 break;
                         case '*':
                                 temp = b*a;
                                 break;
                         case '/':
                                 temp = b/a;
                                 break;
                         case '%':
                                 temp = b%a;
                                 break;
                         case '^':
                                 temp = pow( b, a );
                         }
                         push( temp );
                 }
         }
         result = pop();
         return result;
 } 

Output: -

Time Complexity: -

The time complexity of infix to postfix operation is O(n^2) and the space complexity is O(n).



ADVERTISEMENT
ADVERTISEMENT