Dsa Unit 1notes
Dsa Unit 1notes
UNIT I
A data structure is a particular way of organising data in a computer so that it can be used
effectively. The idea is to reduce the space and time complexities of different tasks.
1
1. Array:
An array is a collection of data items stored at contiguous memory locations. The idea is to
store multiple items of the same type together. This makes it easier to calculate the position
of each element by simply adding an offset to a base value, i.e., the memory location of the
first element of the array (generally denoted by the name of the array).
2. Linked Lists:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not
stored at a contiguous location; the elements are linked using pointers.
3. Stack:
Stack is a linear data structure which 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). In stack, all
insertion and deletion are permitted at only one end of the list.
Stack Operations:
● push(): When this operation is performed, an element is inserted into the stack.
2
● pop(): When this operation is performed, an element is removed from the top of the stack
and is returned.
● top(): This operation will return the last inserted element that is at the top without
removing it.
● size(): This operation will return the size of the stack i.e. the total number of elements
present in the stack.
● isEmpty(): This operation indicates whether the stack is empty or not.
4. Queue:
Like Stack, Queue is a linear structure which follows a particular order in which the
operations are performed. The order is First In First Out (FIFO). In the queue, items are
inserted at one end and deleted from the other end. A good example of the queue is any queue
of consumers for a resource where the consumer that came first is served first. The difference
between stacks and queues is in removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.
Queue Operations:
● Enqueue(): Adds (or stores) an element to the end of the queue..
● Dequeue(): Removal of elements from the queue.
● Peek() or front(): Acquires the data element available at the front node of the queue
without deleting it.
● rear(): This operation returns the element at the rear end without removing it.
● isFull(): Validates if the queue is full.
● isNull(): Checks if the queue is empty.
5. Binary Tree:
Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are
hierarchical data structures. A binary tree is a tree data structure in which each node has at
most two children, which are referred to as the left child and the right child. It is implemented
mainly using Links.
3
A Binary Tree is represented by a pointer to the topmost node in the tree. If the tree is empty,
then the value of root is NULL. A Binary Tree node contains the following parts.
1. Data
2. Pointer to left child
3. Pointer to the right child
7. Heap:
A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
Generally, Heaps can be of two types:
● Max-Heap: In a Max-Heap the key present at the root node must be greatest among the
keys present at all of its children. The same property must be recursively true for all sub-
trees in that Binary Tree.
4
● Min-Heap: In a Min-Heap the key present at the root node must be minimum among the
keys present at all of its children. The same property must be recursively true for all sub-
trees in that Binary Tree.
9. Matrix:
A matrix represents a collection of numbers arranged in an order of rows and columns. It is
necessary to enclose the elements of a matrix in parentheses or brackets.
A matrix with 9 elements is shown below.
5
10. Graph:
Graph is a data structure that consists of a collection of nodes (vertices) connected by edges.
Graphs are used to represent relationships between objects and are widely used in computer
science, mathematics, and other fields. Graphs can be used to model a wide variety of real-
world systems, such as social networks, transportation networks, and computer networks.
An algorithm is a set of step-by-step instructions for solving a problem or completing a task. It tells us
exactly what to do and how to get the final result. Computers use algorithms to help them make
decisions, process data, or perform actions automatically. They can be very simple, like sorting a list
of numbers, or very complex, like recommending videos on YouTube.
6
An algorithm needs to be clear, precise, and finish after a certain number of steps. It should
not go on forever without reaching an answer.
Singly linked lists contain two "buckets" in one node; one bucket holds the data and the other
bucket holds the address of the next node of the list. Traversals can be done in one direction
only as there is only a single link between two nodes of the same list.
Doubly Linked Lists contain three "buckets" in one node; one bucket holds the data and the
other buckets hold the addresses of the previous and next nodes in the list. The list is
traversed twice as the nodes in the list are connected to each other from both sides.
Circular linked lists can exist in both singly linked list and doubly linked list.
Since the last node and the first node of the circular linked list are connected, the traversal in
this linked list will go on forever until it is broken.
7
What Is Time Complexity?
Time complexity is defined in terms of how many times it takes to run a given
algorithm, based on the length of the input. Time complexity is not a measurement of
how much time it takes to execute a particular algorithm because such factors as
programming language, operating system, and processing power are also considered.
Assuming that n is the size of the input, let's use T(n) to represent the overall time and t to
represent the amount of time that a statement or collection of statements takes to execute.
Example:
int add (int n){
if (n <= 0){
return 0;
}
8
return n + add (n-1);
}
Here each call add a level to the stack :
1. add(4)
2. -> add(3)
3. -> add(2)
4. -> add(1)
5. -> add(0)
Each of these calls is added to call stack and takes up actual memory.
So it takes O(n) space.
However, just because you have n calls total doesn’t mean it takes O(n) space.
The size of the input data is the primary Primarily determined by the auxiliary
determinant. variable size
9
Asymptotic Notations are programming languages that allow you to analyze an algorithm's
running time by identifying its behavior as its input size grows. This is also referred to as an
algorithm's growth rate. When the input size increases, does the algorithm become incredibly
slow? Is it able to maintain its fast run time as the input size grows? You can answer these
questions thanks to Asymptotic Notation.
As a result, you compare space and time complexity using asymptotic analysis. It compares
two algorithms based on changes in their performance as the input size is increased or
decreased.
Best Case: It is defined as the condition that allows an algorithm to complete statement
execution in the shortest amount of time. In this case, the execution time serves as a lower
bound on the algorithm's time complexity.
Average Case: You add the running times for each possible input combination and take the
average in the average case. Here, the execution time serves as both a lower and upper bound
on the algorithm's time complexity.
Worst Case: It is defined as the condition that allows an algorithm to complete statement
execution in the shortest amount of time possible. In this case, the execution time serves as an
upper bound on the algorithm's time complexity.
1. REPRESENTATION OF ARRAYS
10
Array
Arrays can be declared in various ways in different languages. For better illustration, below
are some language-specific array declarations.
However, the above declaration is static or compile-time memory allocation, which means
that the array element’s memory is allocated when a program is compiled.
Here only a fixed size (i,e. the size that is mentioned in square brackets []) of memory will be
allocated for storage, but don’t you think it will not be the same situation as we know the size
of the array every time, there might be a case where we don’t know the size of the array. If
we declare a larger size and store a lesser number of elements will result in a waste of
memory or either be a case where we declare a lesser size then we won’t get enough memory
to store the rest of the elements. In such cases, static memory allocation is not preferred.
11
● Dynamic programming: Dynamic programming algorithms often use arrays to store
intermediate results of subproblems in order to solve a larger problem.
12
2. Linked list representation
3.LINEAR LIST
Linear Data Structures are a type of data structure in computer science where data elements
are arranged sequentially or linearly. Each element has a previous and next adjacent, except
for the first and last elements.
13
● Order Preservation: The order in which elements are added to the data structure is
preserved. This means that the first element added will be the first one to be accessed or
removed, and the last element added will be the last one to be accessed or removed.
● Fixed or Dynamic Size: Linear data structures can have either fixed or dynamic sizes.
Arrays typically have a fixed size when they are created, while other structures like linked
lists, stacks, and queues can dynamically grow or shrink as elements are added or
removed.
● Efficient Access: Accessing elements within a linear data structure is typically efficient.
For example, arrays offer constant-time access to elements using their index.
Linear data structures are commonly used for organising and manipulating data in a
sequential fashion.
This structure allows for easy and efficient addition or removal of elements without
rearranging the entire data structure, making it a flexible choice for many applications.
This flexibility and efficiency make singly linked lists a popular choice for implementing
other foundational data structures, such as stacks and queues, and for applications like
memory management, where the allocation and deallocation of memory happen
frequently and in varying sizes.
● Data: This part of the node stores the actual data that the list is meant to hold. It
can be any type of data—numbers, characters, or even more complex data
structures.
● Next: This is a pointer (or reference) to the next node in the list. It's how the list
maintains its sequence, by linking each node to the subsequent one.
Head:
The first node in a linked list is called the head. It is the entry point to the list and used
as a reference point to traverse it.
Null:
The last node of a linked list, which points to null, indicating the end of the list.
15
Singly Linked List Operations in Data Structure
Following are the primary operations you can perform on a singly linked list:
1. Insertion Operations
● Insertion at the Beginning: Also known as "Insertion at Head." This operation
involves adding a new node right at the start of the list. The new node then
becomes the head of the list. This operation is quick because it simply includes
pointing the new node to the current head of the list and then updating the head
to this new node, making it a constant time operation, O(1).
● Insertion at the End: Known as "Insertion at Tail." This requires traversing the
entire list to find the last node and then adding the new node after this. The new
node's next pointer is set to null, indicating the end of the list. Since you need to
traverse the entire list, this operation has a time complexity of O(n), where n is
the number of nodes in the list.
● Insertion after a Given Node: If you need to insert a new node after a specified
node, you connect the new node to the list by adjusting the pointers. The new
node points to the next node of the current node, and the current node's next
pointer is updated to point to the new node. This operation generally requires
O(n) in the worst case because you might need to traverse the list to find the
specified node.
2. Deletion Operations
● Deletion at the Beginning: Removing the first node (head) of the list can be
done by simply updating the head to point to the second node. This is also a
constant time operation, O(1).
● Deletion from the Middle or Specific Node: To delete a node after a specific
node, you update the next pointer of the preceding node to skip the node to be
16
deleted and point to the following node. Depending on the position of the node to
delete, this can also involve traversing the list, making it O(n).
● Deletion at the End: Deleting the last node requires traversing the list to find the
second last node and updating its next pointer to null. This operation takes O(n)
time as well.
3. Display Operation
To display or traverse the singly linked list, start from the head and move through each
node until you reach the end. Each node's data is accessed as you traverse, and this
operation has a time complexity of O(n).
4. Search Operation
Searching involves traversing the list from the head and checking each node's data
against what you are searching for. This operation also has a time complexity of O(n) in
the worst case.
● They only allocate memory for nodes that are actually in use, reducing memory
waste compared to pre-allocated data structures like arrays.
17
● Unlike arrays, linked lists don’t reserve memory in advance, which can be more
efficient for certain types of applications where the size of the data structure
fluctuates.
● Each node in a singly linked list requires additional memory for the pointer
alongside the actual data.
● Implementing a singly linked list is more complex than using an array, particularly
when it comes to handling pointers, which can introduce bugs like memory leaks.
A linked list is a linear data structure which can store a collection of "nodes" connected
together via links i.e. pointers. Linked lists nodes are not stored at a contiguous location,
rather they are linked using pointers to the different memory locations. A node consists of the
data value and a pointer to the address of the next node within the linked list.
A linked list is a dynamic linear data structure whose memory size can be allocated or de-
allocated at run time based on the operation insertion or deletion, this helps in using system
memory efficiently. Linked lists can be used to implment various data structures like a stack,
queue, graph, hash maps, etc.
A linked list starts with a head node which points to the first node. Every node consists of
data which holds the actual data (value) associated with the node and a next pointer which
holds the memory address of the next node in the linked list. The last node is called the tail
node in the list which points to null indicating the end of the list.
The basic operations in the linked lists are insertion, deletion, searching, display, and deleting
an element at a given key. These operations are performed on Singly Linked Lists as given
below −
18
● Deletion − Deletes an element at the beginning of the list.
● Display − Displays the complete list.
● Search − Searches an element using the given key.
● Delete − Deletes an element using the given key.
Adding a new node in linked list is a more than one step activity. We shall learn this with
diagrams here. First, create a node using the same structure and find the location where it has
to be inserted.
INSERTION AT BEGINNING
Algorithm
1. START
2. Create a node to store the data
3. Check if the list is empty
4. If the list is empty, add the data to the node and
assign the head pointer to it.
5. If the list is not empty, add the data to a node and link to the
current head. Assign the head to the newly added node.
6. END
Example:
public class Node
{
int data;
Node next;
19
INSERTION AT ENDING
Algorithm
1. START
2. Create a new node and assign the data
3. Find the last node
4. Point the last node to new node
5. END
Example
class Node {
public int data;
public Node next;
20
LINKED LIST - DELETION OPERATION
Deletion is also a more than one step process. We shall learn with pictorial representation.
First, locate the target node to be removed, by using searching algorithms.
The left (previous) node of the target node now should point to the next node of the target
node −
This will remove the link that was pointing to the target node. Now, using the following code,
we will remove what the target node is pointing at.
We need to use the deleted node. We can keep that in memory otherwise we can simply
deallocate memory and wipe off the target node completely.
21
DELETION AT BEGINNING
You can delete either from the beginning, end or from a particular position.
head = head->next;
while(temp->next->next!=NULL){
22
temp = temp->next;
temp->next = NULL;
if(temp->next!=NULL) {
temp = temp->next;
temp->next = temp->next->next;
23
Search an Element on a Linked List
You can search an element on a linked list using a loop using the following steps. We are
finding item on a linked list.
● Make head as the current node.
● Run a loop until the current node is NULL because the last element points to NULL.
● In each iteration, check if the key of the node is equal to item. If it the key matches the item,
return true otherwise return false.
// Search a node
current = current->next;
return false;
24
Doubly Linked List
A doubly linked list is a type of linked list in which each node consists of 3 components:
● *prev - address of the previous node
● data - data item
● *next - address of next node
struct node {
int data;
25
struct node *prev;
Pushing a node to a doubly-linked list is similar to pushing a node to a linked list, but extra
work is required to handle the pointer to the previous node.
New node
26
27