Data Structures: Types and Applications
Data Structures: Types and Applications
Data Structure
and its
applications
Types of Data
Structures
▶ There are two broad types
of data structure based on
their memory allocation:
▶ Static data structure
▶ Dynamic data structure
Static Data
Structures
▶ Are data structures that are
defined & allocated before
execution, thus the size cannot
be changed during time of
execution.
Example:
Array implementation of
ADTs.
Dynamic Data
Structure
▶ Are data structure that can
grow and shrink in size or
permits discarding of
unwanted memory during
execution time.
Example:
Linked list implementation of
ADTs.
Structure
▶ Is a collection of data items and the data
items can be of different data type.
struct student {
char
name[20];
int
age;
char
Dept[20];
};
▶ The struct keyword creates a new
user defined data type that is used
to declare variable of an aggregated
Accessing Members of Structure Variables
Example:
struct student stud;
struct student *studptr;
cout<<[Link];
OR
cout<<studptr->name; 7
Linked
▶
List
Is self-referential structure.
▶ Is a collection of elements called nodes,
each of which stores two types of fields. Data
items and a pointer to next node.
The data field: holds the actual elements on
the list.
The pointer field: contains the address of the
next node in the list.
Variations of Linked
Lists
1. Single linked lists: is the simplest type of
linked list in which every node contains some
data and a pointer to the next node of the same
data It contain two "buckets" in one node; one
type.
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.
L;
Adding a
node
Steps
to the
list
1. Allocate a new node.
2. Set the node data values and make
new node point to NULL.
▶Break pointer
connection
▶Re-connect the
pointers
Assignment
Write full implementation for doubly linked lists and
Circular lists.
Your implementation should support the following
operations
Adding element/node
At the beginning
At the end
At the middle/specific location
Deleting data/node
From front
From end
From middle
Displaying the list elements
Stacks & its
Applications
Introduction
Stack is a data structure provides
temporary storage in such a way that the
element stored last will be retrieved first.
All deletions and insertions occur at
one end of the stack known as the
TOP.
Data going into the stack first, leaves
out last.
Stacks are also known as LIFO data
structures (Last-In, First-Out).
Basic Stack
Operations
Push() – Adds an item to the top
of a stack.
39
Here are some examples of infix and the
corresponding postfix expressions:
infix postfix
(A+B*C) ABC*+
(A*(B+C)/D-E) ABC+*D/E-
(A+B*(C-D*(E-F)-G*H)-I*3) ABCDEF-*-
GH*-*+I3*-
(A+B*C/D*E-F) ABC*D/E*+F-
(A+B+C*D-E*F*G)
AB+CD*+EF*G*-
(A+(B-(C+(D-(E+F))))) ABCDEF+-+-+
(A*(B+(C*(D+(E*(F+G))))))
ABCDEFG+*+*+*
Exercise
Evaluate the expression 2 3 4 + * 5 *
= 70
if operand is encountered push it into stack
53+62/*35*+
if operator is encountered pop 2 operands from
stack and perform arithmetic.
A top element
B next to top element
Result =B operator A
Push result on to stack
Return to the top of the stack
Queue
▶ Many times, we use a list in a way where we
always add to the end, and always remove from
the front.
▶ The first element put into the list will be
the first element we take out of the list:
First-In, First-Out ("FIFO")
▶ Queue is a more restricted List with the
following constraints:
o Elements are stored by order of insertion from
front to back.
o Items can only be added to the back of the
queue.
o Only the front element can be accessed or
removed.
Queue … (continued)
Operations on a queue
▶ Offer or enqueue: add an element to the back.
▶ Remove or dequeue: remove and return the element at
the
front.
▶ peek: return (but not remove) front element:
▶ peek on an empty queue returns null.
▶ Other operations: isEmpty, size.
Queue features
▶ ORDERING: maintains order elements were
added (new elements are added to the end
by default).
Queue cont.……
OPERATIONS:
▶ Add element to end of list ('offer'
▶
or 'enqueue').
▶ Remove element from beginning of
list ('remove' or 'dequeue')
examine element at beginning of
list ('peek').
▶ Clear all elements.
▶ is empty, get size.
The Queue
Operations
▶A queue is like a line of people waiting for a
bank teller. The queue has a front and a
rear.
$ $
Front
Rear
The Queue Operations
Front
Rear
The Queue
Operations
▶ When an item is taken from the
queue, it always comes from
the front. it is usually called a
dequeue operation.
$ $
Front
Rear
Array
Implementation of
Queue
▶ A queue can be implemented with an
array, as shown here. For example,
this queue contains the integers 4 (at
the front), 8 and 6 (at the rear).
An array of integers
to implement a We don't care what's in
queue of integers this part of the array.
1. Simple Array Implementation of
Queue
▶ The easiest implementation
also keeps track of the
number of items in the queue
(Queue Size) and the index of
3 size
the first element (at the front
of the queue), the last element
(at the rear). 0 first
2 last
1 first
2 last
[0] [1] [2] [4] [5]
[3] ...
4 8 6
Front Rear
An Enqueue
Operation
▶ When an element enters the
queue, size is incremented,
3 size
and last changes, too.
1 first
3 last
0 last
[0] [1] [2] [3] [4]
[5]
4 2 6 1
14
Rear
Front
Linked List
Implementation
▶A queue can also be implemented
with a linked list with both a head
(start) and a tail (end) pointer.
▶Enqueue:- is inserting a node at
the end of a linked list.
▶Dequeue:- is deleting the first
node in a linked list.
13 10 15
null
head_ptr
tail_ptr
Types of
Queue
Deque (pronounced as
Deck)
▶ Is a Double Ended Queue.
▶Insertion and deletion can occur at either
end.
▶ Has the following basic operations:
EnqueueFront:– inserts data at the front
of a list.
DequeueFront:– deletes data at the front
of a list. EnqueueRear:– inserts data at
the end of a list.
DequeueRear:– deletes data at the end
of a list.
▶ Implementation is similar to
that of queue.
▶ Is best implemented using doubly
linked list.
Front Rear
▶Dequeue()- deletes
Meron
Abebe Alemu Belay Kedir Yonas
Male Male Male Male Male
▶ Now the queue has data having equal
priority and dequeue operation deletes
the front element like in the case of
ordinary queues.
▶Dequeue():- deletes
Abebe
Aster Meron
Female Female
EnqueuePriorityQueue(DequeueMalesQueue());
Application of
Queues
1. Access to shared resources
(Example: printer)
Print()
{
EnqueuePrintQueue(Document)
}
EndOfPrint()
{
DequeuePrintQueue()
}
Application of Queues
(...Continued)
C D H I J
K L M
Root: a node with out a parent.
A
Internal node: a node with at least one
child.
A, B, F, I, J
External (leaf) node: a node without a
child.
C, D, E, H, K, L, M, G
Ancestors of a node: parent, grandparent,
grand- grandparent, etc of a node.
Ancestors of K A, F, I
Descendants of a node: children, grandchildren,
grand-grandchildren etc of a node.
Descendants of F H, I, J, K, L, M
B E F G
H I J
C D H I J
K L M
K L M
Binary tree: a tree in which each node
has at most two children called left
child and right child.
Binary Tree …(continued)
Full binary tree: a binary tree
where each node has either 0 or 2
children.
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.
Complete binary tree: a binary tree in
which the length from the root to any leaf
node is either h or h-1.
where h is the height of the tree.
The deepest level should also be filled
from left to right.
Binary search tree (ordered
binary
tree)
A binary tree that may be empty, but if
it is not empty it satisfies the following.
10
6 15
4 8 14 18
7 12 16 19
11 13
Exercise
Insertion
Draw Binary search tree by inserting the
following key values from left to right
11,6,8,19,4,10,5,17,43,49,31
Deletion
If the root node is deleted, then how to insert another
node in to the tree.
1. Inorder predecessor(the larger number from left sub
tree)
2. Inorder successor(the smaller number from right sub
tree)
Binary Search Tree …(continued)
RootNodePtr RootNodePtr
InsNodePtr
17
17
Case 2: If there is data in the
tree:
Search the appropriate position.
Insert the node in that position.
RootNodePtr
InsNodePtr RootNodePtr
InsertBST(RootNodePtr, InsNodePtr)
17 10 10
6 15 6 15
4 8 14 4 8 14
18 18
7 12 12
16 19 7 16 19
11 13 13 17
11
Traversi
▶
ng
Binary search tree can be traversed in three ways.
Preorder traversal:10, 6, 4, 8, 7, 15, 14, 12, 11, 13, 18, 16, 17, 19
Inorder traversal:4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17
18,19
Used to display nodes in ascending order.
Postorder traversal:4, 7, 8, 6, 11, 13, 12, 14, 17, 16, 19, 18, 1
10
Exercise
Construct Binary search tree from the given
Preorder and Postorder traversal
Preorder - 20,16,5,18,17,19,60,85,70
Postorder – 5,17,19,18,16,70,85,60,20
Application of binary tree traversal
A * D /
B C E F
3
0
Preorder traversal
[Link] the value in the root (e.g. print the root value).
[Link] the left subtree with a preorder traversal.
[Link] the right subtree with a preorder traversal.
Inorder traversal :- prints the node values in ascending
order:
[Link] the left subtree with an inorder traversal.
[Link] the value in the root (e.g. print the root value).
[Link] the right subtree with an inorder traversal.
Postorder traversal
[Link] the left subtree with a postorder traversal.
[Link] the right subtree with a postorder traversal.
[Link] the value in the root (e.g. print the root value).
Exercise
Find the Preorder, Inorder and
Postorder traversal of the given
binary tree.
Searchin
g
▶ To search a node (whose Num value is X) in
a binary search tree (whose root node is
pointed by RootNodePtr).
10
6 15
4 8 14 18
7
12 16 19
11 13 17
Implementati
on:
int SearchBST (Node *RootNodePtr, int X)
if(RootNodePtr == NULL)
else
return(SearchBST(RootNodePtr Right,
X));
}
Finding Minimum
value in a Binary
Search Tree
▶ We can get the minimum value
from a Binary Search Tree, by
locating the left most node in
the tree.
10
Minimum
6 15
4 8 14 18
7
12 16 19
11 13 17
Implementation:
int findMin(Node
*RootNodePtr)
{ if(RootNodePtr == NULL)
return -1;
else if(RootNodePtr ->Left ==
NULL) return RootNodePtr -
>Num;
else
return findMin(RootNodePtr -
>Left);
}
Finding Maximum
value in a Binary
Search Tree
▶ We can get the maximum value
from a Binary Search Tree, by
locating the right most node
in the tree.
10
6 15
Maximum
4 8 14 18
7 12 16 19
11 13 17
Implementati
on:
int findMax(Node
*RootNodePtr) { if(RootNodePtr
== NULL)
return -1;
else if(RootNodePtr ->Right ==
NULL) return RootNodePtr ->Num;
else
return findMax(RootNodePtr -
>Right);
}
Exercise
M
A Y E
J R H
P Q T