S2 Data Structures
S2 Data Structures
Unit 2
Data Structure
A data structure is a storage that is used to store and organize data. It is a
way of arranging data on a computer so that it can be accessed and
updated efficiently.
Linear data structure:
Data structure in which data elements are arranged linearly, where each element
is attached to its previous and next adjacent elements, is called a linear data
structure.
ARRAYS
An array is a collection of items stored at contiguous memory locations.
The idea is to store multiple items of the same data type together.
Array Operations:
1. Insertion Operation
In the insertion operation, we are adding one or more elements to the array.
Based on the requirement, a new element can be added at the beginning, end, or
any given index of array. This is done using input statements of the programming
languages.
2. Deletion Operation
In this array operation, we delete an element from the particular index of an
array. This deletion operation takes place as we assign the value in the
consequent index to the current index. Elements can be deleted from an array by
shifting the elements that come after it to fill the gap.
3. Search Operation
Searching for elements: Arrays can be searched for a specific element using
linear search or binary search algorithms. The search operation can be
performed by linear traversal from the first element to the last element.
Algorithm (Linear Search) :
1. Start
2. Create an Array of a desired data type and size.
3. Initialize a variable ‘i’ as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
4. Sorting elements
Elements in an array can be sorted in ascending or descending order using
algorithms like bubble sort, insertion sort, or quick sort, count sort.
5. Display Operation
This operation displays all the elements in the entire array using a print
statement.
6. Updating elements
Elements in an array can be updated or modified by assigning a new value to a
specific index.
7. Traversing elements
The elements in an array can be traversed in order, visiting each element once.
Applications of Array:
1. Sorting: Arrays can be used to sort data in ascending or
descending order. Sorting algorithms such as bubble sort, merge
sort, and quicksort rely heavily on arrays.
2. Storing and accessing data: Arrays are used to store and retrieve
data in a specific order. For example, an array can be used to store
the scores of a group of students, or the temperatures recorded by a
weather station.
3. Searching: Arrays can be searched for specific elements using
algorithms such as linear search and binary search.
4. Matrices: Arrays are used to represent matrices in mathematical
computations such as matrix multiplication, linear algebra, and
image processing.
5. Stacks and queues: Arrays are used as the underlying data
structure for implementing stacks and queues, which are commonly
used in algorithms and data structures.
Real-time applications of arrays:
● Financial Analysis: Arrays are used in financial analysis to store historical
stock prices and other financial data.
● Scientific Computing: Arrays are used in scientific computing to
represent numerical data, such as measurements from experiments and
simulations.
Advantages of array data structure:
● Efficient access to elements
● Memory efficiency
● Fast data retrieval
● Easy to implement
Sorting:
A Sorting Algorithm is used to rearrange a given array or list of elements
according to a comparison operator on the elements. The comparison operator is
used to decide the new order of elements in the respective data structure.
Insertion Sort:
Insertion Sort is a simple sorting algorithm that builds the final sorted array
one element at a time. It works by iteratively considering each element in
the input array and inserting it into its correct position in the already sorted
portion of the array. The algorithm is called "Insertion Sort" because it
inserts each element into its correct position.
Algorithm:
To sort an array of size N in ascending order, iterate over the array and
compare the current element (key) to its predecessor, if the key element is
smaller than its predecessor, compare it to the elements before. Move the
greater elements one position up to make space for the swapped element.
Characteristics:
● Simplest algorithms with a simple implementation.
● Efficient for small data values.
● Adaptive in nature; appropriate for data sets that are already partially
sorted.
LINKED LIST
● A linked list consists of nodes where each node contains a data field
and a reference(link) to the next node in the list.
● A linear data structure, in which elements are not stored at a contiguous
location, rather they are linked using pointers.
● Linked List forms a series of connected nodes, where each node stores
the data and the address of the next node.
➔ At end
Stack as an ADT:
For the stack data structure, the ADT defines the following operations:
push() : Inserts an element to the top of the stack.
pop() : Removes and returns the element from the top of the stack.
top (): Returns the top element of the stack without removing it.
isEmpty() : Checks if the stack is empty.
size () : Returns the number of elements currently in the stack.
Advantages:
● Can grow and shrink according to the needs at runtime.
Disadvantages:
● Requires extra memory due to the involvement of pointers.
● Random accessing is not possible in stack.
Application of Stacks:
● Redo-undo features at many places like editors, photoshop.
● Forward and backward features in web browsers.
● In Memory management, any modern computer uses a stack as the
primary management for a running purpose. Each program that is running
in a computer system has its own memory allocations.
● Stack also helps in implementing function calls in computers. The last
called function is always completed first.
● Stacks are also used to implement the undo/redo operation in text editors.
A queue follows the First-In-First-Out (FIFO) principle, meaning that the first
element added to the queue will be the first one to be removed.
Queue as an ADT:
Operations of a queue are:
1. Enqueue() – Adds an element to the end of the queue.
2. Dequeue() – Removal of elements from the queue.
3. front() – Returns element at front end without removing it
4. rear() – This operation returns the element at the rear end without
removing it.
5. isFull() – Checks if the queue is full.
6. isNull() – Checks if the queue is empty
7. size() – This operation returns the size of the queue i.e. the total number
of elements it contains.
enQueue(): This operation adds a new node after the rear and moves the rear to
the next node.
deQueue(): This operation removes the front node and moves the front to the
next node.
The size of the deque changes as elements are enqueued and dequeued.
Unit 3
Recursion
● Recursion is a programming technique in which a function calls itself
directly or indirectly to solve a problem.
● A recursive function is one that calls itself as part of its execution.
● Allows complex problems to be broken down into smaller, more
manageable subproblems, making it easier to solve them.
● Has a smaller code size.
● Recursive Case: This is the part of the function where the function calls
itself to solve a smaller subproblem. The recursion continues until the base
case is reached, at which point the function starts to "unwind" and return
the results back through each level of recursion.
● Step3 - Ensure the recursion terminates: Make sure that the recursive
function eventually reaches the base case, and does not enter an infinite
loop.
Linear Recursion
● Linear recursion is a type of recursion where a function calls itself only
once in each recursive call.
● The recursion forms a chain-like sequence, with each recursive call
leading to one subsequent recursive call until the base case is reached.
Binary Recursion
● Binary recursion is a type of recursion in which a function calls itself two
times in each recursive call.
● It's characterized by splitting the problem into two smaller subproblems,
making it a binary branching process.
● Each recursive call generates two subsequent recursive calls until the
base case is reached.
● Binary recursion generates a binary tree-like structure of function calls
leading to the base case.
Unit 4
Trees
● 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.
● Specialized method to organize and store data in the computer to be used
more effectively.
● The topmost node of the tree is called the root, and the nodes below it are
called the child nodes.
Binary Trees
A binary tree is a tree data structure in which each node can have at most two
children, which are referred to as the left child and the right child.
Insertion Operation
A new key is always inserted at the leaf.
Start searching for a key from the root till a leaf node. Once a leaf node is found,
the new node is added as a child of the leaf node.
Deletion Operation
It is used to delete a node with a specific key from the BST and return the new
BST.
Searching Operation
Algorithm to search for a key in a given Binary Search Tree:
● We compare the value to be searched with the value of the root.
● If it’s equal we are done with the search. If it's smaller we know that we
need to go to the left subtree because in a binary search tree all the
elements in the left subtree are smaller and all the elements in the right
subtree are larger.
● Repeat the above step till no more traversal is possible
● If at any iteration, a key is found, return True. Else False.
Unit 6
Binary Heap
A Binary Heap is a complete Binary Tree which is used to store data
efficiently to get the max or min element based on its structure.