Postfix to Infix
Postfix to Infix Example: abc-+de-fg-h+/*
Expression Stack
abc-+de-fg-h+/* NULL
bc-+de-fg-h+/* a
c-+de-fg-h+/* ba
-+de-fg-h+/* cba
+de-fg-h+/* a “b-c”
de-fg-h+/* “a+b-c”
e-fg-h+/* d “a+b-c”
-fg-h+/* ed “a+b-c”
fg-h+/* “d-e” “a+b-c”
g-h+/* f “d-e” “a+b-c”
-h+/* gf “d-e” “a+b-c”
h+/* “f-g” “d-e” “a+b-c”
+/* h “f-g” “d-e” “a+b-c”
/* “f-g+h” “d-e” “a+b-c”
* “d-e”/”f-g+h” “a+b-c”
“a+b-c”*“d-e”/”f-g+h”
1.While there are input symbol left
1.1 Read the next symbol from the input.
2.If the symbol is an operand
2.1 Push it onto the stack.
3.Otherwise,
3.1 the symbol is an operator.
3.2 Pop the top 2 values from the stack.
3.3 Put the operator, with the values as arguments and form a string.
3.4 Push the resulted string back to stack.
4.If there is only one value in the stack
4.1 That value in the stack is the desired infix string.
Output: (a+b-c)*(d-e)/(f-g+h)
Priority Queue
Priority Queue
Priority Queue is an extension of queue with following properties.
1. Every item has a priority associated with it.
2. An element with high priority is dequeued before an element with low priority.
3. If two elements have the same priority, they are served according to their order in the queue.
Priority Queue
/* Function to insert value into priority queue */
void insert_by_priority(int data)
{
if (rear >= MAX - 1)
{
printf("n Queue overflow");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pri_que[rear] = data;
return;
}
else
check(data);
rear++;
}
/* Function to check priority and place element */
void check(int data)
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (data >= pri_que[i])
{
for (j = rear + 1; j > i; j--)
{
pri_que[j] = pri_que[j - 1];
}
pri_que[i] = data;
return;
}
}
pri_que[i] = data;
}
Priority Queue
/* Function to delete an element from queue */
void delete_by_priority()
{
int i;
if ((front==-1) && (rear==-1)|| (front > rear))
{
printf("nQueue is empty no elements to delete");
return;
}
printf("Element deleted from Queue is: %dn",pri_que[front]);
front = front + 1;
}
Time complexity
Enqueue – O(?)
Dequeue – O(?)
Traversal – O(?)

Postfix to Infix and priority queue.pptx

  • 1.
  • 2.
    Postfix to InfixExample: abc-+de-fg-h+/* Expression Stack abc-+de-fg-h+/* NULL bc-+de-fg-h+/* a c-+de-fg-h+/* ba -+de-fg-h+/* cba +de-fg-h+/* a “b-c” de-fg-h+/* “a+b-c” e-fg-h+/* d “a+b-c” -fg-h+/* ed “a+b-c” fg-h+/* “d-e” “a+b-c” g-h+/* f “d-e” “a+b-c” -h+/* gf “d-e” “a+b-c” h+/* “f-g” “d-e” “a+b-c” +/* h “f-g” “d-e” “a+b-c” /* “f-g+h” “d-e” “a+b-c” * “d-e”/”f-g+h” “a+b-c” “a+b-c”*“d-e”/”f-g+h” 1.While there are input symbol left 1.1 Read the next symbol from the input. 2.If the symbol is an operand 2.1 Push it onto the stack. 3.Otherwise, 3.1 the symbol is an operator. 3.2 Pop the top 2 values from the stack. 3.3 Put the operator, with the values as arguments and form a string. 3.4 Push the resulted string back to stack. 4.If there is only one value in the stack 4.1 That value in the stack is the desired infix string. Output: (a+b-c)*(d-e)/(f-g+h)
  • 3.
  • 4.
    Priority Queue Priority Queueis an extension of queue with following properties. 1. Every item has a priority associated with it. 2. An element with high priority is dequeued before an element with low priority. 3. If two elements have the same priority, they are served according to their order in the queue.
  • 5.
    Priority Queue /* Functionto insert value into priority queue */ void insert_by_priority(int data) { if (rear >= MAX - 1) { printf("n Queue overflow"); return; } if ((front == -1) && (rear == -1)) { front++; rear++; pri_que[rear] = data; return; } else check(data); rear++; } /* Function to check priority and place element */ void check(int data) { int i,j; for (i = 0; i <= rear; i++) { if (data >= pri_que[i]) { for (j = rear + 1; j > i; j--) { pri_que[j] = pri_que[j - 1]; } pri_que[i] = data; return; } } pri_que[i] = data; }
  • 6.
    Priority Queue /* Functionto delete an element from queue */ void delete_by_priority() { int i; if ((front==-1) && (rear==-1)|| (front > rear)) { printf("nQueue is empty no elements to delete"); return; } printf("Element deleted from Queue is: %dn",pri_que[front]); front = front + 1; } Time complexity Enqueue – O(?) Dequeue – O(?) Traversal – O(?)