Data Structure Using in Python
Data Structure Using in Python
Data Structure Using in Python
Abstract Data type (ADT) is a type (or class) for objects whose
behavior is defined by a set of values and a set of operations. The
definition of ADT only mentions what operations are to be performed
but not how these operations will be implemented. It does not specify
how data will be organized in memory and what algorithms will be
used for implementing the operations. It is called “abstract” because
it gives an implementation-independent view.
3
● Abstract Data Type (ADT): It’s like a box where you can store data
and perform operations on it, but you can’t see how it’s done
inside.
● Interface: This is the part where you, as the user, interact with the
ADT. You can use the public functions provided to work with the
data.
● Public Functions: These are the operations that you’re allowed to
use, like adding or removing data.
● Private Functions: These are hidden operations that help the ADT
work properly, but you don’t need to worry about them.
● Data Structures: Inside the ADT, data is organized in a certain way,
like in an array or a linked list, which are types of lists with
different properties.
● Memory: This is where all the data in the ADT is actually stored.
1. List ADT
Implementation:
5
Output:
6
Stack ADT:
A Stack ADT is a collection where elements are added and removed from
one end, called the top. Instead of storing data directly in each node, a
pointer to the data is stored. The stack has a head node that contains a
pointer to the top element and a count of the number of entries.
Stack Implementation:
Output:
8
Queue ADT
● The queue abstract data type (ADT) follows the basic design
of the stack abstract data type.
● Each node contains a void pointer to the data and the link
pointer to the next element in the queue. The program’s
responsibility is to allocate memory for storing the data.
● enqueue() – Insert an element at the end of the queue.
● dequeue() – Remove and return the first element of the
queue, if the queue is not empty.
● peek() – Return the element of the queue without removing it,
if the queue is not empty.
● size() – Return the number of elements in the queue.
● isEmpty() – Return true if the queue is empty, otherwise return
false.
● isFull() – Return true if the queue is full, otherwise return false.
● front() - This operation returns the element at the front
end without removing it.
● rear() - This operation returns the element at the rear
end without removing it.
9
Queue implementation
class
Queue:
"""
Define the queue data structure.
"""
def __init__(self):
self.front = None
self.rear = None
self.size = 0
"""
"""
Check if the queue is empty
"""
def isEmpty(self):
return self.size == 0
"""
10
"""
Return the rear element of the
queue.
"""
def getRear(self):
if not self.rear:
return None
return self.rear.data
2. dequeue(): pop the front element from the queue and return it.
12
1. BAGS
A bag object is an unordered collection of zero or more elements
of some type.
Bags (Multisets) A bag, or multiset, is a collection of elements
where the order is immaterial but, unlike a set, duplicate elements
do matter.
Example
14
Applications of BAGS
2. Iterators
15
An iterator is an object that can be iterated upon, meaning that you can
traverse through all the values.
Iterator VS Iterable
Implementation :
Output:
2.Code:
Create an iterator that returns numbers, starting with 1, and each sequence
will increase by one (returning 1,2,3,4,5 etc.):
Output:
17
Applications of Iterators:
Data Streaming:
Lazy Evaluation:
Search Algorithms:
Pipeline Processing:
Sparse Matrix
If most of the elements of the matrix have 0 value, then it is
called a sparse matrix.
Example:
00304
00570
00000
02600
1. Array representation
2. Linked list representation
Implementation:
20
Output:
Implementation:
22
Spiral Matrix:
Example:
Input:
r = 4, c = 4
{5, 6, 7, 8},
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
23
Implementation:
Output:
24
Symmetric Matrix:
Statistics
In statistics and multivariate statistics, symmetric matrices can be used
to organize data, which simplifies how statistical formulas are
expressed, or presents relevant data values about an object. Symmetric
matrices may be applied for statistical problems covering data
correlation, covariance, probability or regression analysis.
26
If you remember your high school basic math, you’ll probably recall
mathematical set operations like union, intersection, difference, and
symmetric difference. Now, the interesting part is we can do the same
thing with Python sets.
Set Union
The union of two sets is the set that contains all of the elements from
both sets without duplicates. To find the union of two Python sets, use
the union() method or the | syntax.
Python
# Perform union operation using the `|` operator or the `union()` method
print(union_set)
Output:
Explanation:
In this example, we have two sets set1 and set2. We can perform a
union operation using either the | operator or the union() method,
which combines all the elements from both sets and removes any
duplicates. The resulting union_set contains all elements from set1
and set2.
28
Set Intersection
The intersection of two sets is the set that contains all of the
common elements of both sets. To find the intersection of two Python
sets, we can either use the intersection() method or & operator.
Python
print(intersection_set)
Output:
{33}
29
Explanation:
In this example, we have two sets set1 and set2. We can perform an
intersection operation using either the & operator or the
intersection() method, which returns a new set that contains only the
elements that are common to both set1 and set2. The resulting
intersection_set contains only element 33, which is present in both
set1 and set2.
Set Difference
The difference between the two sets is the set of all the elements
in the first set that are not present in the second set. To
accomplish this, we can either use the difference() function or
the – operator in python.
Python
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
set3 = set1.difference(set2)
print(set3)
30
print(set4)
Output:
{1, 2}
{1, 2}
Explanation: In this example, we have two sets set1 and set2 with
some common elements. We want to get the elements that are
present in set1 but not in set2. We can achieve this by using the
difference() method or the – operator.
Python
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
set3 = set1.symmetric_difference(set2)
print(set3)
print(set4)
Output:
{1, 2, 6, 7}
{1, 2, 6, 7}
Explanation: In this example, we have two sets set1 and set2 with
some common elements. We want to get the elements that are
32
present in either of the sets, but not in both. We can achieve this
by using the symmetric_difference() method or the ^ operator.