0% found this document useful (0 votes)
2 views16 pages

IT12 – Data Structure & Algorithms

The document covers fundamental data structures including arrays, linked lists, stacks, queues, trees, heaps, and hashing techniques. It details their definitions, operations, advantages, disadvantages, and applications, along with algorithm design techniques such as brute-force, divide-and-conquer, and dynamic programming. Additionally, it provides a sample question bank for lab assessments related to these topics.

Uploaded by

kena46582
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)
2 views16 pages

IT12 – Data Structure & Algorithms

The document covers fundamental data structures including arrays, linked lists, stacks, queues, trees, heaps, and hashing techniques. It details their definitions, operations, advantages, disadvantages, and applications, along with algorithm design techniques such as brute-force, divide-and-conquer, and dynamic programming. Additionally, it provides a sample question bank for lab assessments related to these topics.

Uploaded by

kena46582
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/ 16

1.

Arrays and Lists


 Introduction & definitions
 Memory allocation & indexing
 Operations on 1D & 2D arrays/lists
 Applications: sparse matrices & string manipulation
2. Linked Lists
 Introduction and definition
 Memory allocation
 Types: singly, circular, doubly linked lists
 Implementation operation
3. Stacks and Queues
 Stack: definition, theory, and implementation using arrays and linked lists
 Queue variations, including circular queue and priority queue
4. Trees and Heaps
 Basic tree concepts: BT, BST
 Advanced trees: AVL, (and optionally segment/Fenwick trees)
 Heap structures: min-heap and max-heap
5. Sorting, Searching, Hashing, and Heaps
 Searching: linear, binary, interpolation
 Sorting: bubble, selection, insertion, merge, quick, heap sort
 Heap methods
 Hashing techniques: hash tables & collision handling
6. Algorithm Design Techniques
 Brute-force methods
 Divide-and-conquer
 Greedy techniques
 Dynamic programming
 Backtracking (e.g., for combinatorial problems)
Sample Question Bank (LAB ASSESSMENT)
Here are some representative questions and mini-project prompts aligned with
SPPU’s lab assessment trends:
1. Linked Lists
o “Implement a circular doubly-linked list that supports insertion,
deletion, reversal, and display operations.”
2. Stacks & Queues
o “Convert infix expressions to postfix using a stack, then evaluate
the result.”
3. Trees
o “Build a BST for N values, delete a node with two children, and
traverse it in-order.”
4. Heaps
o “Implement a max-heap and use heap sort to sort an array of N
elements.”
5. Hashing
o “Create a hash table with open addressing (linear probing) and
measure average comparison counts for M searches.”
siddhantica.in+5github.com+5studocu.com+5iims.ac.in+2scribd.c
om+2unipune.ac.in+2iims.ac.in+1scribd.com+1github.com+4sppu
codes.vercel.app+4git.kska.io+4
6. Graphs
o “Given an undirected graph, write programs for BFS and DFS, and
count reachable vertices from a given source.”
7. Algorithms
o Greedy: “Implement Kruskal’s algorithm to find the MST for a
weighted undirected graph.”
o Backtracking: “Solve N-Queen problem for N = 8. Count all valid
configurations.”
o DP: “Implement 0/1 knapsack using dynamic programming to
compute maximum value for weight W.”
1. Arrays and Lists
1. INTRODUCTION

Arrays and Lists are fundamental linear data structures used to store collections of data.
Arrays use contiguous memory locations to store data, whereas Lists may use dynamic
memory and flexible sizing depending on the implementation.

2. ARRAYS

2.1 Definition

An array is a data structure consisting of a collection of elements (values or variables), each


identified by at least one array index or key. It stores elements of the same data type and
has a fixed size.

2.2 Declaration and Initialization (Example in C/C++)

int arr[5]; // Declaration

int arr[5] = {10, 20, 30, 40, 50}; // Initialization

2.3 Memory Representation

Arrays are stored in contiguous memory locations. If the base address is 1000 and the size
of int is 4 bytes, then arr[2] would be stored at address 1008 (1000 + 2*4).

2.4 Types of Arrays

 One-dimensional Array

 Two-dimensional Array (Matrix)

 Multidimensional Arrays
2.5 Operations on Arrays

Operation Description Time Complexity

Access Retrieve an element at a specific index O(1)

Traverse Visit and print all elements O(n)

Insertion Insert element at specific index O(n)

Deletion Delete element from specific index O(n)

Searching Linear or binary search O(n) / O(log n)

2.6 Advantages

 Fast element access

 Simplicity in implementation

2.7 Disadvantages

 Fixed size

 Inefficient insertion and deletion

3. TWO-DIMENSIONAL ARRAYS

A 2D array is an array of arrays. It's used to store data in the form of a table or matrix.

Declaration in C/C++:

int matrix[3][4];

This declares a matrix with 3 rows and 4 columns.

Applications:

 Matrix operations

 Image processing

 Game development

4. SPARSE MATRICES
A sparse matrix is a matrix that has a large number of zero elements.

Why Sparse Matrices?

 Save space

 Reduce computation time

Representations:

 Triplet representation

 Linked representation

5. STRINGS

Strings are arrays of characters ending with a null character (\0 in C).

Declaration:

char str[10] = "hello";

String Operations:

 Copy

 Concatenate

 Compare

 Length

 Reverse

6. APPLICATIONS

 Implementing other data structures (Stacks, Queues)

 Mathematical computations

 Data manipulation (text, records)

7. SUMMARY

Arrays are the most basic yet powerful data structures that form the building blocks of many
algorithms and higher-level structures. Mastery over arrays and their variations lays the
groundwork for tackling complex algorithmic challenges.
2. Linked Lists
INTRODUCTION

A linked list is a linear data structure where each element is a separate object, called a node.
Each node contains data and a reference (or link) to the next node in the sequence. Unlike
arrays, linked lists do not require contiguous memory and allow dynamic memory allocation.

2. TYPES OF LINKED LISTS

2.1 Singly Linked List

Each node points to the next node and the last node points to NULL.

struct Node {

int data;

struct Node* next;

};

2.2 Doubly Linked List

Each node contains two links: one to the next node and another to the previous node.

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

2.3 Circular Linked List

The last node points to the first node instead of NULL.

2.4 Circular Doubly Linked List

A combination of circular and doubly linked list.


3. OPERATIONS ON LINKED LISTS

Operation Description Time Complexity

Traversal Visit all nodes O(n)

Insertion Insert at beginning, end, or middle O(1) to O(n)

Deletion Remove from beginning, end, or middle O(1) to O(n)

Search Find a node with given value O(n)

4. ADVANTAGES

 Dynamic size

 Efficient insertions/deletions

5. DISADVANTAGES

 No direct access to elements

 Uses more memory (due to pointers)

6. APPLICATIONS

 Implementation of stacks and queues

 Symbol tables in compilers

 Dynamic memory allocation (e.g., malloc/free)

7. SUMMARY

Linked lists provide flexibility in memory usage and ease of insertion/deletion compared to
arrays. Understanding various types of linked lists is essential for building efficient data
structures.
3. Stacks and Queues
INTRODUCTION

Stacks and Queues are linear data structures that follow different principles for element
access. A stack uses the LIFO (Last In, First Out) principle, whereas a queue uses FIFO (First
In, First Out).

2. STACKS

2.1 Definition

A stack is a collection of elements with two primary operations:

 Push: Adds an element to the top.

 Pop: Removes the element from the top.

2.2 Implementation

 Using Arrays

 Using Linked Lists

#define MAX 100

int stack[MAX];

int top = -1;

2.3 Operations

 Push(): Add an element

 Pop(): Remove top element

 Peek(): View top element

 isEmpty(): Check if stack is empty

 isFull(): Check if stack is full

2.4 Applications

 Expression evaluation (Postfix, Prefix)

 Backtracking (e.g., maze solving)

 Function call stacks


3. QUEUES

3.1 Definition

A queue allows elements to be inserted at the rear and removed from the front.

3.2 Types of Queues

 Simple Queue

 Circular Queue

 Priority Queue

 Double Ended Queue (Deque)

3.3 Implementation

 Using Arrays

 Using Linked Lists

#define SIZE 5

int queue[SIZE];

int front = -1, rear = -1;

3.4 Operations

 Enqueue(): Add element to rear

 Dequeue(): Remove element from front

 isFull() and isEmpty(): Check conditions

3.5 Applications

 Job Scheduling

 Buffer Handling

 Resource Management
4. DIFFERENCE BETWEEN STACK AND QUEUE

Feature Stack (LIFO) Queue (FIFO)

Insertion At top At rear

Deletion From top From front

Use Case Backtracking Scheduling, Buffering

5. SUMMARY

Stacks and Queues are key linear structures useful in many real-world scenarios like task
scheduling, expression parsing, and memory management. Mastering their operations and
variations prepares students for advanced algorithmic problems.
4. Trees and Heaps
INTRODUCTION

A tree is a non-linear data structure consisting of nodes connected by edges. It is hierarchical


in nature and widely used in various applications such as databases, compilers, and network
routing.

A heap is a special tree-based data structure that satisfies the heap property.

2. TREES

2.1 Terminology

 Root: Top node

 Leaf: Node with no children

 Parent/Child: Connected nodes

 Subtree: Tree rooted at a child node

 Height: Longest path from root to a leaf

2.2 Types of Trees

 Binary Tree: Each node has at most 2 children

 Binary Search Tree (BST): Left child < Root < Right child

 AVL Tree: Self-balancing BST

 B Tree / B+ Tree: Used in databases

2.3 Binary Tree Operations

 Insertion

 Deletion

 Traversal:

o In-order (Left, Root, Right)

o Pre-order (Root, Left, Right)

o Post-order (Left, Right, Root)

o Level-order (Breadth-first)
2.4 Applications of Trees

 Expression trees

 File systems

 Decision making (decision trees)

3. HEAPS

3.1 Definition

A heap is a complete binary tree satisfying the heap property:

 Max Heap: Parent >= children

 Min Heap: Parent <= children

3.2 Heap Operations

 Insertion: Add node at bottom and percolate up

 Deletion (Extract Max/Min): Remove root and heapify

 Heapify: Convert an array to a heap

 Heap Sort: Use heap to sort an array

3.3 Applications

 Priority Queues

 CPU Scheduling

 Graph algorithms (Dijkstra's, Prim's)

4. SUMMARY

Trees and Heaps are powerful hierarchical data structures. Trees support efficient search and
hierarchical organization, while heaps are ideal for priority-based operations and sorting.
5. Sorting, Searching, Hashing,
and Heaps
INTRODUCTION

Sorting and Searching are fundamental operations in computer science used to manage and
retrieve data efficiently. Hashing is a technique to map data of arbitrary size to fixed-size
values (hash codes), allowing for constant time average-case lookup.

2. SORTING ALGORITHMS

Sorting arranges data in a particular order (ascending/descending).

2.1 Bubble Sort

 Repeatedly swaps adjacent elements if they are in the wrong order.

 Time Complexity: O(n²)

2.2 Selection Sort

 Selects the smallest/largest element and swaps it with the first unsorted element.

 Time Complexity: O(n²)

2.3 Insertion Sort

 Builds the final sorted array one item at a time.

 Time Complexity: O(n²)

2.4 Merge Sort

 Divide and conquer strategy.

 Time Complexity: O(n log n)

2.5 Quick Sort

 Picks a pivot and partitions the array.

 Time Complexity: Average O(n log n), Worst O(n²)

2.6 Heap Sort

 Builds a heap and repeatedly extracts the max/min element.

 Time Complexity: O(n log n)


2.7 Comparison Table

Algorithm Best Average Worst Space

Bubble Sort O(n) O(n²) O(n²) O(1)

Selection Sort O(n²) O(n²) O(n²) O(1)

Insertion Sort O(n) O(n²) O(n²) O(1)

Merge Sort O(n log n) O(n log n) O(n log n) O(n)

Quick Sort O(n log n) O(n log n) O(n²) O(log n)

Heap Sort O(n log n) O(n log n) O(n log n) O(1)

3. SEARCHING ALGORITHMS

3.1 Linear Search

 Sequentially checks each element.

 Time Complexity: O(n)

3.2 Binary Search

 Works on sorted arrays. Repeatedly divides the array.

 Time Complexity: O(log n)

 Space Complexity: O(1)

3.3 Comparison Table

Method Time (Best) Time (Average) Time (Worst)

Linear Search O(1) O(n) O(n)

Binary Search O(1) O(log n) O(log n)


4. HASHING

Hashing is used to index and retrieve items in a database quickly.

4.1 Hash Functions

 Maps keys to indices in a hash table

 Example: index = key % table_size

4.2 Collision Resolution Techniques

 Chaining: Use a linked list at each index

 Open Addressing:

o Linear Probing

o Quadratic Probing

o Double Hashing

4.3 Load Factor

 Load Factor (λ) = Number of elements / Table size

 Helps in determining when to rehash

4.4 Applications of Hashing

 Symbol tables

 Caches

 Password storage

 Hash maps and hash sets

5. SUMMARY

Sorting helps organize data, searching retrieves data, and hashing allows fast access to data.
Efficient use of these techniques improves algorithm performance significantly.

You might also like