You are on page 1of 17

Spotle.

ai Study Material
Spotle.ai/Learn

Introduction
To
Data
Structures
What Does Data Structure Mean?

Data Structure is a way


of organising data so
that it can be stored,
retrieved and
manipulated easily,
efficiently and concisely.

Img Src: Wikihow

Spotle.ai Study Material


2
Spotle.ai/Learn
Why Is Data Structure Important?

In any large program you


will be dealing with a good
amount of data like student
records, bank transactions
and so on. How well you
organise data determines
how efficiently you:

• Add new data item


• Retrieve existing item
• Search for a data item
• Delete a data item
Img Src: Youtube

Spotle.ai Study Material


3
Spotle.ai/Learn
The efficiency of a computer program
depends on the efficiency of the underlying
data structure and algorithm.

Data Structure

Program

Algorithm

Spotle.ai Study Material


4
Spotle.ai/Learn
The Data Structure Hierarchy
Data
Structure

Non-primitive
Primitive Data
Data
Structure Structure

Character Float Integer Linear Non Linear

Array Linked List Stack Queue Graph Tree

Primitive Data Structures are the basic data structures that directly operate upon
the machine instructions. Non-primitive data structures are complex data structures
derived from the primitive data structures.

Spotle.ai Study Material


5
Spotle.ai/Learn
Arrays – The Simplest And Most Common
Non-Primitive Data Structure

An array is a collection of Tina Didi

homogeneous data elements Guddu

stored in successive Rohit

memory locations. Nisha

Good for storing unordered Bobby Aunty

lists – for example managing Chintu

a list of invitees to your Tony

birthday party.
List of invitees to a party stored as an array

Spotle.ai Study Material


6
Spotle.ai/Learn
Arrays Can Be Too Small Or Too Large For
Your Input Data Set
Arrays are fixed size data Tina Didi

structures so you have to know Guddu

upfront how many elements you Rohit

need to work with. Say you Nisha

declared an array of size 7 and Bobby Aunty

realised you now have 10 invitees Chintu

– your program will fail at run- Tony

time! Or you declared an array of


10 and have 7 invitees –you end
up wasting memory space.

Spotle.ai Study Material


7
Spotle.ai/Learn
Inserting Into Or Deleting From An Array Is
Difficult

Inserting or deleting element from Aman


an array is also difficult. If you Bairam
have an alphabetically sorted list Charlie
of student names as an array how Duggu
Harry
do you insert the name of a new Mariam
student? In this example to insert Nisha
Harry’s name, you have to move all Tony
names from Mariam downwards.

Spotle.ai Study Material


8
Spotle.ai/Learn
Linked Lists

A linked list is a linear data structure where the elements are not
stored at contiguous location; the elements are linked using
pointers.

Each node of a list has 2 components - the data value and a


pointer to the next node. The last node has a pointer to null. If we
implement the alphabetically sorted list of student names as a
linked list this is what it will look like:

Aman Bairam Charlie Duggu Mariam Nisha Tony

Spotle.ai Study Material


9
Spotle.ai/Learn
Linked Lists - Advantages
• A linked list can grow and shrink at run time so you do not have to guess the
size upfront.
• Memory is allocated at run-time so there is no memory wastage. You create
as many nodes as size of input elements at run time.
• Insertion and deletion of nodes work simply. Unlike array you don’t have to
shift elements after inserting or deleting an element. You just have to update
the pointer in the preceding node of the element being added or deleted as
you can see with the sorted student list example.

Aman Bairam Charlie Duggu Mariam Nisha Tony

How do you insert Harry?

Aman Bairam Charlie Duggu Harry Mariam Nisha Tony

Spotle.ai Study Material


10
Spotle.ai/Learn
Stacks

• A stack is a linear data


structure similar to a real stack
or pile, Like with a pile,
insertion and deletion of items
takes place at one end called
top of the stack.
• A stack is used in calling
functions, evaluating
expressions and during depth
first search.
• A stack allows two principal
operations push – to push an
element to the stack and pop
to retrieve top most element of
stack

Spotle.ai Study Material


11
Spotle.ai/Learn
Queues
Queues in Data Structure mimic queues in real-life. A queue is open at both
ends. You can insert data (enqueue) on one end (where the queue starts) and
remove data (dequeue) from the other end.

Queue follows First-In-First-Out methodology, that is the data item stored first will
be accessed / removed first. Queues are used by processors to schedule multiple
tasks.

Spotle.ai Study Material


12
Spotle.ai/Learn
Graphs
Graphs are non-linear structures that represent pair-wise relationships between
objects. A graph is made up of nodes or vertices connected by lines or edges.

Nodes: Nodes are entities whose relationships are expressed using edges.

Edges: Edges represent the relationships between nodes in a graph. An edge


between two nodes denotes a one-way or two-way relationship between the
nodes.

Spotle.ai Study Material


13
Spotle.ai/Learn
The Airlines Route Of Air India Expressed
As A Graph

Here the nodes denote


airports and the edges
denote routes
between two airports.

Spotle.ai Study Material


14
Spotle.ai/Learn
Trees
A tree data structure simulates a
hierarchical tree structure, with a root value
and subtrees of children. The first node of the
tree is called the root.

Simply put, a tree consists of nodes


connected by edges with the additional
constraint that
no reference is duplicated, and none points
to the root. The nodes with no child nodes
are called leaves.

A binary tree is a special type of tree data


structure in which each node has at most two
children.

Spotle.ai Study Material


15
Spotle.ai/Learn
Applications Of Trees

Binary Search Trees are used to


check whether an element is
present in a set or not.

A modified tree data structure is


used in modern routers to store
routing information.

Databases use tree structures to


hierarchically store data.

Compilers use a syntax tree to


validate the syntax of programs.

Spotle.ai Study Material


16
Spotle.ai/Learn
Problem Solution Approach In Computer
Science

Understand Problem Modularize Problem Evaluate Algorithms

Choose Most Optimal


Evaluate Data
Convert To Code Algorithm And Data Structures
Structure

Spotle.ai Study Material


17
Spotle.ai/Learn

You might also like