DSA_Notes
DSA_Notes
A data structure is a way of organizing and storing data so that it can be accessed and
modified efficiently. It is used to manage large amounts of data for processing, storage, and
retrieval.
+----------------+
| Data Structure |
+----------------+
|
-----------------------------------
| |
+---------+ +---------+
| Linear | | Non-Linear |
+---------+ +---------+
| |
---------- ----------------
| Array | | Tree | Graph |
| List | ----------------
| Stack |
| Queue |
----------
A sparse matrix is a matrix in which most of the elements are zero. It is stored efficiently using
different representations to save memory.
Representation Methods:
0 0 3 0
0 4 0 0
2 0 0 5
Triplet Representation:
A linear list is a data structure where elements are arranged in a sequential order. Examples
include arrays and linked lists.
Index: 0 1 2 3 4
Value: 10 20 30 40 50
1. Data
2. A pointer to the next node
Operations:
A Circular Linked List is a linked list where the last node points back to the first node instead
of NULL.
Example Diagram:
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
Operations:
Stack Representation:
TOP → [50]
[40]
[30]
A queue is a linear data structure that follows FIFO (First In, First Out).
Operations of a Queue
A binary tree is a tree where each node has at most two children (left and right).
10
/ \
20 30
/ \
40 50
Arrays are stored contiguously in memory, where the address of each element is calculated as:
1. Insertion
2. Deletion
3. Traversal
4. Search
1. Inorder (L → R → Root)
2. Preorder (Root → L → R)
3. Postorder (L → R → Root)
A
/\
B C
/\ \
D E F
Step-by-Step Execution:
Traversal Order: D → B → E → A → C → F
Step-by-Step Execution:
1. Visit A
2. Traverse left subtree of A → Go to B
3. Visit B
4. Traverse left subtree of B → Go to D
5. Visit D
6. Go back to B → Traverse right subtree → Go to E
7. Visit E
8. Go back to A → Traverse right subtree → Go to C
9. Visit C
10. Traverse right subtree of C → Go to F
11. Visit F
A→B→D→E→C→F
Preorder Traversal Diagram
(A)
/ \
(B) (C)
/ \ \
(D) (E) (F)
Traversal Order: A → B → D → E → C → F
Step-by-Step Execution:
D→E→B→F→C→A
Postorder Traversal Diagram
A
/\
B C
/\ \
(D) (E) (F)
Traversal Order: D → E → B → F → C → A
Infix notation requires operator precedence and parentheses to determine the order of
evaluation.
Postfix notation eliminates the need for parentheses, making it easier for computers to
evaluate expressions using stacks.
To convert an infix expression to postfix, use a stack to store operators while maintaining
precedence.
Algorithm Steps:
A+B*C
Step-by-step Execution Using Stack
Step Symbol Stack (Operators) Postfix Expression
1 A (operand) - A
2 + (operator) + A
3 B (operand) + AB
Step Symbol Stack (Operators) Postfix Expression
5 C (operand) +, * ABC
ABC*+
10. Conclusion
An AVL tree is a self-balancing binary search tree where the height difference (balance factor)
is at most 1.
Balancing is done using Rotations:
1. LL Rotation
2. RR Rotation
3. LR Rotation
4. RL Rotation