Dsa JS
Dsa JS
NOTE : ( Explain 1 program for Each Questions) ( Common for all UNITS)
1) STACK :
DEFINITION :
Stack is a linear data structure that follows a particular order in which the operations
are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO
implies that the element that is inserted last, comes out first and FILO implies that the
element that is inserted first, comes out last.
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It
behaves like a stack of plates, where the last plate added is the first one to be removed.
A Queue Data Structure is a fundamental concept in computer science used for storing
and managing data in a specific order. It follows the principle of “First in, First out” (FIFO),
where the first element added to the queue is the first one to be removed. Queues are
commonly used in various algorithms and applications for their simplicity and efficiency in
managing data flow.
Infix expression:
The expression of the form “a operator b” (a + b) i.e., when an operator is in-between
every pair of operands.
Postfix expression:
The expression of the form “a b operator” (ab+) i.e., When every pair of operands is
followed by an operator.
Examples:
Input: A + B * C + D Input: ((A + B) – C * (D / E)) + F
Output: ABC*+D+ Output: AB+CDE/*-F+
Illustration (Example):
Follow the below illustration for a better understanding
Consider the infix expression exp = “a+b*c+d”
and the infix expression is scanned using the iterator i, which is initialized as i = 0.
1st Step: Here i = 0 and exp[i] = ‘a’ i.e., an operand. So add this in the postfix expression.
Therefore, postfix = “a”.
2nd Step: Here i = 1 and exp[i] = ‘+’ i.e., an operator. Push this into the stack. postfix = “a”
and stack = {+}.
3rd Step: Now i = 2 and exp[i] = ‘b’ i.e., an operand. So add this in the postfix expression.
postfix = “ab” and stack = {+}.
4th Step: Now i = 3 and exp[i] = ‘*’ i.e., an operator. Push this into the stack. postfix = “ab”
and stack = {+, *}.
5th Step: Now i = 4 and exp[i] = ‘c’ i.e., an operand. Add this in the postfix expression.
postfix = “abc” and stack = {+, *}.
6th Step: Now i = 5 and exp[i] = ‘+’ i.e., an operator. The topmost element of the stack has
higher precedence. So pop until the stack becomes empty or the top element has less
precedence. ‘*’ is popped and added in postfix. So postfix = “abc*” and stack = {+}.
Now top element is ‘+‘ that also doesn’t have less precedence. Pop it. postfix = “abc*+”.
Now stack is empty. So push ‘+’ in the stack. stack = {+}.
7th Step: Now i = 6 and exp[i] = ‘d’ i.e., an operand. Add this in the postfix expression.
postfix = “abc*+d”.
Final Step: Now no element is left. So empty the stack and add it in the postfix expression.
postfix = “abc*+d+”.
UNIT-3
1) TREE :
Introduction to 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.
This data structure is a specialized method to organize and store data in the computer
to be used more effectively. It consists of a central node, structural nodes, and sub-nodes,
which are connected via edges. We can also say that tree data structure has roots, branches,
and leaves connected.
Traversal:
i. Preorder Traversal
ii. In order Traversal
iii. Post-order Traversal
2) B-TREE & B+ TREE - DIFFERENCE :
B-Tree:
B-Tree is known as a self-balancing tree as its nodes are sorted in the inorder traversal.
In B-tree, a node can have more than two children. B-tree has a height of logM N (Where ‘M’
is the order of tree and N is the number of nodes).
B+ Tree:
B+ tree eliminates the drawback B-tree used for indexing by storing data pointers only
at the leaf nodes of the tree. Thus, the structure of leaf nodes of a B+ tree is quite different
from the structure of internal nodes of the B tree.
DIFFERENCE :
Insertion takes more time and it is not Insertion is easier and the results
Insertion
predictable sometimes. are always the same.
Deletion of the internal node is very complex Deletion of any node is easy
Deletion and the tree has to undergo a lot of because all node are found at
transformations. leaf.
Leaf nodes are not stored as structural linked Leaf nodes are stored as
Leaf Nodes
list. structural linked list.
Number of Number of nodes at any intermediary level ‘l’ Each intermediary node can have
Nodes is 2l. n/2 to n children.
3) TREE TRAVERSAL :
Tree Traversal Technique :
A Tree Data Structure can be traversed in following ways:
Inorder Traversal:
#Algorithm Inorder(tree)
i. Traverse the left subtree, i.e., call Inorder(left->subtree)
ii. Visit the root.
iii. Traverse the right subtree, i.e., call Inorder(right->subtree)
Uses of Preorder:
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to
get prefix expressions on an expression tree.
Postorder Traversal:
#Algorithm Postorder(tree)
i. Traverse the left subtree, i.e., call Postorder(left->subtree)
ii. Traverse the right subtree, i.e., call Postorder(right->subtree)
iii. Visit the root
Uses of Postorder:
Postorder traversal is used to delete the tree. Please see the question for the deletion of
a tree for details. Postorder traversal is also useful to get the postfix expression of an
expression tree.
(Exlain own programs)
UNIT-4
1) GRAPHS :
#Graph Data Structure And Algorithms
Graph Data Structure is a collection of nodes connected by edges. It’s used to
represent relationships between different entities. Graph algorithms are methods used to
manipulate and analyze graphs, solving various problems like finding the shortest path or
detecting cycles.
Components of a Graph:
I. Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are
also known as vertex or nodes. Every node/vertex can be labeled or unlabeled.
II. Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered
pair of nodes in a directed graph. Edges can connect any two nodes in any possible
way.
Example:
Step1: Initially queue and visited arrays are empty.
Step 3: Remove node 0 from the front of queue and visit the unvisited neighbours and push
them into queue.
Step 4: Remove node 1 from the front of queue and visit the unvisited neighbours and push
them into queue.
Step 5: Remove node 2 from the front of queue and visit the unvisited neighbours and push
them into queue.
Step 6: Remove node 3.Every neighbours of node 3 is visited, so move to the next node that
are in the front of the queue.
Steps 7: Remove node 4 from the front of queue and visit the unvisited neighbours and push
them into queue.
EXAMPLE :
Step1: Initially stack and visited arrays are empty.
Step 2: Visit 0 and put its adjacent nodes which are not visited yet into the stack.
Step 3: Now, Node 1 at the top of the stack, so visit node 1 and pop it from the stack and put
all of its adjacent nodes which are not visited in the stack.
Step 4: Now, Node 2 at the top of the stack, so visit node 2 and pop it from the stack and put
all of its adjacent nodes which are not visited (i.e, 3, 4) in the stack.
Step 5: Now, Node 4 at the top of the stack, so visit node 4 and pop it from the stack and put
all of its adjacent nodes which are not visited in the stack.
Step 6: Now, Node 3 at the top of the stack, so visit node 3 and pop it from the stack and put
all of its adjacent nodes which are not visited in the stack.
Now, Stack becomes empty, which means we have visited all the nodes and our DFS
traversal ends.
a) Time complexity: O(V + E), where V is the number of vertices and E is the number
of edges in the graph.
b) Auxiliary Space: O(V + E), since an extra visited array of size V is required, And
stack size for iterative call to DFS function.
UNIT-5
1) LINEAR SEARCH :
Linear Search is defined as a sequential search algorithm that starts at one end and
goes through each element of a list until the desired element is found, otherwise the search
continues till the end of the data set.
Example:
Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 30
Step 1: Start from the first element (index 0) and compare key with each element
(arr[i]).Comparing key with first element arr[0]. SInce not equal, the iterator moves to the
next element as a potential match.
Comparing key with next element arr[1]. SInce not equal, the iterator moves to the
next element as a potential match.
Step 2: Now when comparing arr[2] with key, the value matches. So the Linear Search
Algorithm will yield a successful message and return the index of the element when key is
found (here 2).
Time and Space Complexity of Linear Search:
Time Complexity:
i. Best Case: Best case complexity is O(1)
ii. Worst Case: The worst-case complexity is O(N) where N is the size of the list.
iii. Average Case: O(N)
Conditions:
To apply Binary Search algorithm:
The data structure must be sorted.
Access to any element of the data structure takes constant time.
EXAMPLE :
Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.
First Step: Calculate the mid and compare the mid element with the key. If the key is less
than mid element, move to left and if it is greater than the mid then move search space to the
right.
Key (i.e., 23) is greater than current mid element (i.e., 16). The search space moves to the
right.
Key is less than the current mid 56. The search space moves to the left.
Second Step: If the key matches the value of the mid element, the element is found and stop
search.
EXAMPLE:
Input: arr[] = {6, 0, 3, 5}
First Pass: The largest element is placed in its correct position, i.e., the end of the array.
Third Pass: Place the remaining two elements at their correct positions.
Total no. of passes: n-1
Total no. of comparisons: n*(n-1)/2
EXAMPLE:
array as an example: arr[] = {64, 25, 12, 22, 11}
First pass:For the first position in the sorted array, the whole array is traversed from index 0
to 4 sequentially. The first position where 64 is stored presently, after traversing whole array
it is clear that 11 is the lowest value.
Second Pass:
For the second position, where 25 is present, again traverse the rest of the array in a
sequential manner.
After traversing, we found that 12 is the second lowest value in the array and it
should appear at the second place in the array, thus swap these values.
Third Pass:
Now, for third place, where 25 is present again traverse the rest of the array and find
the third least value present in the array.
While traversing, 22 came out to be the third least value and it should appear at the
third place in the array, thus swap 22 with element present at third position.
Fourth pass:
Similarly, for fourth position traverse the rest of the array and find the fourth least
element in the array
As 25 is the 4th lowest value hence, it will place at the fourth position.
Fifth Pass:
At last the largest value present in the array automatically get placed at the last
position in the array
The resulted array is the sorted array.
First Pass:
Current element is 23
The first element in the array is assumed to be sorted.
The sorted part until 0th index is : [23]
Second Pass:
Compare 1 with 23 (current element with the sorted part).
Since 1 is smaller, insert 1 before 23.
The sorted part until 1st index is: [1, 23]
Third Pass:
Compare 10 with 1 and 23 (current element with the sorted part).
Since 10 is greater than 1 and smaller than 23, insert 10 between 1 and 23.
The sorted part until 2nd index is: [1, 10, 23]
Fourth Pass:
Compare 5 with 1, 10, and 23 (current element with the sorted part).
Since 5 is greater than 1 and smaller than 10, insert 5 between 1 and 10.
The sorted part until 3rd index is: [1, 5, 10, 23]
Fifth Pass:
Compare 2 with 1, 5, 10, and 23 (current element with the sorted part).
Since 2 is smaller than all elements in the sorted part, insert 2 at the beginning.
The sorted part until 4th index is: [2, 1, 5, 10, 23]
Final Array:
The sorted array is: [2, 1, 5, 10, 23]
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Applications of Hashing :
Hash tables are used in a wide variety of applications, including:
Databases: Storing and retrieving data based on unique keys
Caching: Storing frequently accessed data for faster retrieval
Symbol Tables: Mapping identifiers to their values in programming languages
Network Routing: Determining the best path for data packets