Introducution To Data Structure and Algorithm
Introducution To Data Structure and Algorithm
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.
A data structure is not only used for organizing the data. It is also used for processing,
retrieving, and storing data. There are different basic and advanced types of data
structures that are used in almost every program or software system that has been
developed. So we must have good knowledge of data structures.
Data structures are an integral part of computers used for the arrangement of data in
memory. They are essential and responsible for organizing, processing, accessing, and
storing data efficiently. But this is not all. Various types of data structures have their own
characteristics, features, applications, advantages, and disadvantages. So how do you
identify a data structure that is suitable for a particular task? What is meant by the term
‘Data Structure’? How many types of data structures are there and what are they used for?
This course covers everything about what data structure is, what are the types of data
structures, the classification of data structures, the applications of each data structure,
and so on.
We already have learned about data structure. Many times, what happens is that people
get confused between data type and data structure. So let’s see a few differences between
data type and data structure to make it clear.
The data type is the form of a variable Data structure is a collection of different
to which a value can be assigned. It kinds of data. That entire data can be
defines that the particular variable will represented using an object and can be used
1
Data Type Data Structure
It can hold value but not data. It can hold multiple types of data within a
Therefore, it is dataless. single object.
There is no time complexity in the case In data structure objects, time complexity
of data types. plays an important role.
Data type examples are int, float, Data structure examples are stack, queue,
double, etc. tree, etc.
Data structures are an essential concept in computer science and programming. Here are
some reasons why they are important:
2
Importance of Data Structure
1. Efficient data processing: Data structures provide a way to organize and store data
in a way that allows for efficient retrieval, manipulation, and storage of data. For
example, using a hash table to store data can provide constant-time access to data.
2. Memory management: Proper use of data structures can help to reduce memory
usage and optimize the use of resources. For example, using dynamic arrays can
allow for more efficient use of memory than using static arrays.
3. Code reusability: Data structures can be used as building blocks in various
algorithms and programs, making it easier to reuse code.
4. Abstraction: Data structures provide a level of abstraction that allows programmers
to focus on the logical structure of the data and the operations that can be
performed on it, rather than on the details of how the data is stored and
manipulated.
5. Algorithm design: Many algorithms rely on specific data structures to operate
efficiently. Understanding data structures is crucial for designing and implementing
efficient algorithms.
6. Optimization of algorithms: Data structures help optimize algorithms by enabling
efficient data access and manipulation. For example, a binary search tree allows fast
searching and insertion of elements, making it ideal for implementing searching and
sorting algorithms.
7. Scalability: Data structures enable programs to handle large amounts of data
effectively. For example, a hash table can store large amounts of data while
providing fast access to elements based on their key.
Overall, data structures are essential for managing and manipulating data in an efficient
and effective way. They are a fundamental concept in computer science and are used
extensively in programming and software development.
The structure of the data and the synthesis of the algorithm are relative to each other.
Data presentation must be easy to understand so the developer, as well as the user, can
make an efficient implementation of the operation.
3
short period. Let’s look at different data structures that are used in different situations.
Data structures can be classified into two main types: primitive data structures and non-
primitive data structures.
1. Primitive data structures: These are the most basic data structures and are usually
built into programming languages. Examples include; Integer, Float, Character,
Boolean, Double, Void.
2. Non-primitive data structures: These are complex data structures that are built
using primitive data types. Non-primitive data structures can be further categorized
into the following types:
Arrays: A collection of elements of the same data type, stored in contiguous memory
locations.
Linked lists: A collection of elements that are connected by links or pointers.
Stacks: A collection of elements that follow the Last-In-First-Out (LIFO) principle.
Queues: A collection of elements that follow the First-In-First-Out (FIFO) principle.
Trees: A hierarchical data structure consisting of nodes connected by edges.
Graphs: A non-linear data structure consisting of nodes and edges.
Hash tables: A data structure that stores data in an associative manner using a
hash function.
Heaps: A specialized tree-based data structure that satisfies the heap property.
Tries: A tree-like data structure used to store associative arrays where the keys are
strings.
Sets: A collection of unique elements.
Maps: An abstract data type that stores key-value pairs.
The choice of data structure depends on the problem to be solved and the operations to be
performed on the data. Different data structures have different strengths and weaknesses
and are suitable for different scenarios. Understanding the different types of data
structures and their characteristics is important for efficient algorithm design and
implementation.
1. Linear Data Structure
2. Non-Linear Data Structure.
1. Linear Data Structure:
4
A linear data structure is a type of data structure in which data elements are
arranged in a sequential order, and each element has a unique predecessor and
successor, except for the first and last elements. Linear data structures are one-
dimensional and can be traversed sequentially from the first to the last element.
Elements are arranged in one dimension, also known as linear dimension.
Example: lists, stack, queue, etc.
Static data structure: Static data structure has a fixed memory size. It is
easier to access the elements in a static data structure.
An example of this data structure is an array.
Dynamic data structure: In the dynamic data structure, the size is not fixed.
It can be randomly updated during the runtime which may be considered
efficient concerning the memory (space) complexity of the code.
Examples of this data structure are queue, stack, etc.
5
Need Of Data Structure:
The structure of the data and the synthesis of the algorithm are relative to each other.
Data presentation must be easy to understand so the developer, as well as the user, can
make an efficient implementation of the operation.
Data structures provide an easy way of organizing, retrieving, managing, and storing data.
Here is a list of the needs for data.
ARRAYS
Characteristics of an Array:
7
An array has various characteristics which are as follows:
Arrays use an index-based data structure which helps to identify each of the
elements in an array easily using the index.
If a user wants to store multiple values of the same data type, then the array can be
utilized efficiently.
An array can also handle complex data structures by storing data in a two-
dimensional array.
An array is also used to implement other data structures like Stacks, Queues, Heaps,
Hash tables, etc.
Searching for elements: Arrays can be searched for a specific element using linear
search or binary search algorithms.
Inserting elements: Elements can be inserted into an array at a specific location, but
this operation can be time-consuming because it requires shifting existing elements
in the array.
Deleting elements: Elements can be deleted from an array by shifting the elements
that come after it to fill the gap.
8
Traversing elements: The elements in an array can be traversed in order, visiting
each element once.
These are some of the most common operations performed on arrays. The specific
operations and algorithms used may vary based on the requirements of the problem and
the programming language used.
• The sequential search (also called the linear search) is the simplest search algorithm.
• It simply examines each element sequentially, starting with the first element, until
it finds the key element, or it reaches the end of the array.
• If you were looking for someone on a moving passenger train, you would use a
sequential search.
3. If si = x, return i .
4. Return –1.
The binary search is the standard algorithm for searching through a sorted sequence. It is
much more efficient than the sequential search, but it does require that the elements be in
order. It repeatedly divides the sequence in two, each time restricting the search to the half
that would contain the element. You might use the binary search to look up a word in a
dictionary.
9
(Precondition: s = {s0, s1, . . ., sn–1} is a sorted sequence of n values of the same type as x.)
6. If si < x, repeat steps 2–7 on the subsequence that lies above si.
Applications of Array:
It is also used to implement other data structures like Stacks, Queues, Heaps, Hash
tables, etc.
Arrays can be used in speech processing where every speech signal is an array.
The array is used in many management systems like a library, students, parliament,
etc.
10
The array is used in the online ticket booking system. Contacts on a cell phone are
displayed by this array.
In games like online chess, where the player can store his past moves as well as
current moves. It indicates a hint of position.
LINKED LIST
A linked list is a linear data structure in which elements are not stored at contiguous
memory locations. The elements in a linked list are linked using pointers as shown in the
below image:
Singly-linked list
11
Linked List
During the initialization of the linked list, there is no need to know the size of the
elements.
Each node of the linked list consists of a pointer/link which is the address of the
next node.
A linked list is a linear data structure where each node contains a value and a reference to
the next node. Here are some common operations performed on linked lists:
Initialization: A linked list can be initialized by creating a head node with a reference
to the first node. Each subsequent node contains a value and a reference to the next
node.
Deleting elements: Elements can be deleted from the linked list by updating the
reference of the previous node to point to the next node, effectively removing the
current node from the list.
Searching for elements: Linked lists can be searched for a specific element by
12
starting from the head node and following the references to the next nodes until the
desired element is found.
Updating elements: Elements in a linked list can be updated by modifying the value
of a specific node.
Traversing elements: The elements in a linked list can be traversed by starting from
the head node and following the references to the next nodes until the end of the list
is reached.
Reversing a linked list: The linked list can be reversed by updating the references of
each node so that they point to the previous node instead of the next node.
These are some of the most common operations performed on linked lists. The specific
operations and algorithms used may vary based on the requirements of the problem and
the programming language used.
Linked lists are used to display image containers. Users can visit past, current, and
next images.
13
Linked are used in software development where they indicate the correct syntax of a
tag.
It is used in image viewer. The previous and next images are linked, and hence can be
accessed by the previous and next buttons.
In a music playlist, songs are linked to the previous and next songs.
STACK
Stack is a linear data structure that follows a particular order in which the operations are
performed. The order is LIFO(Last in first out). Entering and retrieving data is possible from
only one end. The entering and retrieving of data is also called push and pop operation in a
stack. There are different operations possible in a stack like reversing a stack using
recursion, Sorting, Deleting the middle element of a stack, etc.
14
Characteristics of a Stack:
Stack is used in many different algorithms like Tower of Hanoi, tree traversal,
recursion, etc.
It follows the Last In First Out operation i.e., an element that is inserted first will pop
in last and vice versa.
The insertion and deletion are performed at one end i.e. from the top of the stack.
In stack, if the allocated space for the stack is full, and still anyone attempts to add
more elements, it will lead to stack overflow.
Applications of Stack:
The stack data structure is used in the evaluation and conversion of arithmetic
expressions.
The stack is used to perform undo as well as redo operations in word processors.
The stack is used in the media players. Useful to play the next and previous song.
15
The stack is used in recursion operations.
A stack is a linear data structure that implements the Last-In-First-Out (LIFO) principle.
Here are some common operations performed on stacks:
Push: Elements can be pushed onto the top of the stack, adding a new element to
the top of the stack.
Pop: The top element can be removed from the stack by performing a pop operation,
effectively removing the last element that was pushed onto the stack.
Peek: The top element can be inspected without removing it from the stack using a
peek operation.
Size: The number of elements in the stack can be determined using a size operation.
These are some of the most common operations performed on stacks. The specific
operations and algorithms used may vary based on the requirements of the problem and
the programming language used. Stacks are commonly used in applications such as
evaluating expressions, implementing function call stacks in computer programs, and many
others.
Real life example of a stack is the layer of eating plates arranged one above the
other. When you remove a plate from the pile, you can take the plate to the top of
the pile. But this is exactly the plate that was added most recently to the pile. If you
want the plate at the bottom of the pile, you must remove all the plates on top of it
to reach it.
Browsers use stack data structures to keep track of previously visited sites.
16
QUEUE
Queue is a linear data structure that follows a particular order in which the operations are
performed. The order is First In First Out(FIFO) i.e. the data item stored first will be
accessed first. In this, entering and retrieving data is not done from only one end. An
example of a queue is any queue of consumers for a resource where the consumer that
came first is served first. Different operations are performed on a Queue like Reversing a
Queue (with or without using recursion), Reversing the first K elements of a Queue, etc. A
few basic operations performed In Queue are enqueue, dequeue, front, rear, etc.
Characteristics of a Queue:
To remove the last element of the Queue, all the elements inserted before the new
element in the queue must be removed.
Applications of Queue:
17
Different applications of Queue are as follows:
It helps in serving requests on a single shared resource, like a printer, CPU task
scheduling, etc.
It is used in the asynchronous transfer of data e.g. pipes, file IO, and sockets.
A queue is a linear data structure that implements the First-In-First-Out (FIFO) principle.
Here are some common operations performed on queues:
Enqueue: Elements can be added to the back of the queue, adding a new element to
the end of the queue.
Dequeue: The front element can be removed from the queue by performing a
dequeue operation, effectively removing the first element that was added to the
queue.
Peek: The front element can be inspected without removing it from the queue using
a peek operation.
Size: The number of elements in the queue can be determined using a size operation.
18
These are some of the most common operations performed on queues. The specific
operations and algorithms used may vary based on the requirements of the problem and
the programming language used. Queues are commonly used in applications such as
scheduling tasks, managing communication between processes, and many others.
A real-world example of a queue is a single-lane one-way road, where the vehicle that
enters first will exit first.
A more real-world example can be seen in the queue at the ticket windows.
People on an escalator
TREE
A tree is a non-linear and hierarchal data structure where the elements are arranged in a
tree-like structure. In a tree, the topmost node is called the root node. Each node contains
some data, and data can be of any type. It consists of a central node, structural nodes, and
sub-nodes which are connected via edges. Different tree data structures allow quicker and
easier access to the data as it is a non-linear data structure. A tree has various
terminologies like Node, Root, Edge, Height of a tree, Degree of a tree, etc.
Binary Tree
AVL Tree
B-Tree, etc
19
Tree
Characteristics of a Tree:
In a tree, the Height of the root can be defined as the longest path from the root
node to the leaf node.
In a tree, one can also calculate the depth from the top to any node. The root node
has a depth of 0.
Applications of Tree:
20
Heap is a tree data structure that is implemented using arrays and used to
implement priority queues.
K-D Tree is a space partitioning tree used to organize points in K-dimensional space.
A tree is a non-linear data structure that consists of nodes connected by edges. Here are
some common operations performed on trees:
Insertion: New nodes can be added to the tree to create a new branch or to increase
the height of the tree.
Deletion: Nodes can be removed from the tree by updating the references of the
parent node to remove the reference to the current node.
Search: Elements can be searched for in a tree by starting from the root node and
traversing the tree based on the value of the current node until the desired node is
found.
Height: The height of the tree can be determined by counting the number of edges
from the root node to the furthest leaf node.
Depth: The depth of a node can be determined by counting the number of edges
from the root node to the current node.
Balancing: The tree can be balanced to ensure that the height of the tree is
minimized and the distribution of nodes is as even as possible.
These are some of the most common operations performed on trees. The specific operations
21
and algorithms used may vary based on the requirements of the problem and the
programming language used. Trees are commonly used in applications such as searching,
sorting, and storing hierarchical data.
The most common use case of a tree is any social networking site.
GRAPH
A graph is a non-linear data structure that consists of vertices (or nodes) and edges. It
consists of a finite set of vertices and set of edges that connect a pair of nodes. The graph
is used to solve the most challenging and complex programming problems. It has different
terminologies which are Path, Degree, Adjacent vertices, Connected components, etc.
Characteristics of Graph:
22
The graph has various different characteristics which are as follows:
The maximum distance from a vertex to all the other vertices is considered the
Eccentricity of that vertex.
The vertex having minimum Eccentricity is considered the central point of the graph.
The minimum value of Eccentricity from all vertices is considered the radius of a
connected graph.
Applications of Graph:
Also used in the World Wide Web where the web pages represent the nodes.
A graph is a non-linear data structure consisting of nodes and edges. Here are some
common operations performed on graphs:
Add Vertex: New vertices can be added to the graph to represent a new node.
Add Edge: Edges can be added between vertices to represent a relationship between
nodes.
Remove Vertex: Vertices can be removed from the graph by updating the references
of adjacent vertices to remove the reference to the current vertex.
Remove Edge: Edges can be removed by updating the references of the adjacent
vertices to remove the reference to the current edge.
Shortest Path: The shortest path between two vertices can be determined using
algorithms such as Dijkstra’s algorithm or A* algorithm.
Cycle Detection: Cycles in a graph can be detected by checking for back edges
during a depth-first search.
These are some of the most common operations performed on graphs. The specific
operations and algorithms used may vary based on the requirements of the problem and
the programming language used. Graphs are commonly used in applications such as
computer networks, social networks, and routing problems.
One of the most common real-world examples of a graph is Google Maps where cities
are located as vertices and paths connecting those vertices are located as edges of
the graph.
A social network is also one real-world example of a graph where every person on the
network is a node, and all of their friendships on the network are the edges of the
graph.
24