Data Structures PPTS[1]
Data Structures PPTS[1]
INTRODUCTION TO
DATA
STRUCTURES,
SEARCHING
AND
SORTIN
G
Contents
• Introduction to Data Structures
• Classification and Operations on Data
Structures
• Preliminaries ofAlgorithm
• Algorithm Analysis and Complexity
• RecursiveAlgorithms
• Searching Techniques - Linear, Binary, Fibonacci
• Sorting Techniques- Bubble, Selection,
Insertion, Quick and Merge Sort
• Comparison of SortingAlgorithms 2
Introduction to Data
• AStructures
data structure is a way of storing data in a
computer so that it can be used efficiently and
it will allow the most efficient algorithm to be
used.
• A data structure should be seen as a logical
concept that must address two fundamental
concerns.
I. First, how the data will be stored, and
II. Second, what operations will be performed on it.
3
Classification of Data
Structures
• Data structures can be classified as
i. Simple data structure
ii. Compound data structure
iii. Linear data structure
iv. Non linear data structure
7
Abstract Data
Type
• An abstract data type, sometimes abbreviated
ADT, is a logical description of how we view
the data and the operations that are allowed
without regard to how they will be
implemented.
• By providing this level of abstraction, we are
creating an encapsulation around the data. The
idea is that by encapsulating the details of the
implementation, we are hiding them from the
user‘s view. This is called information hiding.
8
Algorithm
Definition
• An Algorithm may be defined as a finite
sequence of instructions each of which has a clear
meaning and can be performed with a finite
amount of effort in a finite length of time.
• The word algorithm originates from the Arabic
word Algorism which is linked to the name of the
Arabic Mathematician AI Khwarizmi.
• AI Khwarizmi is considered to be the first
algorithm designer for adding numbers.
9
Structure of an
Algorithm
• An algorithm has the following structure:
– Input Step
– Assignment Step
– Decision Step
– Repetitive Step
– Output Step
10
Properties of an Algorithm
• Finiteness:- An algorithm must terminate after
finite number of steps.
• Definiteness:-The steps of the algorithm must
be precisely defined.
• Generality:- An algorithm must be generic
enough to solve all problems of a particular
class.
• Effectiveness:- The operations of the algorithm
must be basic enough to be put down on pencil
and paper.
11
Input-Output:- The algorithm must have certain
initial and precise inputs, and outputs that may be
generated both at its intermediate and final steps
Algorithm Analysis and
Complexity
• The performances of algorithms can be
measured on the scales of Time and Space.
• The Time Complexity of an algorithm or a
program is a function of the running time of
the algorithm or a program.
• The Space Complexity of an algorithm or a
program is a function of the space needed by
the algorithm or program to run to completion.
13
Algorithm Analysis and
•Complexity
The Time Complexity of an algorithm can be computed
either by an
– Empirical or Posteriori Testing
– Theoretical or AprioriApproach
• The Empirical or Posteriori Testing approach calls for
implementing the complete algorithm and executes
them on a computer for various instances of the
problem.
• The Theoretical or Apriori Approach calls for
mathematically determining the resources such as time
and space needed by the algorithm, as a function of
parameter related to the instances of the problem
considered.
14
Algorithm Analysis and
Complexity
• Apriori analysis computed the efficiency of the
program as a function of the total frequency
count of the statements comprising the
program.
• Example: Let us estimate the frequency count
of the statement x = x+2 occurring in the
following three program segments A, B andC.
15
Total Frequency Count of Program Segment A
16
Total Frequency Count of Program Segment B
18
Asymptotic
Notations
• Big oh(O): f(n) = O(g(n)) ( read as f of n is big
oh of g of n), if there exists a positive
integer n0 and a positive number c such that |
f(n)| ≤ c
|g(n)| for all n ≥ n0.
f(n) g(n)
16n3 + 45n2 +12n n3 f(n) = O(n3 )
34n – 40 n f(n) = O(n)
50 1 f(n) = O(1)
22
Time
Complexity
Complexity
Notation Description
Constant O(1) Constant number of operations, not depending on
the input data size.
Logarithmic O(logn) Number of operations proportional of log(n) where n
is the size of the input data.
Linear O(n) Number of operations proportional to the input data
size.
Quadratic O(n2 ) Number of operations proportional to the square of
the size of the input data.
Cubic O(n3 ) Number of operations proportional to the cube of
the size of the input data.
Exponential O(2n) Exponential number of operations, fast growing.
O(kn )
O(n!)
23
Time Complexities of
various Algorithms
24
Recursion
Examples
Factorial Function
Factorial(Fact, N)
1. If N = 0, then set Fact :=1, and
return.
2. Call Factorial(Fact, N-1)
3. Set Fact := N* Fact
4. Return
25
Fibonacci
Sequence
Fibonacci(Fib, N)
1. If N=0 or N=1, then: Set Fib:=N, and
return.
2. Call Fibonacci(FibA, N-2)
3. Call Fibonacci(FibB, N-1)
4. Set Fib:=FibA +FibB
5. Return
26
Towers of
Tower(N, Beg, Hanoi
Aux, End)
1. If N=1, then:
(a) Write: Beg -> End
(b) Return
2. [Move N-1 disks from peg Beg to peg
Aux]
Call Tower(N-1, Beg, End, Aux)
3. Write: Beg -> End
4. [Move N-1 disks from peg Aux to peg
End]
Call Tower(N-1, Aux, Beg, End) 27
Basic Searching Methods
• Search: A search algorithm is a method of
locating a specific item of information in a
larger collection of data.
• There are three primary algorithms used for
searching the contents of an array:
1. Linear or Sequential Search
2. Binary Search
3. Fibonacci Search
28
Linear Search
• Begins search at first item in list, continues
searching sequentially(item by item) through
list, until desired item(key) is found, or until
end of list is reached.
• Also called sequential or serial search.
• Obviously not efficient
an searching method for lists
ordered
directory(which is ordered like phone
alphabetically).
29
Linear Search
contd..
• Advantages
1. Algorithm is simple.
2. List need not be ordered in any
particular way.
• Time Complexity of Linear Search is O(n).
30
Recursive Linear Search
Algorithm
def linear_Search(l,key,index=0):
if l:
if l[0]==key:
return
index
s=linear_Search(l[1:],key,(index+1)) if
s is not false:
return
s return false 31
Binary Search
• List must be in sorted order to begin with
Compare key with middle entry of list
For lists with even number of entries, either
of the two middle entries can be used.
• Three possibilities for result of comparison
Key matches middle entry --- terminate
search with success
Key is greater than middle entry ---
matching entry(if exists) must be in upper
part of list (lower part of list can be
discarded from search)
32
Binary Search contd…
Key is less than middle entry ---
matching entry (if exists) must be in lower
part
upperof part
list ( of list can be discarded
search) from
• Keep
progressively
applying―reduced‖
above lists,
2 until
steps match is
to the
found or until no further list reduction can be
done.
• Time Complexity of Binary Search is O(logn).
33
Fibonacci Search
• Fibonacci Search Technique is a method of
searching a sorted array using a divide and
conquer algorithm that narrows down possible
locations with the aid of Fibonacci numbers.
• Fibonacci search examines locations whose
addresses have lower dispersion, therefore it
has an advantage over binary search in slightly
reducing the average time needed to access a
storage location.
34
Fibonacci Search
contd…
• Fibonacci search has a complexity
O(log(n)). of
• Fibonacci search was first devised by
Kiefer(1953) as a minimax search the
for
maximum (minimum) of a unimodal
function in an interval.
35
Fibonacci Search Algorithm
• Let k be defined as an element in F, the
array of Fibonacci numbers. n = Fm is the
array size. If the array size is not a Fibonacci
number, let Fm be the smallest number in F that
is greater than n.
• The array of Fibonacci numbers is defined
where Fk+2
= Fk+1 + Fk, when k ≥ 0, F1 = 1, and F0 = 0.
36
• To test whether an item is in the list of
ordered numbers, follow these steps:
Set k = m.
If k = 0, stop. There is no match; the item
is not in the array.
Fibonacci Search Algorithm
Contd…
• Compare the item against element in Fk−1.
• If the item matches, stop.
• If the item is less than entry Fk−1, discard
the elements from positions Fk−1 + 1 to n.
Set k = k − 1 and return to step 2.
• If the item is greater than
Fk−1,
entry the elements from positions 1 to
discard
Renumber the remaining
Fk−1.
from 1elements
to Fk−2, set k = k − 2, and return to
step 38
Basic Sorting Methods :-Bubble
• First Level Considerations
Sort
• To sort list of n elements in ascending order
Pass 1 :make nth element the largest
Pass 2 :if needed make n-1th element the 2nd
largest
Pass 3 :if needed make n-2th element the 3rd
largest
Pass n-2: if needed make 3rd n-(n-3)th element
the (n-2)th largest
Pass n-1 :if needed make 2nd n-(n-2)th element
the (n-1)th largest
• Maximum number of passes is (n-1). 39
Bubble Sort
Second Level Considerations
• Pass 1: Make nth element the largest.
Compare each successive pair of
elements
beginning with 1st 2nd and ending with n-
1th nth and swap the elements if necessary.
• Pass 2 : Make n-1th element the 2nd largest.
Compare each successive pair of
elements beginning with 1st 2nd and
ending with n-2th n-1th and swap the elements
if necessary 40
Pass n-1:Make 2nd n-(n-2)th element the (n-1)th
largest.
Compare each successive pair of elements
beginning with 1st 2nd and ending with n-(n-
1) th n-(n-2)th 1st 2nd and swap the elements if
necessary.
List is sorted when either of the following occurs
No swapping involved in any pass
Pass n-1:the last pass has been executed
Bubble Sort
Example
42
Selection Sort
First Level Considerations
• To sort list of n elements in ascending
order
• Pass 1: make 1st element the smallest
• Pass 2: make 2nd element the 2nd smallest
• Pass 3: make 3rd element the 3rd smallest
• Pass n-2: make (n-2)th element the (n-
2)th smallest
• Pass n-1: make (n-1)th element the (n-
1)th smallest
• Number of passes is (n-1). 43
Selection Sort
Second Level Considerations
• Pass 1: Make 1st element the smallest
Examine list from 1st to last element locate element with
smallest value and swap it with the 1st element where
appropriate .
• Pass 2: Make 2nd element the 2nd smallest
Examine list from 2nd to last element locate element with
smallest value and swap it with the 2nd element where
appropriate.
• Pass n-1: Make (n-1)th element the (n-1)th smallest
Examine list from (n-1)th to last element locate element
with smallest value and swap it with the n-1th element
where appropriate.
44
Selection Sort
Example
45
Insertion Sort
First Level Considerations
To sort list of n items (stored as 1D array) in
ascending order
• NOTE: 1-element sub-array (1st) is always sorted
• Pass 1: make 2-element sub-array (1st 2nd) sorted
• Pass 2 :make 3-element sub-array (1st 2nd 3rd) sorted
• Pass 3 :make 4-element sub-array (1st 4th) sorted
• Pass n-2: make n-1-element sub-array (1st (n-1)th)
sorted
• Pass n-1: make entire n-element array (1st nth)
sorted
• Number of passes is (n-1)
46
Insertion Sort Example
47
Quick
Sort approach:
It uses Divide-and-Conquer
1. Divide: partition A[p..r] into two sub-arrays
A[p..q-1] and A[q+1..r] such that each element of
A[p..q-1] is ≤ A[q], and each element of A[q+1..r]
is ≥ A[q]. Compute q as part of this partitioning.
2. Conquer: sort the sub-arrays A[p..q-1] and
A[q+1..r] by recursive calls to QUICKSORT.
3. Combine: the partitioning and recursive sorting
leave us with a sorted A[p..r] – no work needed
here.
48
The Pseudo-Code of Quick
Sort
49
Quick Sort
Contd…..
50
Quick sort
Analysis
• Best case running time: O(n log2n)
• Worst case running time: O(n2)!!!
51
Quick Sort
Example
conquer paradigm
• Like heap-sort
It uses a comparator
It has O(n log n) running time
• Unlike heap-sort
It does not use an auxiliary priority queue
It accesses data in a sequential manner (suitable to sort
data
on a disk)
• Merge-sort on an input sequence S with n elements consists of
three steps:
Divide: partition S into two sequences S1 and S2 of about
n/2 elements each
Recur: recursively sort S1 and S2
Conquer: merge S1 and S2 into a unique sorted sequence 53
Merge Sort
Algorithm
Algorithm mergeSort(S, C)
Input sequence S with n elements, comparator C
Output sequence S sorted according to C
if S.size() > 1
(S1, S2) partition(S, n/2)
mergeSort(S1, C)
mergeSort(S2, C)
S merge(S1, S2)
54
Merge Sort
Example
55
Merge Sort Example
Contd…
56
Analysis of Merge
Sort
• The height h of the merge-sort tree is O(log n)
– at each recursive call we divide in half the sequence,
• The overall amount or work done at the nodes of depth
i is O(n)
– we partition and merge 2i sequences of size n/2i
– we make 2i+1 recursive calls
• Thus, the total running time of merge-sort is O(n log n)
57
Comparison of Sorting
Algorithms
58
UNIT - II
LINKED
LISTS
61
Arrays versus Linked
• BothLists
an array and a linked list are
representations of a list of items in memory.
The only difference is the way in which the
items are linked together. The Figure below
compares the two representations for a list of
five integers.
64
Applications of linked list
• Linked lists are used to represent and manipulate
polynomial. Polynomials are expression containing
terms with non zero coefficient and exponents. For
example:
P(x) = a0 Xn + a1 Xn-1 + …… + an-1 X + an
• Represent very large numbers and operations of the
large number such as addition, multiplication and
division.
• Linked lists are to implement stack, queue, trees and
graphs.
• Implement the symbol table in compiler construction.
65
Types of linked lists
• There are four types of Linked lists:
– Single linked list
• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction
– Circular single linked list
• Pointer in the last node points back to the first node
– Doubly linked list
• Two ―start pointers‖ – first element and last element
• Each node has a forward pointer and a backward pointer
• Allows traversals both forwards and backwards
– Circular double linked list
• Forward pointer of the last node points to the first node
and backward pointer of the first node points to the last
node
66
Singly Linked Lists
A singly linked list is a concrete
data structure consisting of a nex
sequence of nodes t
Each node stores
element
link to the next
ele nod
node
m e
A B C D
67
Singly Linked Lists
• A linked list allocates space for each element
separately in its own block of memory called a
"node".
• Each node contains two fields; a "data" field to store
whatever element, and a "next" field which is a
pointer used to link to the next node.
• Each node is allocated in the heap using malloc(), so
the node memory continues to exist until it is
explicitly de-allocated using free().
• The front of the list is a pointer to the ―start ‖ head
node.
68
Single Linked
List
69
Operations on Linked Lists
• The basic operations of a single linked list
are
– Creation
– Insertion
– Deletion
– Traversing
– Searching
70
Creating a node for Single Linked List:
Sufficient memory has to be allocated for creating a node. The
information is stored in the memory, allocated by using the
malloc() function. The function create List(), is used for creating
a linked list, after allocating memory for the structure of type
node, the information for the item (i.e., data) has to be read from
the user, set next field to NULL and finally returns the address
of the node.
https://codeforwin.org/data-
structures/c-program-to-create-
and-traverse-singly-linked-list
71
Creating a single linked list with
N nodes
Inserting a
node
• Inserting a node into a single linked list can
be done at
– Insertion at the beginning of the list.
– Insertion at the end of the list.
– Insertion in the middle of the list.
Inserting a node at the beginning
•Allocate memory for new node and initialize its DATA part to 5
•Add the new node as the first node of the list by pointing the NEXT part of the
new node to HEAD.
•Make HEAD to point to the first node of the list.
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF] struct node *new_node;
Step 2: SET NEW_NODE = AVAIL new_node = (struct node*) malloc(sizeof(struct node));
Step 3: SET AVAIL = AVAIL -> NEXT new_node->data = 5;
Step 4: SET NEW_NODE -> DATA = VAL new_node->next = head;
Step 5: SET NEW_NODE -> NEXT = HEAD head = new_node;
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
1000
Note: that the first step of the algorithm checks if there is enough memory
available to create a new node. The second, and third steps allocate memory
for the new node.
102
Inserting a node at the
beginning
Inserting a node at the
end
•Allocate memory for new node and initialize its DATA part to 50.
•Traverse to last node.
•Point the NEXT part of the last node to the newly created node.
•Make the value of next part of last node to NULL.
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET PTR = HEAD
Step 7: Repeat Step 8 while PTR -> NEXT != NULL
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: EXIT
Inserting a node at the end
ptr->next = new_node;
Inserting a node at the
end
Inserting a node at intermediate position
• Insert a Node after a given Node in a Linked list
The last case is when we want to add a new node after a given node. Suppose we
want to add a new node with value 50 after the node having data 20.
Algorithm: InsertAfterAnElement • Allocate memory for new node and initialize its DATA part to 50.
Step 1: IF AVAIL = NULL • Traverse the list until the specified node is reached.
Write OVERFLOW • Change NEXT pointers accordingly.
Go to Step 12
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET PTR = HEAD
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR -> DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 1 : PREPTR -> NEXT = NEW_NODE
Step 11: SET NEW_NODE -> NEXT = PTR
Step 12: EXIT
D.Chiranjevi, Asst.Prof @Aditya University
Inserting a node at intermediate position
Deletion of a
node
• Another primitive operation that can be done
in a singly linked list is the deletion of a node.
Memory is to be released for the node to be
deleted. A node can be deleted from the list
from three different places namely.
– Deleting a node at the beginning.
– Deleting a node at the end.
– Deleting a node at intermediate position.
Deleting a node at the
• The beginning
following steps are followed, to delete a node at
the beginning of the list:
• Check if the linked list is empty or not. Exit if the list is empty.
• Make HEAD points to the second node.
• Free the first node from memory.
if(head == NULL)
{
Step 1: IF HEAD = NULL
printf("Underflow");
Write UNDERFLOW
}
Go to Step 5
else
[END OF IF]
{
Step 2: SET PTR = HEAD
ptr = head;
Step 3: SET HEAD = HEAD -> NEXT
head = head -> next;
Step 4: FREE PTR
free(ptr);
Step 5: EXIT
}
Deleting a node at the
beginning
Deleting a node at the
endsteps are followed to delete a node at the end of the list:
The following
• Traverse to the end of the list.
• Change value of next pointer of second last node to NULL.
• Free last node from memory.
if(head == NULL)
Step 1: IF HEAD = NULL {
Write UNDERFLOW printf("Underflow");
Go to Step 8 }
[END OF IF] else
Step 2: SET PTR = HEAD {
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != NULL struct node* ptr = head;
Step 4: SET PREPTR = PTR struct node* preptr = NULL;
Step 5: SET PTR = PTR -> NEXT while(ptr->next!=NULL){
[END OF LOOP] preptr = ptr;
Step 6: SET PREPTR -> NEXT = NULL ptr = ptr->next;
Step 7: FREE PTR }
Step 8: EXIT preptr->next = NULL;
free(ptr);
}
Deleting a node at the
end
Deleting a node at Intermediate position
• Suppose we want to delete the that comes
after the node which contains data 10.
•Traverse the list upto the specified node.
•Change value of next pointer of previous
node(9) to next pointer of current node(10).
Deleting a node at Intermediate position
110
Search
Finding an element is similar to a traversal operation. Instead of displaying data, we have to check
whether the data matches with the item to find.
Initialize PTR with the address of HEAD. Now the PTR points to the first node of the linked list.
A while loop is executed which will compare data of every node with item.
If item has been found then control goes to last step.
93
Double Linked
• A doubleList
linked list is a two-way list in which all
nodes will have two links. This helps in accessing
both successor node and predecessor node from
the given node position. It provides bi-directional
traversing. Each node contains three fields:
– Left link.
– Data.
– Right link.
• The left link points to the predecessor node and
the right link points to the successor node. The
data field stores the required data.
94
A Double Linked
List
95
Basic operations in a double linked
list
• Creation
• Insertion
• Deletion
• Traversing
The beginning of the double linked list is
stored in a "start" pointer which points to the
first node. The first node‘s left link and last
node‘s right link is set to NULL.
96
Structure of a Double Linked
List
97
Creating a Double Linked List with N number
// make next pointer of
#include <stdio.h> of nodes //third node to NULL
//indicates last node
#include <stdlib.h> // Allocate memory for the nodes three->next = NULL;
one = (struct node*) malloc(sizeof(struct node)); //connect previous nodes
struct node two = (struct node*) malloc(sizeof(struct node)); one->prev = NULL;
{ three = (struct node*) malloc(sizeof(struct node)); two->prev = one;
int data;
three->prev = two;
struct node *next; // Assign data to nodes
struct node *prev; one->data = 1; // Save address of first node in
}; two->data = 2; head
three->data = 3; head = one;
int main()
current = head;
{ // Connect first node // print the linked list values
// Create and initialize nodes // with the second node forward
struct node *head; one->next = two; while(current != NULL)
struct node *one = NULL;
{
struct node *two = NULL; // Connect second node printf("%d ", current->data);
struct node *three = NULL; // with the third node current = current->next;
struct node *current = NULL; two->next = three; }
return 0;
} 98
Creating a Double Linked List with N numbe
of nodes
99
Inserting a node at the
beginning
Step 1: IF head = NULL
Exit
Step 2: SET ptr = head
Step 3: SET temp -> data = data
Step 4: SET temp -> prev = NULL
Step 5: SET temp -> next = ptr
Step 6 SET ptr->prev = temp
Step 6: SET temp -> prev = NULL
Step 7: SET head = temp
Step 8: EXIT
100
Inserting a node at the
beginning
101
Inserting a node at the begining
void InsertAtStart(struct Node** head, int data)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct
Node));
temp->data = new_data;
temp->next = (*head);
temp->prev = NULL;
if ((*head) != NULL)
(*head)->prev = temp;
(*head) = temp;
}
120
Inserting a node at the
end
Step 1: IF head = NULL
Exit
Step 2: SET ptr = head
Step 3: While ptr->next != NULL
ptr = ptr->next
Step 4: SET temp -> data = data
Step 5: SET temp -> prev = ptr
Step 6: SET temp -> next = NULL
Step 7 SET ptr->next = temp
Step 8: EXIT
120
Inserting a node at the
end
Inserting a node at the
void InsertAtEnd(struct Node** head, int data)
{ end
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
struct Node* ptr = *head;
temp->data = data
if (*head == NULL) {
ptr = temp;
return;
}
temp->next = NULL;
ptr->next = temp;
temp->prev = ptr;
return;
}
Inserting a node at an intermediate position
void insertBefore(struct Node **head, struct Node*
Step 1: IF head = NULL fixNode, int num){
Exit struct Node *ptr, *temp; temp->next = fixNode;
If head == fixNode
temp->prev = ptr;
InsertAtStart(head,data) temp = (struct Node*)malloc(sizeof(struct Node)); ptr->next = temp;
Exit temp->data = num; fixNode->prev = temp;
Step 2: SET ptr = head
}
Step 3: While ptr->next != fixNode if (*head == fixNode) {
ptr = ptr->next temp->next = *head;
Step 4: SET temp->data = data (*head)->prev = temp;
Step 5: SET temp -> prev = ptr *head = temp;
Step 6: SET temp -> next = fixNode return;
Step 7 SET ptr->next = temp }
Step 8: SET fixNode->prev = temp
Step 8: EXIT ptr = *head;
while (ptr->next != fixNode) {
ptr = ptr->next;
}
Inserting a node at an intermediate position
Deleting a node at the beginning
TEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 4: SET HEAD → PREV = NULL
STEP 5: FREE PTR
STEP 6: EXIT
Deleting a node at the
beginning
Deleting a node at the
end
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 6: i = i + 1
Step 7: PTR = PTR → next
Step 8: Exit
Advantages and Disadvantages of Double
Linked List
• The major disadvantage of doubly linked lists
(over singly linked lists) is that they require more
space (every node has two pointer fields instead
of one). Also, the code to manipulate doubly
linked lists needs to maintain the prev fields as
well as the next fields; the more fields that have to
be maintained, the more chance there is for errors.
• The major advantage of doubly linked lists is
that they make some operations (like the removal
of a given node, or a right-to-left traversal of the
list) more efficient.
Circular Single Linked List
• It is just a single linked list in which the link field
of the last node points back to the address of the
first node.
• A circular linked list has no beginning and no end.
It is necessary to establish a special pointer called
start pointer always pointing to the first node of
the list.
• Circular linked lists are frequently used instead of
ordinary linked list because many operations are
much easier to implement. In circular linked list
no null pointers are used, hence all pointers
contain valid address.
Circular Single Linked List and its basic
operations
140
Deleting a node at the
beginning
Deleting a node at the
•
•
end
Traverse to the end of the list.
Change value of next pointer of second last node to HEAD.
• Free last node from memory.
Where, ci is the coefficient of the ith term and n is the degree of the polynomial
Some examples are:
5x2 + 3x + 1
5x4 – 8x3 + 2x2 + 4x1 + 9x0
The computer implementation requires implementing polynomials as a list of pairs of
coefficient and exponent. Each of these pairs will constitute a structure, so a polynomial
will be represented as a list of structures. A linked list structure that represents
polynomials 5x4 – 8x3 + 2x2 + 4x1 + 9x0 illustrated.
Addition of
Polynomials
• To add two polynomials, if we find terms with
the same exponent in the two polynomials, then
we add the coefficients; otherwise, we copy the
term of larger exponent into the sum and go on.
When we reach at the end of one of the
polynomial, then remaining part of the other is
copied into the sum.
• To add two polynomials follow the
following steps:
– Read two polynomials.
– Add them.
– Display the resultant polynomial.
UNIT - III
LINEAR DATASTRUCTURES
Contents
• Stack - Primitive Operations and Implementation
• Applications of Stack
• Queue - Primitive Operations and Implementation
• Linear Queue operations
• Applications of Linear Queue
• Circular Queue operations
• Priority Queue
• Double Ended Queue (Deque)
Stacks
• A stack is a list of elements in which an
element may be inserted or deleted only at one
end, called the top of the stack.
• The elements are removed from a stack in the
reverse order of that in which they were
inserted into the stack.
• Stack is also known as a LIFO (Last in Fast
out) list or Push down list.
Basic Stack
PUSH: Operations
It is the term used to insert an
element into a stack.
PUSH operations on
stack
Basic Stack
Operations
POP: It is the term used to delete an
element from a stack.
Now, delete two elements 11, 22 from the circular queue. The circular queue status is
as follows:
Insertion and Deletion
operations on a Circular
Queue
Again, insert another element 66 to the circular queue. The status of
the
circular queue is:
Again, insert 77 and 88 to the circular queue. The status of the Circular
queue is:
Double Ended Queue
• It(DEQUE)
is a special queue like data structure that
supports insertion and deletion at both the
front and the rear of the queue.
• Such an extension of a queue is called a
double-ended queue, or deque, which is
usually pronounced "deck" to avoid confusion
with the dequeue method of the regular queue,
which is pronounced like the abbreviation
"D.Q."
• It is also often called a head-tail linked list.
DEQUE Representation using arrays
Types of DEQUE
• There are two variations of deque. They are:
– Input restricted deque (IRD)
– Output restricted deque (ORD)
• An Input restricted deque is a deque, which
allows insertions at one end but allows
deletions at both ends of the list.
• An output restricted deque is a deque, which
allows deletions at one end but allows
insertions at both ends of the list.
Priority
Queue
• A priority queue is a collection of elements
that each element has been assigned a priority
and such that order in which elements are
deleted and processed comes from the
following riles:
– An element of higher priority is processed before
any element of lower priority.
– Two element with the same priority are processed
according to the order in which they were added to
the queue.
Priority Queue Operations and
Usage
• Inserting new elements.
• Removing the largest or smallest element.
• Priority Queue Usages are:
Simulations: Events are ordered by the time
at which they should be executed.
Job scheduling in computer systems:
Higher priority jobs should be executed first.
Constraint systems: Higher priority
constraints should be satisfied before lower priority
constraints.
UNIT - 4
NON LINEAR
DATA
STRUCTURES
CONTENTS
• Basic Tree Concepts, Binary Trees
• Representation of Binary Trees
• Operations on a Binary Tree
• Binary Tree Traversals
• Threaded Binary Trees
• Basic Graph Concepts
• Graph Traversal Techniques: DFS and
BFS
Tree – a Hierarchical Data
Structure
• Trees are non linear data structure that can
be represented in a hierarchical manner.
– A tree contains a finite non-empty set
ofelements.
– Any two nodes in the tree are connected
with a relationship of parent-child.
– Every individual elements in a tree can have
any number of sub trees.
An Example of a
Tree
Tree – Basic
Terminology
• Root : The basic node of all nodes in the tree. All
operations on the tree are performed with passing
root node to the functions.
• Child : a successor node connected to a node is
called child. A node in binary tree may have at
most two children.
• Parent : a node is said to be parent node to all its
child nodes.
• Leaf : a node that has no child nodes.
• Siblings : Two nodes are siblings if they are
children to the same parent node.
Tree – Basic Terminology
Contd…
• Ancestor : a node which is parent of parent node
( A is ancestor node to D,E and F).
• Descendent : a node which is child of child node
( D, E and F are descendent nodes of node A)
• Level : The distance of a node from the root
node, The root is at level – 0,( B and C are at
Level 1 and D, E, F have Level 2 ( highest
level of tree is called height of tree )
• Degree : The number of nodes connected to a
particular parent node.
Binary
Tree
• A binary tree is a hierarchy of nodes, where
every parent node has at most two child nodes.
There is a unique node, called the root, that does
not have a parent.
• A binary tree can be defined recursively as
• Root node
• Left subtree: left child and all its descendants
• Right subtree: right child and all its
descendants
Binary Tree
a
b c
d e f
g h i j k
l
Full and Complete Binary
• ATrees
full tree is a binary tree inwhich
– Number of nodes at level l is 2l–1
– Total nodes in a full tree of height n is
• A complete tree of height n is a binarytree
– Number of nodes at level 1 l n–1 is 2l–1
– Leaf nodes at level n occupy the leftmost
positions in the tree
Full and Complete Binary
Trees
Tree
• A binary treeTraversals
is defined recursively: it
consists of a root, a left subtree, and a right
subtree.
• To traverse (or walk) the binary tree is to
visit each node in the binary tree exactly
once.
• Tree traversals are naturally recursive.
• Standard traversal orderings:
• preorder
• inorder
• postorder
Preoder, Inorder,
Postorder
• In Preorder, the root Preorder Traversal:
1. Visit the root
is visited before 2. Traverse left subtree
(pre) 3. Traverse right subtree
visited in-between
left Postorder Traversal:
1. Traverse left subtree
and right subtree 2. Traverse right subtree
3. Visit the root
traversal.
• In Preorder, the root
Example of Tree
Traversal
• Assume: visiting a
1
node
is printing its data 5
2
8
• Preorder: 15 8 2 6 3 7 0
11 10 12 14 20 27 22 2 1 2
1 7
30
• Inorder: 2 3 6 7 8 10 6 10 2
2
3
0
12
11 3 7 1
12 14 15 20 22 27 30 4
• Postorder: 3 7 6 2 10
14
Traversal
Techniques
void preorder(tree *tree) {
if (tree->isEmpty( )) return;
visit(tree->getRoot( ));
preOrder(tree->getLeftSubtree());
preOrder(tree->getRightSubtree());
}
void inOrder(Tree *tree){
if (tree->isEmpty( ))
return; inOrder(tree-
>getLeftSubtree( )); visit(tree-
>getRoot( )); inOrder(tree-
>getRightSubtree( ));
}
void postOrder(Tree *tree){
if (tree->isEmpty( )) return;
postOrder(tree->getLeftSubtree( ));
postOrder(tree->getRightSubtree( ));
visit(tree->getRoot( ));
}
Threaded Binary
Tree
• A threaded binary tree
defined as:
• "A binary tree is threaded
by making all right child
pointers that would
normally be null point to
the inorder successor of
the node, and all left child
pointers that would
normally be null point to
the inorder predecessor of
the node
Graph
Basics of nodes connected
• Graphs are collections
by edges – G = (V,E) where V is a set of
nodes and E a set of edges.
• Graphs are useful in a number of
applications including
– Shortest path problems
– Maximum flow problems
• Graphs unlike trees are more general for
they can have connected components.
Graph
• Directed
Types
Graphs: A directed edges
allow
graph travel in one
• direction.
Undirected Graphs: An graph
undirected edges allow travel in either
direction.
Graph
Terminology
• A graph is an ordered pair G=(V,E) with a set
of vertices or nodes and the edges that
connect them.
• A subgraph of a graph has a subset
ofthe vertices and edges.
• The edges indicate how we can move
through the graph.
• A path is a subset of E that is a series
ofedges between two nodes.
• A graph is connected if there is at least
one path between every pair of nodes.
Graph Terminology
• The length of a path in a graph is the number of
edges in the path.
• A complete graph is one that has an edge
between every pair of nodes.
• A weighted graph is one where each edge has
a cost for traveling between the nodes.
• A cycle is a path that begins and ends at the
same node.
• An acyclic graph is one that has no cycles.
• An acyclic, connected graph is also called an
unrooted tree
Data Structures for
Graphs An Adjacency
• For an Matrix
undirected graph, the matrix will be
symmetric along the diagonal.
• For a weighted graph, the adjacency matrix
would have the weight for edges in the graph,
zeros along the diagonal, and infinity (∞)
every place else.
Adjacency Matrix Example
1
Adjacency Matrix Example
2
Data Structures for
Graphs An
• A list of Adjacency List
pointers, one for each node of the
graph.
• These pointers are the start of a linked list of
nodes that can be reached by one edge of the
graph.
• For a weighted graph, this list would also
include the weight for each edge.
Adjacency List Example
1
Adjacency List Example
2
Graph
Traversals
• Some algorithms require that every vertex of
a graph be visited exactly once.
• The order in which the vertices are visited
may important, and may
be upon
depend particular algorithm. the
• The two common traversals:
- depth-first
- breadth-first
Graph Traversals:
Depth First Search
• WeTraversal
follow a path through the graph until we
reach a dead end.
• We then back up until we reach a node with
an edge to an unvisited node.
• We take this edge and again follow it until we
reach a dead end.
• This process continues until we back up to the
starting node and it has no edges to unvisited
nodes.
Depth First Search
Traversal
• Consider the following
graph:
Example
170
Binary Search
• In a Trees
BST, each node stores some information
including a unique key value, and perhaps some
associated data. A binary tree is a BST iff, for every
node n in the tree:
• All keys in n's left subtree are less than the key in n,
and
• All keys in n's right subtree are greater than the key in
n.
• In other words, binary search trees are binary trees in
which all values in the node‘s left subtree are less
than node value all values in the node‘s right subtree
are greater than node value. 180
BST
Example
Properties and
Operations
A BST is a binary tree of nodes ordered in
the following way:
i. Each node contains one key (also unique)
ii. The keys in the left subtree are < (less) than
the key in its parent node
iii. The keys in the right subtree > (greater) than
the key in its parent node
iv. Duplicate node keys are not allowed.
Inserting a
node
• A naïve algorithm for inserting a node into a BST is
that, we start from the root node, if the node to insert
is less than the root, we go to left child, and otherwise
we go to the right child of the root.
• We then insert the node as a left or right child of the
leaf node based on node is less or greater than the leaf
node. We note that a new node is always inserted as a
leaf node.
Inserting a
node
• A recursive algorithm for inserting a node into a
BST is as follows. Assume we insert a node N to
tree T. if the tree is empty, the we return new
node N as the tree. Otherwise, the problem of
inserting is reduced to inserting the node N to left
of right sub trees of T, depending on N is less or
greater than T.A definition is asfollows.
Insert(N, T) = N if T is empty
= insert(N, T.left) if N <T
= insert(N, T.right) if N > T
Searching for a
• Searchingnode
for a node is similar to inserting a node. We
start from root, and then go left or right until we find
(or not find the node). A recursive definition of
search is as follows. If the node is equal to root, then
we return true. If the root is null, then we return false.
Otherwise we recursively solve the problem for T.left
or T.right, depending on N < T or N > T. A recursive
definition is as follows.
• Search should return a true or false, depending on the
node is found or not.
Searching for a
• Search(N, node
T) = false if T is empty Searching for a node is
similar to inserting a node. We start from root, and then
go left or right until we find (or not find the node).
• A recursive definition of search is as follows. If the node
is equal to root, then we return true. If the root is null,
then we return false. Otherwise we recursively solve the
problem for T.left or T.right, depending on N < T or N >
T.A recursive definition is as follows.
• Search should return a true or false, depending on the
node is found or not.
Search(N, T) = false if T is empty
= true if T = N
= search(N, T.left) if N < T
186
= search(N, T.right) if N >
Deleting a
node structure. That is, all nodes in a
• A BST is a connected
tree are connected to some other node. For example,
each node has a parent, unless node is the root.
Therefore deleting a node could affect all sub trees of
that node. For example, deleting node 5 from the tree
could result in losing sub trees that are rooted at 1 and
9.
Balanced Search
Trees (or height-balanced) binary
• A self-balancing search
tree is any node-based binary search tree that
automatically keeps its height (maximal number of levels
below the root) small in the face of arbitrary item
insertions and deletions.
• AVL Trees: An AVL tree is another balanced binary search
tree. Named after their inventors, Adelson-Velskii and
Landis, they were the first dynamically balanced trees to
be proposed. Like red-black trees, they are not perfectly
balanced, but pairs of sub-trees differ in height by at most
1, maintaining an O(logn) search time. Addition and
deletion operations also take O(logn) time.
AVL Tree -
Definition
• Definition of an AVL tree: An AVL tree is a binary search tree
which has the following properties:
i. The sub-trees of every node differ in height by at most one.
ii. Every sub-tree is anAVL tree.
• Balance requirement for an AVL tree: the left and right sub-trees d
by at most 1 in height.
190
Balance
• To implementFactor
our AVL tree we need to keep track of
a balance factor for each node in the tree. We do this
by looking at the heights of the left and right subtrees
for each node. More formally, we define the balance
factor for a node as the difference between the height
of the left subtree and the height of the right subtree.
balanceFactor=height(leftSubTree)−height(rightSubTree)
• Using the definition for balance factor given above
we say that a subtree is left-heavy if the balance
factor is greater than zero. If the balance factor is less
than zero then the subtree is right heavy. If the
balance factor is zero then the tree is perfectly in
Balance
Factor
Introduction to M-Way Search Trees
• A multiway tree is a tree that can have more than two
children. A multiway tree of order m (or an m-way tree)
is one in which a tree can have m children.
• As with the other trees that have been studied, the nodes
in an m-way tree will be made up of key fields, in this
case m-1 key fields, and pointers to children.
• Multiday tree of order 5
Properties of M-way Search
•Trees
m-way search tree is a m-way tree in which:
i. Each node has m children and m-1 key fields
ii. The keys in each node are in ascending order.
iii. The keys in the first i children are smaller than the ithkey
iv. The keys in the last m-i children are larger than the
ith key
• 4-way search tree
B -Trees
• An extension of a multiway search tree of order m is a B-
tree of order m. This type of tree will be used when the
data to be accessed/stored is located on secondary storage
devices because they allow for large amounts of data to
be stored in a node.
• A B-tree of order m is a multiway search tree inwhich:
i. The root has at least two subtrees unless it is the only node
in the tree.
ii. Each nonroot and each nonleaf node have at most m
nonempty children and at least m/2 nonempty children.
iii. The number of keys in each nonroot and each nonleaf node is
one less than the number of its nonempty children.
iv. All leaves are on the same level.
Searching a B -
Tree
• Start at the root and determine which pointer to
follow based on a comparison between the search
value and key fields in the root node.
• Follow the appropriate pointer to a child node.
• Examine the key fields in the child node and
continue to follow the appropriate pointers until
the search value is found or a leaf node is reached
that doesn't contain the desired search value.
Insertion into a B -Tree
• The condition that all leaves must be on the same
level forces a characteristic behavior of B-trees,
namely that B-trees are not allowed to grow at the
their leaves; instead they are forced to grow at the
root.
• When inserting into a B-tree, a value is inserted
directly into a leaf. This leads to three common
situations that can occur:
i. A key is placed into a leaf that still hasroom.
ii. The leaf in which a key is to be placed is full.
iii. The root of the B-tree is full.
Deleting from a B -
Tree process will basically be a reversal
• The deletion
of the insertion process - rather than splitting
nodes, it's possible that nodes will be merged so
that B-tree properties, namely the requirement
that a node must be at least half full, can be
maintained.
• There are two main cases to be considered:
i. Deletion from a leaf
ii. Deletion from a non-leaf
Hashing
• Hashing is the technique used for performing almost
constant time search in case of insertion, deletion and find
operation.
• Taking a very simple example of it, an array with its index
as key is the example of hash table. So each index (key)
can be used for accessing the value in a constant search
time. This mapping key must be simple to compute and
must helping in identifying the associated value. Function
which helps us in generating such kind of key- value
mapping is known as Hash Function.
• In a hashing system the keys are stored in an array which
is called the Hash Table. A perfectlyimplemented hash
table would always promise average
204
an insert/delete/retrieval time of O(1).
Hashing
Function
• A function which employs some algorithm to computes
the key K for all the data elements in the set U, such that
the key K which is of a fixed size. The same key K can be
used to map data to a hash table and all the operations
like insertion, deletion and searching should be possible.
The values returned by a hash function are also referred
to as hash values, hash codes, hash sums, or hashes.
Hash
• A situation Collision
when the resultant hashes for two more
data
orelements in the data set U, maps to the same location
in the has table, is called a hash collision. In such a
situation two or more data elements would qualify to be
stored / mapped to the same location in the hash table.
• Hash collision resolution techniques:
• Open Hashing (Separate chaining):Open Hashing, is a
technique in which the data is not directly stored at the
hash key index (k) of the Hash table. Rather the data at
the key index (k) in the hash table is a pointer to the head
of the data structure where the data is actually stored. In
the most simple and common implementations the data
structure adopted for storing the element is a linked-list.
Closed Hashing (Open
•Addressing)
In this technique a hash table with pre-identified size
is considered. All items are stored in the hash table
itself. In addition to the data, each hash bucket also
maintains the three states: EMPTY, OCCUPIED,
DELETED. While inserting, if a collision occurs,
alternative cells are tried until an empty bucket is
found. For which one of the following technique is
adopted.
• Liner Probing
• Quadratic probing
• Double hashing
A Comparative Analysis of
Closed Hashing vs Open
Hashing
Applications of
• A hashHashing
function maps a variable length input string to
fixed length output string -- its hash value, or hash for
short. If the input is longer than the output, then some
inputs must map to the same output -- a hash collision.
• Comparing the hash values for two inputs can give us one
of two answers: the inputs are definitely not the same, or
there is a possibility that they are the same. Hashing as we
know it is used for performance improvement, error
checking, and authentication.
• In error checking, hashes (checksums, message digests,
etc.) are used to detect errors caused by either hardware or
software. Examples are TCP checksums, ECC memory,
and MD5 checksums on downloaded files. 210
Applications of
Hashing
• Construct a message authentication code (MAC)
• Digital signature
• Make commitments, but reveal message later
• Timestamping
• Key updating: key is hashed at specific
intervals resulting in new key
211
THANK
YOU