Data Structures Full Notes (All Units,
Detailed Line-by-Line)
Unit 1: Stack
Definition of Stack:
- A stack is a linear data structure which follows the LIFO (Last In First Out) principle.
- Insertion and deletion take place from only one end called the 'top' of the stack.
Operations on Stack:
- PUSH: Adds an element to the top of the stack.
- POP: Removes the top element from the stack.
Implementation of Stack using Array:
- Uses a fixed-size array.
- Requires management of a 'top' index.
Implementation of Stack using Linked List:
- Each node contains data and a pointer to the next node.
- The top of the stack is the head of the list.
Applications of Stack:
- Expression Conversion: Infix to Prefix/Postfix, Prefix to Infix/Postfix, etc.
- Expression Evaluation: Evaluating Postfix expressions using a stack.
- Recursion Handling: Function calls are managed using the call stack.
Unit 2: Queue
Definition of Queue:
- A linear data structure that follows FIFO (First In First Out) principle.
Types of Queue:
- Priority Queue: Elements are served based on priority rather than arrival time.
- Circular Queue: The last position is connected back to the first position to make a
circle.
- Double Ended Queue (Deque): Allows insertion and deletion from both front and rear.
Operations of Queue:
- INSERT (ENQUEUE): Add element at the rear end.
- DELETE (DEQUEUE): Remove element from the front end.
- TRAVERSE: Visit and display each element of the queue.
Implementation of Queue using Array:
- Use front and rear pointers.
- Limited by the size of the array.
Implementation of Queue using Linked List:
- Uses dynamic memory allocation.
- No fixed size limit, flexible growth.
Applications of Queue:
- Used in CPU scheduling, Disk scheduling.
- Data buffering and resource sharing in networking.
Unit 3: Tree
Definition of Tree:
- A hierarchical data structure with nodes connected by edges.
- The topmost node is the root, and nodes with no children are leaves.
Types of Trees:
- General Trees, Binary Trees, Binary Search Trees, AVL Trees, etc.
Binary Tree:
- A tree where each node has at most two children: left and right.
Properties of Binary Tree:
- A binary tree of height h has at most 2^h - 1 nodes.
- In a full binary tree, every node has 0 or 2 children.
Operations on Binary Tree:
- Insertion: Add new node while maintaining structure.
- Deletion: Remove node and rearrange connections.
- Searching: Traverse nodes to find specific value.
Tree Traversal Algorithms:
- Preorder: Visit root, left subtree, right subtree.
- Inorder: Visit left subtree, root, right subtree.
- Postorder: Visit left subtree, right subtree, root.
Binary Search Tree (BST):
- Left child < root < right child.
- Allows faster search, insertion, and deletion.
Implementation of Trees:
- Typically implemented using nodes and pointers.
AVL Trees:
- Self-balancing BST.
- Maintains height difference (balance factor) of at most 1.
Unit 4: Graph
Definition of Graph:
- A graph is a collection of vertices (nodes) and edges (connections).
- It is a non-linear data structure.
Types of Graphs:
- Directed and Undirected Graphs.
- Weighted and Unweighted Graphs.
- Cyclic and Acyclic Graphs.
Adjacency Representation of Graph:
- Adjacency Matrix: 2D array indicating presence/weight of edges.
- Adjacency List: Array of lists representing connected nodes.
Incidence Matrix Representation:
- A matrix showing relationship between vertices and edges.
Graph Traversal:
- Breadth First Traversal (BFS): Uses queue, level-by-level.
- Depth First Traversal (DFS): Uses stack or recursion, explores deep first.
Connectivity of Graphs:
- Connected Graph: Path exists between every pair of vertices.
- Disconnected Graph: Some vertices are not reachable from others.
Weighted Graph:
- Edges have weights representing cost, distance, etc.
Shortest Path Algorithm:
- Dijkstra’s Algorithm: Greedy method to find shortest paths from source.
Spanning Tree:
- A subgraph that connects all vertices with minimum edges and no cycles.
Minimum Spanning Tree:
- Spanning tree with the least total edge weight.
- Kruskal's Algorithm: Greedy, adds smallest edge avoiding cycles.
- Prim's Algorithm: Greedy, grows tree from starting node.
Unit 5: Sorting and Searching
Sorting Definition:
- Sorting is arranging elements in a specific order: ascending or descending.
Selection Sort:
- Repeatedly finds the minimum element and puts it at the beginning.
- Time Complexity: O(n²), Space: O(1), Not Stable.
Insertion Sort:
- Builds the sorted array by inserting each element at the right position.
- Time Complexity: O(n²), Space: O(1), Stable.
Bubble Sort:
- Repeatedly swaps adjacent elements if they are in wrong order.
- Time Complexity: O(n²), Stable.
Quick Sort:
- Divide and conquer algorithm that selects a pivot.
- Partitions the array around the pivot and recursively sorts parts.
- Avg Time: O(n log n), Worst Time: O(n²), Not Stable.
Merge Sort:
- Recursively divides the array and merges sorted subarrays.
- Time: O(n log n), Space: O(n), Stable.
Radix Sort:
- Sorts integers digit-by-digit using a stable sorting algorithm like counting sort.
- Time: O(nk), Stable.
Searching Definition:
- Searching is locating a specific item in a dataset.
Linear Search:
- Checks every element sequentially until match is found.
- Time Complexity: O(n).
Binary Search:
- Requires sorted data.
- Repeatedly divides the array and compares mid-element.
- Time Complexity: O(log n).