2. Outline
Classification of Data Structure
Stacks
Linked lists
Queues
Trees
Graphs
10/20/2023 2
•COSC 709: Natural Language Processing
3. DS&A and Program
As we know that an algorithm is a step by step procedure to solve a particular function or
problem
Program = algorithm + Data Structure
Data Structure = Operation + Organized Data
Storage structure is the representation of particular DS in the memory of the computer.
That means, algorithm is a set of instruction written to carry out certain tasks & the data
structure is the way of organizing the data with their logical relationship retained.
To develop a program of an algorithm, we should select an appropriate data structure for that
algorithm.
To convert a formal algorithm to a program we use step wise Refinement Techniques
(mathematical model, pseudo code, DS, C/C++ program etc.).
10/20/2023 Data Structures and Algorithms 3
4. Classification of Data Structure
Data structure
Primitive DS Non-Primitive DS
Integer Float Character Pointer
Float
Integer Float
6. Primitive data structure
Primitive data structure is a fundamental type of data structure
that stores the data of only one type.
The fundamental data types can hold a single type of value
example integer, float, character, pointer type of value.
Non-primitive
In the case of non-primitive data structure, it is categorized into
two parts such as linear data structure and non-linear data
structure.
Linear data structure is a sequential type of data structure we
have different linear data structures holding the sequential
values such as Array, Linked list, Stack, Queue.
10/20/2023 6
Data Structures and Algorithms
7. Static Data Structure vs Dynamic Data Structure
10/20/2023 7
Data Structures and Algorithms
9. Array
Why array????
An array is a linear data structure and it is a collection of items stored at contiguous memory
locations.
The idea is to store multiple items of the same type together in one place. It allows the processing
of a large amount of data in a relatively short period. The first element of the array is indexed by 0.
It cannot contain the elements of different types like integer with character. The commonly used
operation in an array is insertion, deletion, traversing, searching.
For example:
int a[6] = {1,2,3,4,5,6};
10/20/2023 9
Data Structures and Algorithms
11. Characteristics of an Array:
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.
The search process in an array can be done very easily.
10/20/2023 11
•Data Structures and Algorithms
12. Stack Data structure
What is a Stack? Example:- ordered list of Book, CD
A Stack is a linear data structure that follows the LIFO (Last-
In-First-Out) principle.
It is a data structure that follows some order to insert and delete
the elements, and that order can be LIFO or FILO.
In other words, a stack can be defined as a container in which
insertion and deletion can be done from the one end known as
the top of the stack.
Both insertion and deletion only possible at top of the stack.
10/20/2023 12
Data Structures and Algorithms
13. Standard Stack Operations
The following are some common operations implemented on the stack:
push(): When we insert an element in a stack then the operation is known as a
push. If the stack is full then the overflow condition occurs.
pop(): When we delete an element from the stack, the operation is known as a
pop. If the stack is empty means that no element exists in the stack, this state is
known as an underflow state.
peek(): It returns the element at the given position.
isEmpty(): It determines whether the stack is empty or not.
isFull(): It determines whether the stack is full or not.'
10/20/2023 13
Data Structures and Algorithms
14. Working of Stack
Stack works on the LIFO pattern. As we can observe in the below figure there are five memory blocks in the
stack; therefore, the size of the stack is 5.
Suppose we want to store the elements in a stack and let's assume that stack is empty. We have taken the stack
of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full.
In the above case, the value 1 is entered first, so it will be removed only after the deletion of all the other
elements.
10/20/2023 14
Data Structures and Algorithms
15. Logical PUSH operation
10/20/2023 15
Data Structures and Algorithms
The steps involved in the PUSH operation is given below:
Before inserting an element in a stack, we check whether the stack is full.
If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.
When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
When the new element is pushed in a stack, first, the value of the top gets incremented, i.e., top=top+1, and the element
will be placed at the new position of the top.
The elements will be inserted until we reach the max size of the stack.
16. POP operation
The steps involved in the POP operation is given below:
Before deleting the element from the stack, we check whether the stack is empty.
If we try to delete the element from the empty stack, then the underflow condition
occurs.
If the stack is not empty, we first access the element which is pointed by the top.
Once the pop operation is performed, the top is decremented by 1.
10/20/2023 16
Data Structures and Algorithms
17. Inserting on stack(push)
void push()
{
int val
if (top == n - 1)
cout << "Stack Overflow" << endl;
else
{
if (top == - 1)
top= 0;
cout << "Insert an element to push : " << endl;
cin>>val;
top++;
stack[top] = val;
}
}
10/20/2023 17
Data Structures and Algorithms
18. Deleting from stack(pop)
void pop()
{
int item;
if (top == - 1 )
{
cout << "Stack Underflow ";
else
}
item=stack(top)
{
cout << "Element popped off from stack is : " << endl;
top--;
}
}
10/20/2023 18
Data Structures and Algorithms
19. Linked list DS
Linked list is a linear data structure that includes a series of connected nodes. In
which the nodes that are randomly stored in the memory. A node in the linked list
contains two parts, i.e., first is the data part and second is the address part. The last
node of the list contains a pointer to the null.
Representation of a Linked list
Linked list can be represented as the connection of nodes in which each node
points to the next node of the list.
10/20/2023 19
Data Structures and Algorithms
20. How to declare a linked list?
The declaration of linked list is given as follows -
struct node
{
int data;
struct node *next;
}
In the above declaration, we have defined a structure named
as node that contains two variables, one is data that is an
integer type, and another one is next that is a pointer which
contains the address of next node.
10/20/2023 20
Data Structures and Algorithms
21. Types of Linked list
Singly Linked List
It is the most common. Each node has data and a pointer to the next node. It is forward only.
Doubly Linked List
We add a pointer to the previous node in a doubly-linked list. Thus, we can go in either direction: forward or backward.
10/20/2023 21
Data Structures and Algorithms
22. Cont…
Circular Linked List
A circular linked list is a sequence of elements in which each node has a link to the
next node, and the last node is having a link to the first node.
The representation of the circular linked list will be similar to the singly linked list,
as shown below:
A circular linked list can be either singly linked or doubly linked.
10/20/2023 22
Data Structures and Algorithms
23. Representation
Representation of the node in a singly linked list
struct node
{
int data;
struct node *next;
}
Representation of the node in a doubly linked list
struct node
{
int data;
struct node *next;
struct node *prev;
}
10/20/2023 23
Data Structures and Algorithms
24. Queue DS
A Queue is defined as a linear data structure that is open at both ends and the
operations are performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end, and
all deletions from the list are made at the other end. The element which is first pushed
into the order, the operation is first performed on that.
10/20/2023 24
Data Structures and Algorithms
25. Principle of Queue
A Queue is like a line waiting to purchase tickets, where the first person in line is the
first person served. (i.e. First come first serve).
Position of the entry in a queue ready to be served, that is, the first entry that will be
removed from the queue, is called the front of the queue(sometimes, head of the
queue), similarly, the position of the last entry in the queue, that is, the one most
recently added, is called the rear (or the tail) of the queue.
10/20/2023 25
Data Structures and Algorithms
26. Queue Representation:
Like stacks, Queues can also be represented in an array: In this representation, the
Queue is implemented using the array.
Queue: the name of the array storing queue elements.
Front: the index where the first element is stored in the array representing the queue.
Rear: the index where the last element is stored in an array representing the queue.
Basic operation
Enqueue() - Insertion of elements to the queue.
Dequeue() - Removal of elements from the queue.
Rule
Insertion only from one end and deletion from another end.
10/20/2023 26
Data Structures and Algorithms
27. Enqueue() Operation
The following steps should be followed to insert (enqueue) data
element into a queue .
Step 1: Check if the queue is full.
Step 2: If the queue is full, Overflow error.
Step 3: If the queue is not full, increment the rear pointer to point
to the next available empty space.
Step 4: Add the data element to the queue location where the rear
is pointing.
Step 5: Here, you have successfully added.
10/20/2023 27
Data Structures and Algorithms
29. Implementation of queue
int f = - 1, r = - 1, n=3;
To enqueue an element
void enqueue()
{
int val;
if (r == n-1 )
cout <<"Queue Overflow"<<endl;
else
{
if (f == - 1)
f = 0;
cout <<"Insert the element in queue : "<<endl;
cin>>val;
r++;
q[r] = val;
}
}
10/20/2023 29
Data Structures and Algorithms
30. Tree Data Structure
We already discussed a linear data structures like an array, linked list, stack and queue in which all the
elements are arranged in a sequential manner.
A tree is also one of the data structures that represent hierarchical data. Suppose we want to show the
employees and their positions in the hierarchical form then it can be represented as shown below:
10/20/2023 30
Data Structures and Algorithms
31. Tree Terminology
Root: The root node is the topmost node in the tree hierarchy. root node doesn't have any parent.
Child node: If the node is a descendant of any node, then the node is known as a child node.
Parent: If the node contains any sub-node, then that node is said to be the parent of that sub-node.
Sibling: The nodes that have the same parent are known as siblings.
Leaf Node:- The node of the tree, which doesn't have any child node, is called a leaf node.
Internal nodes: A node has at least one child node known as an internal node.
Ancestor node:- An ancestor of a node is any predecessor node on a path from the root to that
node. The root node doesn't have any ancestors. In the tree shown in the above image, nodes 1, 2,
and 5 are the ancestors of node 10.
Descendant: The immediate successor of the given node is known as a descendant of a node. In the
above figure, 10 is the descendant of node 5.
What is a common ancestor of 6 and 7 ?
How many sub trees are there for node 1?
10/20/2023 31
Data Structures and Algorithms
32. Con…
Number of edges: If there are n nodes, then there would n-1 edges. Each arrow in the
structure represents the link or path. Each node, except the root node, will have at least
one incoming link known as an edge. There would be one link for the parent-child
relationship.
Depth of node x: The depth of node x can be defined as the length of the path from the
root to the node x. One edge contributes one-unit length in the path. So, the depth of
node x can also be defined as the number of edges between the root node and the node x.
The root node has 0 depth.
Height of node x: The height of node x can be defined as the longest path from the node
x to the leaf node.
10/20/2023 32
Data Structures and Algorithms
33. Binary Tree
Binary tree: a tree in which each node has at most two children.
10/20/2023 33
Data Structures and Algorithms
Full binary tree: a binary tree where each node has either 0 or 2 children.
34. Cont…
Balanced binary tree: a binary tree where each node except the leaf nodes has
left and right children and all the leaves are at the same level.
10/20/2023 34
Data Structures and Algorithms
35. Binary search tree
Every node has a key and no two elements have the same key.
The keys in the right subtree are larger than the key in the root.
The keys in the left subtree are smaller than the key in the root.
The left and the right subtrees are also binary search trees.
Examples of Binary Search Tree.
10/20/2023 35
Data Structures and Algorithms
36. Traversing (Visiting) in a BST
Binary search tree can be traversed in three ways.
Preorder traversal:- traversing binary tree in the order of parent, left and right.
In order traversal:- traversing binary tree in the order of left, parent and right.
Post order traversal:- traversing binary tree in the order of left, right and parent.
Example:
Preorder traversal: 10, 6, 4, 8, 7, 15, 14, 12, 11, 13, 18, 16, 17, 19
In order traversal: 4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 Used to display nodes in ascending
order.
Post order traversal: 4, 7, 8, 6, 11, 13, 12, 14, 17, 16, 19, 18, 15, 10
10/20/2023 36
Data Structures and Algorithms
37. Implementation of Tree
The tree data structure can be created by creating the nodes dynamically with the help
of the pointers. The tree in the memory can be represented as shown below:
The above figure shows the representation of the tree data structure in the memory. In
the above structure, the node contains three fields. The second field stores the data; the
first field stores the address of the left child, and the third field stores the address of the
right child.
10/20/2023 37
Data Structures and Algorithms
38. Cont…
10/20/2023 38
Data Structures and Algorithms
In programming, the structure of a node can be defined as:
struct node
{
int data;
struct node *left;
struct node *right;
}
39. Graphs in Data Structure
• A graph is a non-linear kind of data structure made up of nodes or vertices and
edges. The edges connect any two nodes in the graph, and the nodes are also
known as vertices.
This graph has a set of vertices V= { 1,2,3,4,5} and a set of edges
E= { (1,2),(1,3),(2,3),(2,4),(2,5),(3,5),(4,5}.
10/20/2023 39
Data Structures and Algorithms
40. Types of Graphs in Data Structures
Finite Graph:-The graph G=(V, E) is called a finite graph if the number of vertices
and edges in the graph is limited in number.
Infinite Graph:- The graph G=(V, E) is called a finite graph but if the number of vertices and edges in
the graph is endless.
10/20/2023 40
Data Structures and Algorithms
41. Cont…
Weighted Graph:- A graph G= (V, E) is called a labeled or weighted graph because each edge has a
value or weight representing the cost of traversing that edge.
Directed Graph:- A directed graph is a set of nodes connected by edges, each with a direction.
10/20/2023 41
Data Structures and Algorithms
42. Cont…
Undirected Graph :- An undirected graph comprises a set of nodes and links
connecting them. The order of the two connected vertices is irrelevant and has no
direction. You can form an undirected graph with a finite number of vertices and
edges.
10/20/2023 42
Data Structures and Algorithms
43. Representation of Graphs in Data Structures
Graphs in data structures are used to represent the relationships between objects. Every graph consists
of a set of points known as vertices or nodes connected by lines known as edges. The vertices in a
network represent entities.
Basically Let us look three representation using matrix
1. Undirected Graph Representation
10/20/2023 43
Data Structures and Algorithms
44. Cont…
2. Directed Graph Representation
3. Weighted Undirected Graph Representation
10/20/2023 44
Data Structures and Algorithms
45. Graph Traversal Algorithm
There are two techniques to implement a graph traversal algorithm:
Breadth-first search(BFS)
Depth-first search(DFS)
BFS:- It begins at the root of the graph and investigates all nodes at the current depth level before moving
on to nodes at the next depth level.
10/20/2023 45
Data Structures and Algorithms
46. Depth-First Search(DFS)
DFS:- The strategy followed by depth-first search is, as its name implies, to search "deeper" in the
graph whenever possible. Depth-first search explores edges out of the most recently discovered vertex
v that still has unexplored edges leaving it.
DFS: 1,4,3,10,9,2,8,7,5,6 Use stack data structure for DFS
10/20/2023 46
Data Structures and Algorithms
47. All about chapter three
10/20/2023 47
Data Structures and Algorithms
Thank You !!!