0% found this document useful (0 votes)
3 views35 pages

Programming and Data Structures

The document provides an overview of format specifiers in C, detailing their usage for various data types such as characters, integers, floating-point numbers, and strings. It also explains tree data structures, including binary trees and their types, AVL trees, red-black trees, B-trees, and B+ trees, along with their properties and operations. Additionally, it introduces segment trees for storing interval information and querying segments.

Uploaded by

diwakarkhushi69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views35 pages

Programming and Data Structures

The document provides an overview of format specifiers in C, detailing their usage for various data types such as characters, integers, floating-point numbers, and strings. It also explains tree data structures, including binary trees and their types, AVL trees, red-black trees, B-trees, and B+ trees, along with their properties and operations. Additionally, it introduces segment trees for storing interval information and querying segments.

Uploaded by

diwakarkhushi69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

# Print bitwise NOT operation

print(~a)
# ~a= -a-1

Format Specifiers in C
The format specifier in C is used to tell the compiler about the type of data to be printed or scanned in input and output operations.

1. Character Format Specifier – %c in C


The %c is the format specifier for the char data type in C language. It can be used for both formatted input and formatted output in C language.

2. Integer Format Specifier (signed) – %d in C


We can use the signed integer format specifier %d in the scanf() and print() functions or other functions that use formatted string for input and output
of int data type.

3. Unsigned Integer Format Specifier – %u in C


The %u is the format specifier for the unsigned integer data type. If we specify a negative integer value to the %u, it converts the integer to its first
complement.

// C Program to illustrate the how to use %u


#include <stdio.h>

// driver code
int main()
{
unsigned int var;

scanf("Enter an integer: %u", &var);

printf("Entered Unsigned Integer: %u", var);

// trying to print negative value using %u


printf("Printing -10 using %%u: %u\n", -10);
return 0;
}

Input:
Enter an integer: 25
Output:
Entered unsigned integer: 25
Printing -10 using %u: 4294967286
-10 will be converted to its positive equivalent and printed as 4294967286 (assuming 32-bit unsigned integers).

4. Floating-point format specifier – %f in C


The %f is the floating point format specifier in C language that can be used inside the formatted string for input and output of float data type. Apart from
%f, we can use %e or %E format specifiers to print the floating point value in the exponential form.
Syntax:
printf("%f...", ...);
scanf("%e...", ...);
printf("%E...", ...);

// C program to demonstrate the use of %f, %e and %E


#include <stdio.h>

// driver code
int main()
{
float a = 12.67;
printf("Using %%f: %f\n", a);
printf("Using %%e: %e\n", a);
printf("Using %%E, %E", a);
return 0;
}

Output
Using %f: 12.670000
Using %e: 1.267000e+01
Using %E, 1.267000E+01

5. Unsigned Octal number for integer – %o in C


We can use the %o format specifier in the C program to print or take input for the unsigned octal integer number.

#include <stdio.h>
int main()
{
int a = 67;
printf("%o\n", a);
return 0;
}
Output
103

6. Unsigned Hexadecimal for integer – %x in C


The %x format specifier is used in the formatted string for hexadecimal integers. In this case, the alphabets in the hexadecimal numbers will be in
lowercase. For uppercase alphabet digits, we use %X instead.
Syntax:
printf("%x...", ...);
scanf("%X...", ...);

// C Program to demonstrate the use of %x and %X


#include <stdio.h>
int main()
{
int a = 15454;
printf("%x\n", a);
printf("%X", a);
return 0;
}

Output
3c5e
3C5E

7. String Format Specifier – %s in C


The %s in C is used to print strings or take strings as input.
Syntax:
printf("%s...", ...);
scanf("%s...", ...);

// C program to illustrate the use of %s in C


#include <stdio.h>

int main()
{
char a[] = "Hi Geeks";
printf("%s\n", a);
return 0;
}

Output
Hi Geeks

8. Address Format Specifier – %p in C


The C language also provides the format specifier to print the address/pointers. We can use %p to print addresses and pointers in C
Syntax
printf("%p...", ...);

#include <stdio.h>
int main()
{
int a = 10;
printf("The Memory Address of a: %p\n",(void*)&a);
return 0;
}

Output
The Memory Address of a: 0x7ffe9645b3fc

Tree
A tree data structure is a hierarchical structure that is used to represent and organize data in a way that is easy to navigate and search. It is a collection of
nodes that are connected by edges and has a hierarchical relationship between the nodes.
Types of Tree data structures:
 Binary tree: In a binary tree, each node can have a maximum of two children linked to it. Some common types of binary trees include full binary trees,
complete binary trees, balanced binary trees, and degenerate or pathological binary trees.
 Ternary Tree: A Ternary Tree is a tree data structure in which each node has at most three child nodes, usually distinguished as “left”, “mid” and
“right”.
 N-ary Tree or Generic Tree : Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references
to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes.

Degree in Tree:
 Degree of a node is the total number of children of that node.
 Degree of a tree is the highest degree of a node among all the nodes in the tree.
Example-

Level-

 In a tree, each step from top to bottom is called as level of a tree.


 The level count starts with 0 and increments by 1 at each level or step.

Example-

 Total number of edges that lies on the longest path from any leaf node to a particular node is called as height of that node.
 Height of a tree is the height of root node.
 Height of all leaf nodes = 0
 Total number of edges from root node to a particular node is called as depth of that node.
 Depth of a tree is the total number of edges from root node to a leaf node in the longest path.
 Depth of the root node = 0
 The terms “level” and “depth” are used interchangeably.

Forest-
A forest is a set of disjoint trees.

Types of Binary Tree based on the number of children:


1. Full Binary Tree
2. Degenerate Binary Tree
3. Skewed Binary Trees

1. Full Binary Tree


A Binary Tree is a full binary tree if every node has 0 or 2 children.
2. Degenerate (or pathological) tree
A Tree where every internal node has one child.

3. Skewed Binary Tree


A skewed binary tree is a pathological/degenerate tree in which the tree is either dominated by the left nodes or the right nodes. Thus, there are two types
of skewed binary tree: left-skewed binary tree and right-skewed binary tree.

Types of Binary Tree On the basis of the completion of levels:


1. Complete Binary Tree
2. Perfect Binary Tree
3. Balanced Binary Tree

1. Complete Binary Tree


A Binary Tree is a Complete Binary Tree if all the levels are completely filled except possibly the last level and the last level has all keys as left as
possible.

2. Perfect Binary Tree


A Binary tree is a Perfect Binary Tree in which all the internal nodes have two children and all leaf nodes are at the same level.
In a Perfect Binary Tree, the number of leaf nodes is the number of internal nodes plus 1.

3. Balanced Binary Tree


A binary tree is balanced if the height of the tree is O(Log n) where n is the number of nodes. For Example, the AVL tree maintains O(Log n) height by
making sure that the difference between the heights of the left and right subtrees is at most 1. Red-Black trees maintain O(Log n) height by making sure
that the number of Black nodes on every root to leaf paths is the same and that there are no adjacent red nodes. Balanced Binary Search trees are
performance-wise good as they provide O(log n) time for search, insert and delete.
It is a type of binary tree in which the difference between the height of the left and the right subtree for each node is either 0 or 1. In the figure above, the
root node having a value 0 is unbalanced with a depth of 2 units.

Some Special Types of Trees:


On the basis of node values, the Binary Tree can be classified into the following special types:
1. Binary Search Tree
2. AVL Tree
3. Red Black Tree
4. B Tree
5. B+ Tree
6. Segment Tree

1. Binary Search Tree

 The left subtree of a node contains only nodes with keys lesser than the node’s key.
 The right subtree of a node contains only nodes with keys greater than the node’s key.
 The left and right subtree each must also be a binary search tree.

Deletion in a Binary Search Tree


Deletion in a binary search tree can be divided into three cases:
1. The node to be deleted is a leaf node: If the node to be deleted is a leaf node, it can simply be removed from the tree. The parent node of the deleted
node must have its corresponding child pointer set to NULL to reflect the change in the tree.
2. The node to be deleted has only one child: If the node to be deleted has only one child, the child can be promoted to replace the deleted node.
3. The node to be deleted has two children: If the node to be deleted has two children, the replacement node can be found by either selecting the
minimum value from the right subtree or the maximum value from the left subtree of the node to be deleted. After finding the replacement node, it can
be promoted to replace the deleted node.

Question:
Solution:

2. AVL Tree
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all
nodes.
Example of AVL Tree shown below:
The below tree is AVL because the differences between the heights of left and right subtrees for every node are less than or equal to 1

Operations on an AVL Tree:


 Insertion
 Deletion
 Searching [It is similar to performing a search in BST]

Rotating the subtrees in an AVL Tree:


An AVL tree may rotate in one of the following four ways to keep itself balanced:
1. Left Rotation:
When a node is added into the right subtree of the right subtree, if the tree gets out of balance, we do a single left rotation.

2. Right Rotation:
If a node is added to the left subtree of the left subtree, the AVL tree may get out of balance, we do a single right rotation.
3. Left-Right Rotation :
A left-right rotation is a combination in which first left rotation takes place after that right rotation executes.

4. Right-Left Rotation :
A right-left rotation is a combination in which first right rotation takes place after that left rotation executes.

Deletion in AVL Tree

Deletion may disturb the balance factor of an AVL tree and therefore the tree needs to be rebalanced in order to maintain the AVLness. For this purpose,
we need to perform rotations. The two types of rotations are L rotation and R rotation. Here, we will discuss R rotations. L rotations are the mirror images
of them.

If the node which is to be deleted is present in the left sub-tree of the critical node, then L rotation needs to be applied, if the node which is to be deleted is
present in the right sub-tree of the critical node, the R rotation will be applied.

R0 rotation (Node B has balance factor 0)

If the node B has 0 balance factor, and the balance factor of node A disturbed upon deleting the node X, then the tree will be rebalanced by rotating tree
using R0 rotation.
Example:
Delete the node 30 from the AVL tree shown in the following image.

Solution:
In this case, the node B has balance factor 0, therefore the tree will be rotated by using R0 rotation as shown in the following image. The node B(10)
becomes the root, while the node A is moved to its right. The right child of node B will now become the left child of node A.
R1 Rotation (Node B has balance factor 1)
R1 Rotation is to be performed if the balance factor of Node B is 1.

Example:
Delete Node 55 from the AVL tree shown in the following image.

Solution:
Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node A which becomes the critical node. This is the condition of R1 rotation
in which, the node A will be moved to its right (shown in the image below). The right of B is now become the left of A (i.e. 45).
The process involved in the solution is shown in the following image.

R-1 Rotation (Node B has balance factor -1)


R-1 rotation is to be performed if the node B has balance factor -1.

Example:
Delete the node 60 from the AVL tree shown in the following image.

Solution:
in this case, node B has balance factor -1. Deleting the node 60, disturbs the balance factor of the node 50 therefore, it needs to be R-1 rotated. The node C
i.e. 45 becomes the root of the tree with the node B(40) and A(50) as its left and right child.
3. Red Black Tree
A red-black tree is a kind of self-balancing binary search tree where each node has an extra bit, and that bit is often interpreted as the color (red or black).
These colors are used to ensure that the tree remains balanced during insertions and deletions. Although the balance of the tree is not perfect, it is good
enough to reduce the searching time and maintain it around O(log n) time, where n is the total number of elements in the tree.

4. B – Tree
A B-tree is a type of self-balancing tree data structure that allows efficient access, insertion, and deletion of data items. B-trees are commonly used in
databases and file systems, where they can efficiently store and retrieve large amounts of data. A B-tree is characterized by a fixed maximum degree (or
order), which determines the maximum number of child nodes that a parent node can have. Each node in a B-tree can have multiple child nodes and
multiple keys, and the keys are used to index and locate data items.

Time Complexity of B-Tree:

Sr. No. Algorithm Time Complexity

1. Search O(log n)

2. Insert O(log n)

3. Delete O(log n)

Properties of B-Tree:

 All leaves are at the same level.


 B-Tree is defined by the term minimum degree ‘t‘. The value of ‘t‘ depends upon disk block size.
 Every node except the root must contain at least t-1 keys. The root may contain a minimum of 1 key.
 All nodes (including root) may contain at most (2*t – 1) keys.
 Number of children of a node is equal to the number of keys in it plus 1.
 All keys of a node are sorted in increasing order. The child between two keys k1 and k2 contains all keys in the range from k1 and k2.
 B-Tree grows and shrinks from the root which is unlike Binary Search Tree. Binary Search Trees grow downward and also shrink from downward.
 Like other balanced Binary Search Trees, the time complexity to search, insert, and delete is O(log n).
 Insertion of a Node in B-Tree happens only at Leaf Node.

5. B+ Tree
A B+ tree is a variation of the B-tree that is optimized for use in file systems and databases. Like a B-tree, a B+ tree also has a fixed maximum degree and
allows efficient access, insertion, and deletion of data items. However, in a B+ tree, all data items are stored in the leaf nodes, while the internal nodes only
contain keys for indexing and locating the data items. This design allows for faster searches and sequential access of the data items, as all the leaf nodes are
linked together in a linked list.

6. Segment Tree
In computer science, a Segment Tree, also known as a statistic tree, is a tree data structure used for storing information about intervals, or segments. It
allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, it’s a structure that cannot be modified once
it’s built. A similar data structure is the interval tree.
A segment tree for a set I of n intervals uses O(n log n) storage and can be built in O(n log n) time. Segment trees support searching for all the intervals
that contain a query point in time O(log n + k), k being the number of retrieved intervals or segments.
Consider an array A of size N and a corresponding Segment Tree T:
1. The root of T will represent the whole array A[0:N−1].
2. Each leaf in the Segment Tree T will represent a single element A[i] such that 0≤i<N.
3. The internal nodes in the Segment Tree T represents the union of elementary intervals A[i:j] where 0≤i<j<N.

The root of the Segment Tree represents the whole array A[0:N−1]. Then it is broken down into two half intervals or segments and the two children of the root
in turn represent the A[0:(N−1)/2] and A[(N−1)/2+1:(N−1)]. So in each step, the segment is divided into half and the two children represent those two halves.
So the height of the segment tree will be log2N.

Binary Heap
A Binary Heap is a complete Binary Tree which is used to store data efficiently to get the max or min element based on its structure. A Binary Heap is
either Min Heap or Max Heap. In a Min Binary Heap, the key at the root must be minimum among all keys present in Binary Heap. The same property
must be recursively true for all nodes in Binary Tree.
Examples of Min Heap:
10 10
/ \ / \
20 100 15 30
/ / \ / \
30 40 50 100 40

// Heap Sort in C

#include <stdio.h>

// Function to swap the the position of two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i) {


// Find largest among root, left child and right child
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

// Swap and continue heapifying if root is not largest


if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

// Main function to do heap sort


void heapSort(int arr[], int n) {
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);

// Heapify root element to get highest element at root again


heapify(arr, i, 0);
}
}

// Print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}

// Driver code
int main() {
int arr[] = {1, 12, 9, 5, 6, 10};
int n = sizeof(arr) / sizeof(arr[0]);

heapSort(arr, n);

printf("Sorted array is \n");


printArray(arr, n);
}

Heap Sort Complexity

Time Complexity

Best O(nlog n)

Worst O(nlog n)

Average O(nlog n)

Space Complexity O(1)

Graph
A Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that
connect any two nodes in the graph. More formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted by G(E, V).
Types of Graphs:
1. Finite Graphs
A graph is said to be finite if it has a finite number of vertices and a finite number of edges.

2. Infinite Graph:
A graph is said to be infinite if it has an infinite number of vertices as well as an infinite number of edges.

3. Trivial Graph:
A graph is said to be trivial if a finite graph contains only one vertex and no edge. It is also known as a singleton graph or a single vertex graph.
4. Simple Graph:
A simple graph is a graph that does not contain more than one edge between the pair of vertices.

5. Multi Graph:
Any graph which contains some parallel edges but doesn’t contain any self-loop.

6. Null Graph:
A null graph is a graph with no edges. A null graph can also be referred to as an edgeless graph, an isolated graph, or a discrete graph

7. Complete Graph:
A simple graph with n vertices is called a complete graph if the degree of each vertex is n-1, that is, one vertex is attached with n-1 edges or the rest of the
vertices in the graph. A complete graph is also called Full Graph.

8. Pseudo Graph:
A graph G with a self-loop and some multiple edges is called a pseudo graph.

9. Regular Graph:
If a graph is regular, then every vertex has the same degree.
10. Bipartite Graph:
A bipartite graph is a graph in which the vertices can be divided into two disjoint sets, such that no two vertices within the same set are adjacent. In other
words, it is a graph in which every edge connects a vertex of one set to a vertex of the other set.

11. Labeled Graph:


If the vertices and edges of a graph are labeled with name, date, or weight then it is called a labeled graph. It is also called Weighted Graph.

12. Digraph Graph:


A graph G = (V, E) with a mapping f such that every edge maps onto some ordered pair of vertices (Vi, Vj) are called a Digraph. It is also called Directed
Graph. The ordered pair (Vi, Vj) means an edge between Vi and Vj with an arrow directed from Vi to Vj. Here in the figure: e1 = (V1, V2) e2 = (V2, V3)
e4 = (V2, V4)

13. Subgraph:
A graph G1 = (V1, E1) is called a subgraph of a graph G(V, E) if V1(G) is a subset of V(G) and E1(G) is a subset of E(G) such that each edge of G1 has
same end vertices as in G.

14. Connected or Disconnected Graph:


Graph G is said to be connected if any pair of vertices (Vi, Vj) of a graph G is reachable from one another. Or a graph is said to be connected if there exists
at least one path between each and every pair of vertices in graph G, otherwise, it is disconnected. A null graph with n vertices is a disconnected graph
consisting of n components. Each component consists of one vertex and no edge.
15. Cyclic Graph:
A graph G consisting of n vertices and n> = 3 that is V1, V2, V3- – – – Vn and edges (V1, V2), (V2, V3), (V3, V4)- – – – (Vn, V1) are called cyclic
graph.

Tree, Back, Forward and Cross Edges in DFS of Graph


Consider a directed graph given in below, DFS of the below graph is 1 2 4 6 3 5 7 8. In below diagram if DFS is applied on this graph a tree is obtained
which is connected using green edges.

 Tree Edge: It is an edge that is present in the tree obtained after applying DFS on the graph. All the Green edges are tree edges.
 Forward Edge: It is an edge (u, v) such that v is a descendant but not part of the DFS tree. An edge from 1 to 8 is a forward edge.
 Back edge: It is an edge (u, v) such that v is the ancestor of node u but is not part of the DFS tree. Edge from 6 to 2 is a back edge. Presence of
back edge indicates a cycle in directed graph.
 Cross Edge: It is an edge that connects two nodes such that they do not have any ancestor and a descendant relationship between them(in case
of DFS). The edge from node 5 to 4 is a cross edge.

Example:

Time Complexity(DFS):
Since all the nodes and vertices are visited, the average time complexity for DFS on a graph is O(V + E), where V is the number of vertices and E is the
number of edges. In case of DFS on a tree, the time complexity is O(V), where V is the number of nodes.

Articulation Points (or Cut Vertices) in a Graph


A vertex v is an articulation point (also called cut vertex) if removing v increases the number of connected components.

Degree of vertex in Directed graph

If the graph is a directed graph, then in this graph, each vertex must have an in-degree and out-degree.

In-degree of a vertex: A number of edges coming to the vertex.

Out-degree of a vertex: A number of edges coming out from the vertex


Degree of a vertex: Addition of in-degree of a vertex and out-degree of a vertex.

Tree Order

The order of the tree represents the maximum number of children a tree’s node could have. So when we say we have a B-Tree of order N, It means every
node of that B-Tree can have a maximum of N children.

Tree Degree

The degree of a tree represents the maximum degree of a node in the tree.

Infix notation
An infix notation is a notation in which an expression is written in a usual or normal format. It is a notation in which the operators lie between the operands.
The examples of infix notation are A+B, A*B, A/B, etc.
In order to parse any expression, we need to take care of two things, i.e., Operator precedence and Associativity. Operator precedence means the precedence
of any operator over another operator.

Precedence order
1. Parenthesis { }, ( ), [ ]
2. Exponential notation ^
3. Multiplication and Division *, /
4. Addition and Subtraction +, -

Associativity means when the operators with the same precedence exist in the expression. For example, in the expression, i.e., A + B - C, '+' and '-' operators
are having the same precedence, so they are evaluated with the help of associativity. Since both '+' and '-' are left-associative, they would be evaluated as (A +
B) - C.
Associativity order
1. ^ Right to Left
2. *, / Left to Right
3. +, - Left to Right

Infix to Prefix Conversion:

o Scan the expression from right to left.


o When operands arrive, print them.
o If operator arrives and stack is empty, then push operator into stack.
o If operator has higher or equal precedence than TOP of stack, push operator into stack.
o else, print the top of stack and pop. Test the operator against top of stack again and print the top of the stack and pop till the operator is of lower
precedence or top of stack is not ‘)’. After that push the operator.
o When we reach the end of the expression, pop, and print all the operators from the top of the stack.
o If the operator is ')', then push it into the stack.
o If the operator is '(', then pop all the operators from the stack till it the top of stack becomes ‘)’, after that pop the stack.
o If the top of the stack is ')', push the operator on the stack.
o When scanning is done print and pop the top of stack one by one.
o At the end, reverse the output.

Example: K + L - M * N + (O^P) * W/U/V * T + Q

Input expression Stack Prefix expression

Q Q

+ + Q

T + QT

* +* QT

V +* QTV

/ +*/ QTV

U +*/ QTVU

/ +*// QTVU

W +*// QTVUW

* +*//* QTVUW
) +*//*) QTVUW

P +*//*) QTVUWP

^ +*//*)^ QTVUWP

O +*//*)^ QTVUWPO

( +*//* QTVUWPO^

+ ++ QTVUWPO^*//*

N ++ QTVUWPO^*//*N

* ++* QTVUWPO^*//*N

M ++* QTVUWPO^*//*NM

- ++- QTVUWPO^*//*NM*

L ++- QTVUWPO^*//*NM*L

+ ++-+ QTVUWPO^*//*NM*L

K ++-+ QTVUWPO^*//*NM*LK

QTVUWPO^*//*NM*LK+-++

The above expression, i.e., QTVUWPO^*//*NM*LK+-++, is not a final expression. We need to reverse this expression to obtain the prefix expression.

Prefix to Infix Conversion:

 Scan the given prefix expression from right to left character by character.
 If the character is an operand, push it into the stack.
 if the character is an operator, pop the top two values from stack.
 Concatenate this operator with these two values (1st top value+operator+2nd top value) to get a new string.
 Now push this resulting string back into the stack.
 Repeat this process until the end of prefix expression. Now the value in the stack is the desired infix expression.

Input String Prefix Expression Stack (Infix)

*-A/BC-/AKL *-A/BC-/AK L

*-A/BC-/AKL *-A/BC-/A LK

*-A/BC-/AKL *-A/BC-/ LKA

*-A/BC-/AKL *-A/BC- L(A/K)

*-A/BC-/AKL *-A/BC ((A/K)-L)

*-A/BC-/AKL *-A/B ((A/K)-L)C

*-A/BC-/AKL *-A/ ((A/K)-L)CB

*-A/BC-/AKL *-A ((A/K)-L)(B/C)

*-A/BC-/AKL *- ((A/K)-L)(B/C)A

*-A/BC-/AKL * ((A/K)-L)(A-(B/C))

*-A/BC-/AKL ((A-(B/C))*((A/K)-L))

Infix to Postfix Conversion:

 Scan input string from left to right character by character.


 If the character is an operand, print the operand.
 If the character is an operator and operator's stack is empty or the operator is ‘(‘, push operator into operator's stack.
 If the operator's stack is not empty and not ‘(‘, then:
o If the precedence of scanned operator is greater than the top most operator of operator's stack, push this operator into operand's stack.
o else, print and pop the top of stack till the condition is satisfied or we find '(' operator. Then push the scanned operator.
o If the operator is ')', print and pop the top of stack until we find '(', then pop the top.
o Now print and pop all the remaining operators from the stack.
Example: A + B * C / ( E – F ) + ( D + G – H )

Input String Prefix expression Operator Stack

A+B*C/(E-F)+(D+G-H) A

A+B*C/(E-F)+(D+G-H) A +

A+B*C/(E-F)+(D+G-H) AB +

A+B*C/(E-F)+(D+G-H) AB +*

A+B*C/(E-F)+(D+G-H) ABC +*

A+B*C/(E-F)+(D+G-H) ABC* +/

A+B*C/(E-F)+(D+G-H) ABC* +/(

A+B*C/(E-F)+(D+G-H) ABC*E +/(

A+B*C/(E-F)+(D+G-H) ABC*E +/(-

A+B*C/(E-F)+(D+G-H) ABC*EF +/(-

A+B*C/(E-F)+(D+G-H) ABC*EF- +/

A+B*C/(E-F)+(D+G-H) ABC*EF-/+ +

A+B*C/(E-F)+(D+G-H) ABC*EF-/+ +(

A+B*C/(E-F)+(D+G-H) ABC*EF-/+D +(

A+B*C/(E-F)+(D+G-H) ABC*EF-/+D +(+

A+B*C/(E-F)+(D+G-H) ABC*EF-/+DG +(+

A+B*C/(E-F)+(D+G-H) ABC*EF-/+DG+ +(-

A+B*C/(E-F)+(D+G-H) ABC*EF-/+DG+H +(-

A+B*C/(E-F)+(D+G-H) ABC*EF-/+DG+H- +

A+B*C/(E-F)+(D+G-H) ABC*EF-/+DG+H-+

Postfix to infix Conversion:

 Scan the given postfix expression from left to right character by character.
 If the character is an operand, push it into the stack.
 But if the character is an operator, pop the top two values from stack.
 Concatenate this operator with these two values (2nd top value + operator + 1 st top value) to get a new string.
 Now push this resulting string back into the stack.
 Repeat this process until the end of postfix expression. Now the value in the stack is the infix expression.

Input String Postfix Expression Stack (Infix)

ab*c+ b*c+ a

ab*c+ *c+ ab

ab*c+ c+ (a*b)

A0b*c+ + (a*b)c

ab*c+ ((a*b)+c)
Question

Solution:

Note: If the question ask for Row major order, then A[I][J] = B + ((J - LC) + (I – LR) * N) * W
where N = no. of columns.

Question:

Solution:
Question:
Question:
Question:

Question:
Question:

Question:
Question:

Question:
Solution:
256 (Two fifty six)

Question:

Solution:

Question:
Question:

Solution:
If G is a graph with n vertices and p components, then G has n-p edges. Hence graph G has 9-3 = 6 edges.

Question:

Question:
Question:

Solution:
Question:

Question:
Question:

Solution:

Question:

Solution: x3 = 4(four)

Question:
Solution:
2004
2040

Question:

Question:
Question:

Solution:
Option A

Question:
Question:
Question:

Question:
Solution: 35(Three five)

Question:

Solution:
Question:

Solution:

Question:

Solution:
Question:

Solution:

You might also like