DS UNIT-3
DS UNIT-3
UNIT-3
Nonlinear Data Structures:
We read the linear data structures like an array, linked list, stack and queue in which all the elements are
arranged in a sequential manner. The different data structures are used for different kinds of data.
A tree is also one of the data structures that represent hierarchical data.
o A tree data structure is defined as a collection of objects or entities known as nodes that are
linked together to represent or simulate hierarchy.
o A tree data structure is a non-linear data structure because it does not store in a sequential
manner. It is a hierarchical structure as elements in a Tree are arranged in multiple levels.
o In the Tree data structure, the topmost node is known as a root node. Each node contains some
data, and data can be of any type. In the above tree structure, the node contains the name of the
employee, so the type of data would be a string.
o Each node contains some data and the link or reference of other nodes that can be called
children.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
o Recursive data structure: The tree is also known as a recursive data structure. A tree can be
defined as recursively because the distinguished node in a tree data structure is known as a root
node. The root node of the tree contains a link to all the roots of its subtrees. The left subtree is
shown in the yellow color in the below figure, and the right subtree is shown in the red color. The
left subtree can be further split into subtrees shown in three different colors. Recursion means
reducing something in a self-similar manner. So, this recursive property of the tree data structure
is implemented in various applications.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
o Number of edges: If there are n nodes, then there would n-1 edges. Each arrow in the structure
represents the link or path. Each node, except the root node, will have atleast one incoming link
known as an edge. There would be one link for the parent-child relationship.
o Depth of node x: The depth of node x can be defined as the length of the path from the root to
the node x. One edge contributes one-unit length in the path. So, the depth of node x can also be
defined as the number of edges between the root node and the node x. The root node has 0
depth.
o Height of node x: The height of node x can be defined as the longest path from the node x to
the leaf node.
Based on the properties of the Tree data structure, trees are classified into various categories.
Implementation of Tree
The tree data structure can be created by creating the nodes dynamically with the help of the pointers.
The tree in the memory can be represented as shown below:
The above figure shows the representation of the tree data structure in the memory. In the above
structure, the node contains three fields. The second field stores the data; the first field stores the address
of the left child, and the third field stores the address of the right child.
struct node
{
int data;
struct node *left;
struct node *right;
}
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
2. Decision-making: Trees are used to model decision-making processes, where each node
represents a decision point, and each branch represents a possible outcome.
3. Expression parsing: Trees are used to represent mathematical expressions and can be used to
evaluate them.
4. Huffman coding: Trees are used to compress data by representing frequently occurring data with
shorter codes and less frequent data with longer codes.
5. Graph algorithms: Many graph algorithms, such as depth-first search and breadth-first search, are
implemented using tree data structures.
6. Artificial Intelligence: Decision Trees and Random Forest are used in machine learning and AI as
models.
7. Database indexing: Trees are used to index and organize data in databases for faster retrieval.
8. Network routing protocols: Trees are used to organize and optimize communication networks.
9. Compiler Design: Syntax trees are used in compilers to represent the syntactic structure of a
program.
10. Game AI: Game AI often uses tree data structures to model game states and possible moves in a
game and to evaluate them using game-specific metrics.
o General tree: The general tree is one of the types of tree data structure. In the general tree, a
node can have either 0 or maximum n number of nodes. There is no restriction imposed on the
degree of the node (the number of nodes that a node can contain). The topmost node in a
general tree is known as a root node. The children of the parent node are known as subtrees.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
There can be n number of subtrees in a general tree. In the general tree, the subtrees are
unordered as the nodes in the subtree cannot be ordered.
Every non-empty tree has a downward edge, and these edges are connected to the nodes known
as child nodes. The root node is labeled with level 0. The nodes that have the same parent are
known as siblings.
Binary tree: Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree, each
node in a tree can have utmost two child nodes. Here, utmost means whether the node has 0
nodes, 1 node or 2 nodes.
o Binary Search tree: Binary search tree is a non-linear data structure in which one node is
connected to n number of nodes. It is a node-based data structure. A node can be represented in
a binary search tree with three fields, i.e., data part, left-child, and right-child. A node can be
connected to the utmost two child nodes in a binary search tree, so the node contains two
pointers (left child and right child pointer). Every node in the left subtree must contain a value less
than the value of the root node, and the value of each node in the right subtree must be bigger
than the value of the root node.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
AVL tree
It is one of the types of the binary tree, or we can say that it is a variant of the binary search tree. AVL tree
satisfies the property of the binary tree as well as of the binary search tree. It is a self-balancing binary
search tree that was invented by Adelson Velsky Lindas. Here, self-balancing means that balancing the
heights of left subtree and right subtree. This balancing is measured in terms of the balancing factor.
We can consider a tree as an AVL tree if the tree obeys the binary search tree as well as a balancing factor.
The balancing factor can be defined as the difference between the height of the left subtree and the
height of the right subtree. The balancing factor's value must be either 0, -1, or 1; therefore, each node
in the AVL tree should have the value of the balancing factor either as 0, -1, or 1.
Red-Black Tree
The red-Black tree is the binary search tree. The prerequisite of the Red-Black tree is that we should
know about the binary search tree. In a binary search tree, the value of the left-subtree should be less
than the value of that node, and the value of the right-subtree should be greater than the value of that
node. As we know that the time complexity of binary search in the average case is log 2n, the best case is
O(1), and the worst case is O(n).
When any operation is performed on the tree, we want our tree to be balanced so that all the operations
like searching, insertion, deletion, etc., take less time, and all these operations will have the time
complexity of log2n.
The red-black tree is a self-balancing binary search tree. AVL tree is also a height balancing binary search
tree then why do we require a Red-Black tree. In the AVL tree, we do not know how many rotations
would be required to balance the tree, but in the Red-black tree, a maximum of 2 rotations are required
to balance the tree. It contains one extra bit that represents either the red or black color of a node to
ensure the balancing of the tree.
o Preorder traversal
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
o Inorder traversal
o Postorder traversal
So, in this article, we will discuss the above-listed techniques of traversing a tree. Now, let's start
discussing the ways of tree traversal.
Preorder traversal
This technique follows the 'root left right' policy. It means that, first root node is visited after that the left
subtree is traversed recursively, and finally, right subtree is recursively traversed. As the root node is
traversed before (or pre) the left and right subtree, it is called preorder traversal.
So, in a preorder traversal, each node is visited before both of its subtrees.
Algorithm
Procedure : RPREORDER(T)
Given a binary tree whose root node address is given by pointer variable T and whose
node structure issame as described below. This procedure traverses the tree in
preorder, in a recursive manner.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
4. [Finished]
return
Now, start applying the preorder traversal on the above tree. First, we traverse the root node A; after that,
move to its left subtree B, which will also be traversed in preorder.
So, for left subtree B, first, the root node B is traversed itself; after that, its left subtree D is traversed. Since
node D does not have any children, move to right subtree E. As node E also does not have any children,
the traversal of the left subtree of root node A is completed.
Now, move towards the right subtree of root node A that is C. So, for right subtree C, first the root
node C has traversed itself; after that, its left subtree F is traversed. Since node F does not have any
children, move to the right subtree G. As node G also does not have any children, traversal of the right
subtree of root node A is completed.
Therefore, all the nodes of the tree are traversed. So, the output of the preorder traversal of the above
tree is -
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
A→B→D→E→C→F→G
Now, let's see an example of preorder traversal. It will be easier to understand the process of preorder
traversal using an example.
The nodes with yellow color are not visited yet. Now, we will traverse the nodes of the above tree using
preorder traversal.
40, 30, 25, 15, 28, 35, 50, 45, 60, 55, 70
Postorder traversal
This technique follows the 'left-right root' policy. It means that the first left subtree of the root node is
traversed, after that recursively traverses the right subtree, and finally, the root node is traversed. As the
root node is traversed after (or post) the left and right subtree, it is called postorder traversal.
So, in a postorder traversal, each node is visited after both of its subtrees.
Algorithm
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
Procedure : RPOSTORDER(T)
Given a binary tree whose root node address is given by pointer variable T and whose
node structure issame as described below. This procedure traverses the tree in
postorder, in a recursive manner.
5. [Finished]
return
Example
Now, start applying the postorder traversal on the above tree. First, we traverse the left subtree B that will
be traversed in postorder. After that, we will traverse the right subtree C in postorder. And finally, the root
node of the above tree, i.e., A, is traversed.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
So, for left subtree B, first, its left subtree D is traversed. Since node D does not have any children, traverse
the right subtree E. As node E also does not have any children, move to the root node B. After traversing
node B, the traversal of the left subtree of root node A is completed.
Now, move towards the right subtree of root node A that is C. So, for right subtree C, first its left
subtree F is traversed. Since node F does not have any children, traverse the right subtree G. As node G
also does not have any children, therefore, finally, the root node of the right subtree, i.e., C, is traversed.
The traversal of the right subtree of root node A is completed.
At last, traverse the root node of a given tree, i.e., A. After traversing the root node, the postorder
traversal of the given tree is completed.
Therefore, all the nodes of the tree are traversed. So, the output of the postorder traversal of the above
tree is -
D→E→B→F→G→C→A
Inorder traversal
This technique follows the 'left root right' policy. It means that first left subtree is visited after that root
node is traversed, and finally, the right subtree is traversed. As the root node is traversed between the left
and right subtree, it is named inorder traversal.
So, in the inorder traversal, each node is visited in between of its subtrees.
Algorithm
Procedure : RINORDER(T)
Given a binary tree whose root node address is given by pointer variable T and whose
node structure issame as described below. This procedure traverses the tree in inorder,
in a recursive manner.
1. [Check for empty Tree]
If T = NULL
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
4. [Process
the Right
Subtree] If
RPTR (T) ≠
NULL then
RINORDER
(RPTR (T))
5. [Finished]
return
Example
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
Now, start applying the inorder traversal on the above tree. First, we traverse the left subtree B that will be
traversed in inorder. After that, we will traverse the root node A. And finally, the right subtree C is
traversed in inorder.
So, for left subtree B, first, its left subtree D is traversed. Since node D does not have any children, so after
traversing it, node B will be traversed, and at last, right subtree of node B, that is E, is traversed. Node E
also does not have any children; therefore, the traversal of the left subtree of root node A is completed.
At last, move towards the right subtree of root node A that is C. So, for right subtree C; first, its left
subtree F is traversed. Since node F does not have any children, node C will be traversed, and at last, a
right subtree of node C, that is, G, is traversed. Node G also does not have any children; therefore, the
traversal of the right subtree of root node A is completed.
As all the nodes of the tree are traversed, the inorder traversal of the given tree is completed. The output
of the inorder traversal of the above tree is -
D→B→E→A→F→C→G
binary tree data structure is represented using two methods. Those methods are as follows...
1. Array Representation
2. Linked List Representation
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
To represent a binary tree of depth 'n' using array representation, we need one dimensional array with a maximum
size of 2n + 1.
The above example of the binary tree represented using Linked list representation is shown as follows...
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
Note: If the parent node has only the right child in the general tree then it becomes the rightmost
child node of the last node following the parent node in the binary tree. In the above example, if
node B has the right child node L then in binary tree representation L would be the right child of
node D.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
Below are the steps for the conversion of Generic Tree to Binary Tree:
1. As per the rules mentioned above, the root node of general tree A is the root node of the binary
tree.
2. Now the leftmost child node of the root node in the general tree is B and it is the leftmost child
node of the binary tree.
3. Now as B has E as its leftmost child node, so it is its leftmost child node in the binary tree
whereas it has C as its rightmost sibling node so it is its right child node in the binary tree.
4. Now C has F as its leftmost child node and D as its rightmost sibling node, so they are its left
and right child node in the binary tree respectively.
5. Now D has I as its leftmost child node which is its left child node in the binary tree but doesn’t
have any rightmost sibling node, so doesn’t have any right child in the binary tree.
6. Now for I, J is its rightmost sibling node and so it is its right child node in the binary tree.
7. Similarly, for J, K is its leftmost child node and thus it is its left child node in the binary tree.
8. Now for C, F is its leftmost child node, which has G as its rightmost sibling node, which
has H as its just right sibling node and thus they form their left, right, and right child node
respectively.
Algorithm
We will understand the algorithm stepwise. Let's go.
Create a generic tree and call the "genericToBinary" function.
o Check if the root node has no children. If true, return the root as the node is already a leaf node.
o Check if the root node has only one child. If yes, call the "genericToBinary" function recursively
with the child node and set it as the left child of the current node in the binary tree.
o If the root has multiple children, set the left child of the binary tree as the result of recursively
calling the genericToBinary function with the first child node as the argument.
o Set the right child of the binary tree as the result of recursively calling the genericToBinary
function with the next sibling node as the argument.
o Iterate over the remaining child nodes and set them as the left child of the rightmost node in the
binary tree.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
The Manipulation of Arithmetic Expression :
The way to write arithmetic expression is known as a notation. An arithmetic expression can
be written in three different but equivalent notations, i.e., without changing the essence or
output of an expression. These notations are −
Infix Notation
Prefix (Polish) Notation
Postfix (Reverse-Polish) Notation
These notations are named as how they use operator in expression. We shall learn the same
here in this chapter.
Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are used in-between
operands. It is easy for us humans to read, write, and speak in infix notation but the same
does not go well with computing devices. An algorithm to process infix notation could be
difficult and costly in terms of time and space consumption.
Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands.
For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known
as Polish Notation.
Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style, the operator
is postfixed to the operands i.e., the operator is written after the operands.
The following table briefly tries to show the difference in all three notations –
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
Parsing Expressions
To parse any arithmetic expression, we need to take care of operator precedence and
associativity also.
Precedence
When an operand is in between two different operators, which operator will take the
operand first, is decided by the precedence of an operator over others. For example −
Associativity
Associativity describes the rule where operators with the same precedence appear in an
expression. For example, in expression a + b − c, both + and − have the same precedence,
then which part of the expression will be evaluated first, is determined by associativity of
those operators. Here, both + and − are left associative, so the expression will be evaluated
as (a + b) − c.
Right
1 Exponentiation ^ Highest
Associative
The above table shows the default behavior of operators. At any point of time in expression
evaluation, the order can be altered by using parenthesis. For example −
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
In a + b*c, the expression part b*c will be evaluated first, with multiplication as precedence
over addition. We here use parenthesis for a + b to be evaluated first, like (a + b)*c.
Here, we will use the stack data structure for the conversion of infix expression to prefix expression. Whenever an operator
will encounter, we push operator into the stack. If we encounter an operand, then we append the operand to the expression.
K K
+ +
L + KL
- - K L+
M - K L+ M
* -* K L+ M
N -* KL+MN
+ + K L + M N*
K L + M N* -
( +( K L + M N *-
O +( KL+MN*-O
^ +(^ K L + M N* - O
P +(^ K L + M N* - O P
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
) + K L + M N* - O P ^
* +* K L + M N* - O P ^
W +* K L + M N* - O P ^ W
/ +/ K L + M N* - O P ^ W *
U +/ K L + M N* - O P ^W*U
/ +/ K L + M N* - O P ^W*U/
V +/ KL + MN*-OP^W*U/V
* +* KL+MN*-OP^W*U/V/
T +* KL+MN*-OP^W*U/V/T
+ + KL+MN*-OP^W*U/V/T*
KL+MN*-OP^W*U/V/T*+
Q + KL+MN*-OP^W*U/V/T*Q
KL+MN*-OP^W*U/V/T*+Q+
The final postfix expression of infix expression(K + L - M*N + (O^P) * W/U/V * T + Q) is KL+MN*-OP^W*U/V/T*+Q+.
K + L - M * N + (O^P) * W/U/V * T + Q
If we are converting the expression from infix to prefix, we need first to reverse the expression.
To obtain the prefix expression, we have created a table that consists of three columns, i.e., input expression, stack, and
prefix expression. When we encounter any symbol, we simply add it into the prefix expression. If we encounter the operator,
we will push it into the stack.
Q Q
+ + Q
T + QT
* +* QT
V +* QTV
/ +*/ QTV
U +*/ QTVU
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
/ +*// 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.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
o If the operator is '(', then pop all the operators from the stack till it finds ) opening bracket in the stack.
o If the top of the stack is ')', push the operator on the stack.
o At the end, reverse the output.
Here, we will see the conversion of prefix to postfix expression using a stack data structure.
Let's understand the conversion of Prefix to Postfix expression using Stack through an example.
/ Pop A from the stack L, A K / Pop two operands from the stack, i.e.,
Pop K from the stack A and K. Add '/' operator after K
Push A K / into the stack operand, i.e., AK/. Push AK/ into the
stack.
- Pop A K / and L from the A K / L - Pop two operands from the stack, i.e.,
stack. AK/ and L. Add '-' operator after 'L'
Push (A K / L -) into the operand.
stack
/ Pop B and C from the AK/L-, Pop two operands from the stack, i.e.,
stack. BC/ B and C. Add '/' operator after C
Push BC/ into the stack. operator, i.e., BC/. Push BC/ into the
stack.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
BC/, A
- Pop BC/ and A from the AK/L-, Pop two operands from the stack, i.e.,
stack. Push ABC/- into ABC/- A and BC/. Add '-' operator after '/'.
the stack.
* Pop ABC/- and AK/L- ABC/- Pop two operands from the stack, i.e.,
from the stack. Push AK/L-* ABC/-, and AK/L- . Add '*' operator
ABC/AK/L-* into the after L and '-' operator, i.e., ABC/-AK/L-
stack. *.
The following are the steps required to convert postfix into prefix expression:
ab-c+
First, we scan the expression from left to right. We will move '-' operator before the operand ab.
-abc+
The next operator '+' is moved before the operand -abc is shown as below:
+-abc
The following are the steps used to convert postfix to prefix expression using stack:
Create an expression by concatenating two operands and adding operator before the operands.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
Push the result back to the stack.
o Repeat the above steps until we reach the end of the postfix expression.
AB + CD - *
A Push A into A
the stack
+ Pop B from the +AB Pop two operands from the stack, i.e., A and B.
stack Add '+' operator before the operands AB, i.e., +AB.
Pop A from
the stack
Push +AB into
the stack.
- Pop D from +AB - Pop two operands from the stack, i.e., D and C.
the stack. CD Add '-' operator before the operands CD, i.e., -CD.
Pop C from the
stack.
Push -CD into
the stack
* Pop -CD from *+AB - Pop two operands from the stack, i.e., -CD and
the stack. CD +AB. Add '*' operator before +AB then the
Pop +AB from expression would become *+AB-CD.
the stack.
Push *+AB -CD
into the stack.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
Inserting into this structure is very much like inserting the same node into two separate lists. In multi-linked lists it
is quite common to have back-pointers, i.e. inverses of each of the forward links; in the above example, this would
mean that each node had 4pointers.
The sparse matrix can be represented by using a linked list for every row and column.
A node in a multi-linked list has four parts:
The first part stores the data.
The second stores the pointer to the next row.
Third for the pointer to the next column and
Fourth for storing the coordinate number of the cell in the matrix.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
List of List:
A multi-linked list can be used to represent a list of lists. For example, we can create a linked list where each node
is itself a list and have pointers to other nodes.
See the structure below:
It is a 2-dimensional data structure.
Here each node has three fields:
The first field stores the data.
The second field stores a pointer to the child node.
The third field stores the pointer to the next node.
By : Cheerag Jayswal
D. L. PATEL INSTITUTE OF MANAGEMENT & TECHNOLOGY MCA COLLEGE, HIMMATNAGAR
Subject : Data Structure (629401) MCA SEM - II
By : Cheerag Jayswal