Material
Material
UNIT I
LINEAR STRUCTURES
1.1 Abstract Data Types (ADT)
1.2 List ADT
1.3 Array-based implementation
1.4 Linked list implementation
1.5 Doubly-linked lists
1.6 Cursor-based linked lists
1.7 Applications of lists
INTRODUCTION
• A DATA is a collection of facts, concepts, figures, observation, occurrences or instruction in
formalized manner.
• If we arrange some data in an appropriate sequence, then it forms a Structure and gives us a
meaning. This meaning is called Information (i.e, processed data)
• So, we found two things in Information: One is Data and the other is Structure.
DATA STRUCTURE
A data structure is a systematic way of organizing, storing, manipulating and accessing data in
such a way that some meaningful operations can be performed on these data in an effective way.
Applications of data structures
• Compiler design
• Operating System
• Database Management system
• Network analysis
• Expert Systems
CLASSIFICATION OF DATA STRUCTURE
Primitive The primitive data structures are known as basic data Integer, Float, Character, Pointer,
data structures. Boolean, String
structure These data structures are directly operated by the
machine instructions.
Normally, primitive data structures have different
representation on different computers.
UNIT 1 Page 1
16CS3202 - Data Structures II Yr CSE / III Sem
Non- The non-primitive data structures are highly Arrays, Lists, Files
Primitive developed complex data structures.
data Basically, these are developed from the primitive
structure data structure.
The non-primitive data structure is responsible for
organizing the group of homogeneous and
heterogeneous data elements.
A non primitive data structure is further divided into
linear and non linear data structure.
Linear Data In Linear data structures, the data items are arranged Array, Linked List, Stack, Queue
Structure in a linear sequence.
Data elements in a liner data structure are traversed
one after the other and only one element can be
directly reached while traversing.
Non Linear The data structure where data items are not Trees, Graphs
Data organized sequentially is called non linear data
Structure structure.
The data elements of the non linear data structure
could be connected to more than one element to
reflect a special relationship among them.
Every item is related to its previous and next item. Every item is attached with many other items.
UNIT 1 Page 2
16CS3202 - Data Structures II Yr CSE / III Sem
Data items can be traversed in a single run. Data cannot be traversed in a single run.
UNIT 1 Page 4
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 5
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 6
16CS3202 - Data Structures II Yr CSE / III Sem
int p,i;
if(isempty())
{
cout<<"List is Empty Cannot delete\n";
}
else
{
cout<<"Enter the position to delete:";
cin>>p;
if(p-1>=pos)
{
cout<<"Position incorrect cannot delete\n";
}
else
{
cout<<"Element deleted at position "<<p<<" is "<<list[p-1]<<"\n";
if(p-1==pos-1)
{
pos--;
}
else
{
for(i=p-1;i<pos-1;i++)
{
list[i]=list[i+1];
}
pos--;
}
}
}
display();
}
void Arraylist::display()
UNIT 1 Page 7
16CS3202 - Data Structures II Yr CSE / III Sem
{
int i;
if(isempty())
{
cout<<"List is Empty \n";
}
else
{
cout<<"List Elements are\n";
for(i=0;i<pos;i++)
{
cout<<list[i]<<" ";
}
}
cout<<"\n";
}
int Arraylist::isempty()
{
if(pos==0)
return 1;
else
return 0;
}
int Arraylist::isfull()
{
if(pos==MAX)
return 1;
else
return 0;
}
void Arraylist::search()
{
int data,i;
UNIT 1 Page 8
16CS3202 - Data Structures II Yr CSE / III Sem
}
}
int main()
{
int choice;/*to select multiple menu choices*/
clrscr();
Arraylist obj;
do
{
cout<<"*******MENU*******\n1.Insert\n2.Delete\n3.Display\n4.Search\nEnter your Choice:";
UNIT 1 Page 9
16CS3202 - Data Structures II Yr CSE / III Sem
cin>>choice;
switch(choice)
{
case 1:
obj.insert();
break;
case 2:
obj.del();
break;
case 3:
obj.display();
break;
case 4:
obj.search();
break;
default:
cout<<"wrong choice \n";
}
}while(choice<5);
getch();
return 0;
}
OUTPUT:
*******MENU*******
1.Insert
2.Delete
3.Display
4.Search
Enter your Choice:1
Enter the element to insert:10
List Elements are
10
*******MENU*******
UNIT 1 Page 10
16CS3202 - Data Structures II Yr CSE / III Sem
1.Insert
2.Delete
3.Display
4.Search
Enter your Choice:1
Enter the element to insert:20
Enter the position to insert:2
List Elements are
10 20
*******MENU*******
1.Insert
2.Delete
3.Display
4.Search
Enter your Choice:1
Enter the element to insert:30
Enter the position to insert:1
List Elements are
30 10 20
*******MENU*******
1.Insert
2.Delete
3.Display
4.Search
Enter your Choice:1
Enter the element to insert:40
Enter the position to insert:2
List Elements are
30 40 10 20
UNIT 1 Page 11
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 12
16CS3202 - Data Structures II Yr CSE / III Sem
2. Insert
3. Delete
4. Display
1. Create
The linked list structure will be
struct NODE
{
int data;
NODE *next;
};
NODE *first,*last,*curr,*temp;
Create function will create a node initially as per the structure definition and get the data from the
user and store it in the ‘data’ field and place the ‘NULL’ in the next field. If the node created is the first
element then the first and last pointer is assigned to the node created. If the list contains elements then the
temporary pointer is moved to the last element and link with newly created node.
The codes to create the singly linked list are as follows:
curr=new node();
cout<<”Enter the element”;
cin>>curr→data;
curr→next=NULL;
if(first==NULL && last==NULL)
{
fisrt=curr;
last=curr;
temp=curr;
}
else
{
temp→next=curr;
temp=curr;
last=curr;
}
Example:
Assume the list created using the above code contains 5 elements as shown in figure 5.
UNIT 1 Page 13
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 14
16CS3202 - Data Structures II Yr CSE / III Sem
Step 2: Create a new node with given value and curr → next as NULL.
Step 3: Set temp → next = curr and last=curr.
The codes to insert at the end of a list are as follows:
while(temp->next!=NULL)
temp=temp->next;
curr=new node();
cout<<"\nEnter the data for the newnode";
cin>>curr->data;
curr->next=NULL;
temp->next=curr;
last=curr;
Example:
Assume the list contains 6 elements and the new element 60 is inserted at the last of a list.
UNIT 1 Page 15
16CS3202 - Data Structures II Yr CSE / III Sem
cin>>curr->data;
curr->next=temp->next;
temp->next=curr;
Example:
Assume the list contains 6 elements and the new element 35 is inserted after the element 30 in a list.
UNIT 1 Page 16
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 17
16CS3202 - Data Structures II Yr CSE / III Sem
}
else
{
prev->next=temp->next;
cout<<”\nDeleted node”<<temp->data;
free(temp);
}
Example:
Assume the list contains 6 elements and the element 60 is deleted in a list.
UNIT 1 Page 18
16CS3202 - Data Structures II Yr CSE / III Sem
Figure
UNIT 1 Page 19
16CS3202 - Data Structures II Yr CSE / III Sem
If a doubly linked list contains the elements a1, a2, a3, a4 and a5, then the representation is shown in
figure 11 .
Figure 12 shows the actual representation of the list. The list contains four structures, which happen to
reside in memory locations 1000, 800,712, 992 respectively.
UNIT 1 Page 20
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 21
16CS3202 - Data Structures II Yr CSE / III Sem
2. Insertion
There are three situation for inserting element in list.
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Insertion at the front of a list
The steps to insert a new element at the front of a list are:
Step 1: Check whether the list is empty. If it is TRUE, then print to create a new list.
Step 2: If it if FALSE, then define a new pointer ‘temp’ and assign to first element.
Step 3: Create a new node with the definition of structure node and assign the value to curr->data and
NULL to curr->prev.
Step 4: Assign curr->next to ‘temp’ to connect the element.
Step 5: Also assign the temp->prev to curr in order to link the previous node and make the curr node to
first.
The code used to insert a new element at the beginning of doubly linked list is as follows:
void double_llist::add_begin(int value)
{
if (first == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
temp=first;
node *curr;
curr = newnode;
curr->prev = NULL;
curr->data = value;
curr->next = temp;
temp->prev = curr;
first=curr;
cout<<"Element Inserted"<<endl;
}
UNIT 1 Page 22
16CS3202 - Data Structures II Yr CSE / III Sem
Example:
The doubly linked list after inserting the element 10 at the beginning is as follows:
UNIT 1 Page 23
16CS3202 - Data Structures II Yr CSE / III Sem
{
temp = temp->next;
}
curr = newnode;
curr->data = value;
if (temp->next == NULL) /*Insert at Last position*/
{
temp->next = curr;
curr->next = NULL;
curr->prev = temp;
}
else /*Insert at middle of the list*/
{
curr->next = temp->next;
curr->prev = temp;
curr->next->prev = curr;
temp->next = curr;
}
cout<<"Element Inserted"<<endl;
}
Example:
The new element 30 inserted after the element 30 is shown below:
Figure 14 Doubly Linked list after inserting the element at the position
The element 50 inserted at the last of a doubly linked list is shown below:
UNIT 1 Page 24
16CS3202 - Data Structures II Yr CSE / III Sem
Figure 15 Doubly Linked list after inserting the element at the last
3. Deletion
Similar to the insertion, the deletion operation in a doubly linked list is performed in three ways:
1. Delete from the beginning
2. Delete at the middle
3. Delete at the last
Steps to follow for deletion operation
Step1: If the given key element matches with the first element, then assign a temporary pointer ‘temp’to
first and NULL to first->prev to indicate no element previous. Then deallocate the memory used by temp.
Step 2: Else, keep moving the temp pointer until it reaches the node previous to the required position.
Step 3: Define another temporary pointer variable ‘temp1’and assign to the matched element node.
Step 4: Before deleting the temp1 node, link the temp node and to the node next to temp1.
Step 5: Deallocate the memory used by temp1.
Step 6: If both the conditions in step 1 & 2 fails, then the element to be deleted is the last node. Assign
NULL to the next of previous node and deallocate the memory.
The code used to perform the deletion operation in a doubly linked list is as follows:
void double_llist::delete_element(int value)
{
node *temp, *q;
if (first->data == value) /*first element deletion*/
{
temp = first;
first = temp->next;
first->prev = NULL;
cout<<"Element Deleted"<<endl;
UNIT 1 Page 25
16CS3202 - Data Structures II Yr CSE / III Sem
free(tmp);
return;
}
temp = first;
while (temp->next->next != NULL)
{
if (temp->next->data == value) /*Element deleted in between*/
{
temp1 = temp->next;
temp->next = temp1->next;
temp1->next->prev = temp;
cout<<"Element Deleted"<<endl;
free(tmp1);
return;
}
temp= temp->next;
}
if (temp->next->data == value) /*last element deleted*/
{
temp1= temp->next;
free(temp1);
temp->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}
}
Example:
Assume the doubly linked list contains the 4 elements after the create operation as shown below:
UNIT 1 Page 26
16CS3202 - Data Structures II Yr CSE / III Sem
Figure 17 Doubly Linked list after deleting the element from the beginning
The list after deleting the middle element 30 is as follows:
Figure 18 Doubly Linked list after deleting the element from the middle
4. Display
The display function is used to display the element present in the list from first to last using the next
pointer and from last to first using prev pointer.
The code used to display the list of elements in a doubly linked list is as follows:
void display()
{
temp=first;
//Display the elements from first to last
cout << “First to Last\n”;
cout<<”NULL”;
for(temp=first;temp!=NULL;temp=temp->next)
cout<<”<-“<<temp->data << ”->”;
cout>>”NULL\n”;
UNIT 1 Page 27
16CS3202 - Data Structures II Yr CSE / III Sem
Figure 20 shows the actual representation of the circular doubly link list in memory. The list contains five
nodes, which happen to reside in memory locations 1000, 800,712, 992, and 692 respectively.
UNIT 1 Page 28
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 29
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 30
16CS3202 - Data Structures II Yr CSE / III Sem
CURSOR_SPACE[0].next = p;
}
Deletion routine for linked lists--cursor implementation
void delete( element_type x, LIST L )
{
position p, tmp_cell;
p = find_previous( x, L );
if( !is_last( p, L) )
{
tmp_cell = CURSOR_SPACE[p].next;
CURSOR_SPACE[p].next = CURSOR_SPACE[tmp_cell].next;
cursor_free( tmp_cell );
}
}
fatal_error("Out of space!!!");
else
{
CURSOR_SPACE[tmp_cell].element = x;
CURSOR_SPACE[tmp_cell].next = CURSOR_SPACE[p].next;
CURSOR_SPACE[p].next = tmp_cell;
}
}
Polynomial Representation: Header linked list are frequently used for maintaining polynomials in
memory. The header node plays an important part in this representation since it is needed to
represent the zero polynomial.
Specifically, the information part of node is divided into two fields representing respectively, the
coefficient and the exponent of corresponding polynomial term and nodes are linked according to
decreasing degree. List pointer variable POLY points to header node whose exponent field is
assigned a negative number, in this case -1. The array representation of List will require three
linear arrays as COEFF, EXP and LINK
Example 1:
Input:
1st number = 5x^2 + 4x^1 + 2x^0
UNIT 1 Page 32
16CS3202 - Data Structures II Yr CSE / III Sem
Example2:
Input:
1st number = 5x^3 + 4x^2 + 2x^0
2nd number = 5x^1 + 5x^0
Output:
5x^3 + 4x^2 + 5x^1 + 7x^0
To perform the above polynomial addition using linked list, the structure must be defined as below:
Using the above definition, the polynomial can be added in a linked list as follows:
#include<iostream>
UNIT 1 Page 33
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 34
16CS3202 - Data Structures II Yr CSE / III Sem
{
Node *last=p;
while(poly1!=NULL && poly2!=NULL)
{
// If power of 1st polynomial is greater then 2nd, then store 1st as it is and move its pointer
if(poly1->pow > poly2->pow)
{
p->pow = poly1->pow;
p->coeff = poly1->coeff;
poly1 = poly1->next;
}
// If power of 2nd polynomial is greater then 1st, then store 2nd as it is and move its pointer
else if(poly1->pow < poly2->pow)
{
p->pow = poly2->pow;
p->coeff = poly2->coeff;
poly2 = poly2->next;
}
// If power of both polynomial numbers is same then add their coefficients
else
{
p->pow = poly1->pow;
p->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
// Dynamically create new node
p->next = new Node();
last = p;
p = p->next;
p->next = NULL;
}
while(poly1!=NULL|| poly2!=NULL)
UNIT 1 Page 35
16CS3202 - Data Structures II Yr CSE / III Sem
{
if(poly1!=NULL)
{
p->pow = poly1->pow;
p->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2!=NULL)
{
p->pow = poly2->pow;
p->coeff = poly2->coeff;
poly2 = poly2->next;
}
p->next = new Node();
last=p;
p = p->next;
p->next = NULL;
}
last->next=NULL;
}
// Display Linked list
void show(Node *node)
{
while(node!= NULL)
{
cout<< node->coeff <<"^"<< node->pow;
node = node->next;
if(node!= NULL)
cout<<" + ";
}
}
//main program
int main()
UNIT 1 Page 36
16CS3202 - Data Structures II Yr CSE / III Sem
{
Node *poly1 = NULL, *poly2 = NULL;
// Create first list of 5x^2 + 4x^1 + 2x^0
poly1=create_node(5,2,poly1);
poly1=create_node(4,1,poly1);
poly1=create_node(2,0,poly1);
// Create second list of 5x^1 + 5x^0
poly2=create_node(5,1,poly2);
poly2=create_node(5,0,poly2);
cout<<"1st Number: ";
show(poly1);
cout<<"\n2nd Number: ";
show(poly2);
Node *poly;
// Function add two polynomial numbers
polyadd(poly1, poly2, poly);
// Display resultant List
cout<<"\nAdded polynomial: ";
show(poly);
return 0;
}
Output:
1st Number: 5x^2 + 4x^1 + 2x^0
2nd Number: 5x^1 + 5x^0
Added polynomial: 5x^2 + 9x^1 + 7x^0
UNIT 1 Page 37
16CS3202 - Data Structures II Yr CSE / III Sem
Unit-II
STACK AND QUEUES
Syllabus:
2.1 Stack ADT
2.2 Queue ADT
2.3 circular queue implementation
2.4 Applications of stacks
2.5 Applications of queues
A stack is a list with the restriction that insertions and deletions can be performed in only one position,
namely, the end of the list, called the top. The fundamental operations on a stack are push, which is
equivalent to an insert, and pop, which deletes the most recently inserted element. The most recently
inserted element is available in top position. Stacks are also known as LIFO (last in, first out) lists.
UNIT 2 Page 1
16CS3202 - Data Structures II Yr CSE / III Sem
The codes to create empty stackand various operations on stack are as follows:
#define SIZE 10
int stack[SIZE], top = -1;
void push(int);
void pop();
void display();
UNIT 2 Page 2
16CS3202 - Data Structures II Yr CSE / III Sem
top--;
}
}
UNIT 2 Page 3
16CS3202 - Data Structures II Yr CSE / III Sem
In above example, the last inserted node is 99 and the first inserted node is 25. The order of elements
inserted is 25, 32,50 and 99.
Operations:
To implement stack using linked list, we need to set the following things before implementing actual
operations.
Step 1: Include all the header files which are used in the program. And declare all the user defined
functions.
Step 2: Define a 'Node' structure with two members such as data and next.
Step 3: Define a Node pointer 'top' and set it to NULL.
The codes to create empty stack are as follows:
struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();
UNIT 2 Page 4
16CS3202 - Data Structures II Yr CSE / III Sem
Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
Step 4: Then set 'top = top → next'.
Step 7: Finally, delete 'temp' (free(temp)).
The codes to delete existing element on stack are as follows:
void pop()
{
if(top == NULL)
cout<<"\nStack is Empty!!!\n";
else{
struct Node *temp = top;
cout<<”\nDeleted element:”<<temp->data;
top = temp->next;
free(temp);
}
}
UNIT 2 Page 5
16CS3202 - Data Structures II Yr CSE / III Sem
Example
After inserting 25, 30, 51, 60 and 85, queue is as follows:
Queue data structure can be implemented in two ways. They are as follows:
1. Array based implementation
2. Linked List based implementation
When a queue is implemented using array, that queue can organize only limited number of elements.
When a queue is implemented using linked list, that queue can organize unlimited number of elements.
(i) Array based implementation:
A queue data structure can be implemented using one dimensional array. The implementation of
queue using array contains one dimensional array of specific size, variables 'front' and 'rear'. Initially
both 'front' and 'rear' are set to -1. Whenever, we want to insert a new value into the queue, increment
'rear' value by one and then insert at that position. Whenever we want to delete a value from the queue,
then increment 'front' value by one and then delete the value at 'front' position.
Operations using Array:
The below steps are used to create an empty queue.
Step 1: Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
Step 2: Declare all the user defined functions which are used in queue implementation.
Step 3: Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'.
(int front = -1, rear = -1)
UNIT 2 Page 6
16CS3202 - Data Structures II Yr CSE / III Sem
queue[rear] = value.
The code to insert new element in queue are as follows:
void enQueue(int value){
if(rear == SIZE-1)
cout<<"\nQueue is Full!!! Insertion is not possible!!!";
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
cout<<"\nInsertion success!!!”;
}}
(b) deQueue() - Deleting a value from the Queue
In a queue data structure, deQueue() is a function used to delete an element from frontposition from the
queue. We can use the following steps to delete an element from the queue:
Step 1: Check whether queue is EMPTY. (front == rear)
Step 2: If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate
the function.
Step 3: If it is NOT EMPTY, then increment the front value by one (front ++). Then display
queue[front] as deleted element. Then check whether both front and rear are equal (front == rear), if
it TRUE, then set both front and rear to '-1' (front = rear = -1).
The code to delete an element in queue is as follows:
void deQueue(){
if(front == rear)
cout<<"\nQueue is Empty!!! Deletion is not possible!!!";
else{
cout<<"\nDeleted :"<< queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
(c)display() - Displays the elements of a Queue
We can use the following steps to display the elements of a queue.
Step 1: Check whether queue is EMPTY. (front == rear)
Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 3: Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value is
equal to rear (i<= rear)
The code to delete an element in queue is as follows:
void display(){
if(rear == -1)
cout<<"\nQueue is Empty!!!";
else{
int i;
UNIT 2 Page 7
16CS3202 - Data Structures II Yr CSE / III Sem
Example:
In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted node is 10
and it is pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.
Operations
To implement queue using linked list, we need to set the following things before implementing actual
operations.
Step 1:Declare all the user defined functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define two Node pointers 'front' and 'rear' and set both to NULL.
The code to create empty queue are as follows:
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void insert(int);
void delete();
void display();
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
cout<<"\nInsertion is Success!!!\n";
}
UNIT 2 Page 9
16CS3202 - Data Structures II Yr CSE / III Sem
Circular Queue is a linear data structure in which the operations are performed based on FIFO (First
In First Out) principle and the last position is connected back to the first position to make a
circle.Graphical representation of a circular queue is as follows:
UNIT 2 Page 10
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 2 Page 11
16CS3202 - Data Structures II Yr CSE / III Sem
if(front-1 == rear)
front = rear = -1;
}
}
Double Ended Queue is also a Queue data structure in which the insertion and deletion operations are
performed at both the ends (front and rear). That means, we can insert at both front and rear positions
and can delete from both front and rear positions.
UNIT 2 Page 12
16CS3202 - Data Structures II Yr CSE / III Sem
Double Ended Queue can be represented in TWO ways, those are as follows...
1. Input Restricted Double Ended Queue
2. Output Restricted Double Ended Queue
(i) Input Restricted Double Ended Queue
In input restricted double ended queue, the insertion operation is performed at only one end and deletion
operation is performed at both the ends.
The linear data structure stack can be used in the following situations.
1. It can be used to process function calls.
2. It can be used to check whether symbols are balanced in programming language statements
3. Converting and evaluating expressions.
(i) Balancing Symbols
In syntax error checking, a lack of one symbol(such as a missing brace or comment starter) can
cause the compiler to spill out a hundredlines of diagnostics. For simplicity,we will check for balancing
of parentheses, brackets, and braces using stack and ignore any othercharacter that appears.
The simple algorithm uses a stack and is as follows:
Make an empty stack. Read characters until end of file. If the character is an openingsymbol,
push it onto the stack. If it is a closing symbol and the stack is empty, reportan error. Otherwise, pop the
stack. If the symbol popped is not the correspondingopening symbol, then report an error. At end of file,
if the stack is not empty, report anerror.
UNIT 2 Page 13
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 2 Page 14
16CS3202 - Data Structures II Yr CSE / III Sem
(iii)Function Call:
When there is a function call, all the important information that needs to be saved, suchas register values
(corresponding to variable names) and the return address (which can beobtained from the program
counter, which is typically in a register), is saved on a stack. Then the control is transferredto the new
function, which is free to replace the registers with its values. If it makes otherfunction calls, it follows
the same procedure. When the function wants to return, it looksat the stack at the top and restores all the
registers. It then makes the returnjump.The information savedis called either an activation record or
stack frame. There is always the possibility that we will run out of stack spaceby having too many
simultaneously active functions.
(iv) Infix to Postfix Conversion:
We can use stack to convert an infix expression into postfix form. To know about the basics of
expression, following topics will be useful.An expression is a collection of operators and operands that
represents a specific value. Based on the operator position, expressions are divided into 3 types, such as
• Infix Expression
• Postfix Expression
• Prefix Expression
In infix expression, operator is used in between operands. For example, a+b. In postfix
expression, operator is used after operands. For example, ab+. In prefix expression, operator is used
before operands. For example, +ab.
To convert Infix Expression into Postfix Expression using a stack data structure, we can use the
following steps:
1. Read all the symbols one by one from left to right in the given Infix Expression.
2. If the reading symbol is operand, then directly print it to the result (Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective left
parenthesis is poped and print each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However, first pop the
operators which are already on the stack that have higher or equal precedence than current operator and
print them to the result.
Example:
Consider the following Infix Expression...
(A+B)*(C-D)
The given infix expression can be converted into postfix expression using Stack data Structure as
follows:
UNIT 2 Page 15
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 2 Page 16
16CS3202 - Data Structures II Yr CSE / III Sem
• When jobs are submitted to a printer, jobs sent to a printer are placed on a queue.
• Lines at ticketcounters are queues, because service is first-come first-served.
• Users on other machines are given access to files on a first-come first-served basis, so the data
structure is a queue.
• Further examples include the following:
1. Calls to large companies are generally placed on a queue when all operators are busy.
2. In large universities, where resources are limited, students must sign a waiting list ifall computers are
occupied. The student who has been at a computer the longest isforced off first, and the student who has
been waiting the longest is the next user to beallowed on.
UNIT 2 Page 17
16CS3202 - Data Structures II Yr CSE / III Sem
b. Using an Unordered Array (Dynamic Array) with the index of the maximum value
In this representation elements are inserted according to their arrival order and maximum
element is deleted first from max priority queue. For example, assume that elements are inserted in the
order of 8, 2, 3 and 5. And they are removed in the order 8, 5, 3 and 2.
UNIT 2 Page 18
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 2 Page 19
16CS3202 - Data Structures II Yr CSE / III Sem
• insert() - New element is added at a particular position in the increasing order into the queue
with O(n), because we need to shift existing elements inorder to insert new element in increasing
order. This operation requires O(n) time complexity.
• findMax() - To find maximum element in the queue is very simple as maximum element is at
the end of the queue. This operation requires O(1) time complexity.
• remove() - To remove an element from the queue first we need to perform findMax() which
requires O(1), removal of particular element requires constant time O(1) and rearrange
remaining elements which requires O(n). This operation requires O(n) time complexity.
f. Using Unordered Linked List with reference to node with the maximum value
In this representation, we use a single linked list to represent max priority queue. Always we
maintain a reference (maxValue) to the node with maximum value. In this representation elements are
inserted according to their arrival and node with maximum value is deleted first from max priority
queue. For example, assume that elements are inserted in the order of 2, 8, 3 and 5. And they are
removed in the order 8, 5, 3 and 2.
UNIT 2 Page 20
16CS3202 - Data Structures II Yr CSE / III Sem
• findMax() - To find maximum element in the queue is very simple as maxValue is referenced to
the node with maximum value in the queue. This operation requires O(1) time complexity.
• remove() - To remove an element from the queue is deleting the node which referenced by
maxValue which requires O(1) and update maxValue reference to new node with maximum
value in the queue which requires O(n) time complexity. This operation requires O(n) time
complexity.
UNIT 2 Page 21
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT – III
NON LINEAR DATA STRUCTURES-TREE
3.1 Tree ADT
3.2 Representation of trees
3.3 Binary Tree ADT
3.4 Expression trees
3.5 Applications of trees
3.6 BST ADT
3.7 Tree traversals
3.8 AVL Trees
3.9 B-Tree
3.10 Heaps
3.10.1 Binary heaps
3.10.2 Applications of binary heaps
3.11 Binomial heaps
Terminology
UNIT 4 Page 1
16CS3202 - Data Structures II Yr CSE / III Sem
2. Edge
In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
3. Parent
In a tree data structure, the node which is predecessor of any node is called as PARENT
NODE. In simple words, the node which has branch from it to any other node is called as parent node.
Parent node can also be defined as "The node which has child / children".
4. Child
UNIT 4 Page 2
16CS3202 - Data Structures II Yr CSE / III Sem
In a tree data structure, the node which is descendant of any node is called as CHILD Node. In
simple words, the node which has a link from its parent node is called as child node. In a tree, any
parent node can have any number of child nodes. In a tree, all the nodes except root are child nodes.
5. Siblings
In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple
words, the nodes with same parent are called as Sibling nodes.
6. Leaf
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a
node with no child. In a tree, leaf node is also called as 'Terminal' node.
UNIT 4 Page 3
16CS3202 - Data Structures II Yr CSE / III Sem
7. Internal Nodes
In a tree data structure, the node which has at least one child is called as INTERNAL Node. In
simple words, an internal node is a node with at least one child.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root
node is also said to be Internal Node if the tree has more than one node. Internal nodes are also called
as 'Non-Terminal' nodes.
8. Degree
In a tree data structure, the total number of children of a node is called as DEGREE of that
Node. In simple words, the Degree of a node is total number of children it has. The highest degree of a
node among all the nodes in a tree is called as 'Degree of Tree'
9. Level
In a tree data structure, the root node is said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In simple
words, in a tree each step from top to bottom is called as a Level and the Level count starts with '0' and
incremented by one at each level (Step).
UNIT 4 Page 4
16CS3202 - Data Structures II Yr CSE / III Sem
10. Height
In a tree data structure, the total number of edges from leaf node to a particular node in the
longest path is called as HEIGHT of that Node. In a tree, height of the root node is said to be height of
the tree. In a tree, height of all leaf nodes is '0'.
11. Depth
In a tree data structure, the total number of edges from root node to a particular node is called
as DEPTH of that Node. In a tree, the total number of edges from root node to a leaf node in the
longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf node in a
tree is said to be depth of that tree. In a tree, depth of the root node is '0'.
12. Path
UNIT 4 Page 5
16CS3202 - Data Structures II Yr CSE / III Sem
In a tree data structure, the sequence of Nodes and Edges from one node to another node is
called as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In
below example the path A - B - E - J has length 4.
UNIT 4 Page 6
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 4 Page 7
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 4 Page 8
16CS3202 - Data Structures II Yr CSE / III Sem
Hash Trees - used in p2p programs and specialized image-signatures in which a hash needs to be
verified, but the whole file is not available.
Syntax Tree - Constructed by compilers and (implicitly) calculators to parse expressions.
1. Array Representation
In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a
binary tree.
Consider the above example of binary tree and it is represented as follows...
To represent a binary tree of depth 'n' using array representation, we need one dimensional
array with a maximum size of 2n+1 - 1.
2. Linked List Representation
We use double linked list to represent a binary tree. In a double linked list, every node consists
of three fields. First field for storing left child address, second for storing actual data and third for
storing right child address.
In this linked list representation, a node has the following structure...
The above example of binary tree represented using Linked list representation is shown as follows...
UNIT 4 Page 9
16CS3202 - Data Structures II Yr CSE / III Sem
Insertion:
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->left=NULL;
temp->right=NULL;
return temp;
}
void insert(node *root,node *New)
{
charch;
New=get_node();
cout<<"\nEnter the Element";
cin>>New->data;
if(root==NULL)
root=New;
else
cout<<nWhere to insert left/right of %d"<<root->data;
ch=getche();
UNIT 4 Page 10
16CS3202 - Data Structures II Yr CSE / III Sem
if((ch=='r')||(ch=='r'))
{
if(root->right==NULL)
{
root->right=New;
}
else
insert(root->right,New);
}
else
{
if(root->left==NULL)
{
root->left=New;
}
else
insert(root->left,New);
}
}
UNIT 4 Page 11
16CS3202 - Data Structures II Yr CSE / III Sem
In this traversal, the left child node is visited first, then the root node is visited and later we go
for visiting right child node.
This in-order traversal is applicable for every root node of all subtrees in the tree. This is
performed recursively for all nodes in the tree.
That means here we have visited in the order of I - D - J - B - F - A - G - K - C - H using In-
Order Traversal.
In-Order Traversal for above example of binary tree is
I-D-J-B-F-A-G-K-C–H
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
voidinorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
cout>>temp->data;
inorder(temp->right);
}
}
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
voidpreorder(node *temp)
{
if(temp!=NULL)
UNIT 4 Page 12
16CS3202 - Data Structures II Yr CSE / III Sem
{
printf("%d",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
voidpostorder(node *temp)
{
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("%d",temp->data);
}
}
UNIT 4 Page 13
16CS3202 - Data Structures II Yr CSE / III Sem
Inorder traversal of expression tree produces infix version of given postfix expression (same
with preorder traversal it gives prefix expression)
This image can help you in understanding the main principles of Expression tree:
UNIT 4 Page 14
16CS3202 - Data Structures II Yr CSE / III Sem
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains
nodes with smaller values and right subtree of every node contains larger values.
Every Binary Search Tree is a binary tree but all the Binary Trees need not to be binary search
trees.
UNIT 4 Page 15
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 4 Page 16
16CS3202 - Data Structures II Yr CSE / III Sem
structtree_node
{
tree_node *left;
tree_node *right;
int data;
};
classbst
{
tree_node *root;
public:
bst()
UNIT 4 Page 17
16CS3202 - Data Structures II Yr CSE / III Sem
{
root=NULL;
}
intisempty()
{
return(root==NULL);
}
};
voidbst::insert(int no)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=no;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(no>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(no<parent->data)
parent->left=p;
else
parent->right=p;
}
}
UNIT 4 Page 18
16CS3202 - Data Structures II Yr CSE / III Sem
Step 3: If both are matching, then display "Given node found!!!" and terminate the function
Step 4: If both are not matching, then check whether search element is smaller or larger than
that node value.
Step 5: If search element is smaller, then continue the search process in left subtree.
Step 6: If search element is larger, then continue the search process in right subtree.
Step 7: Repeat the same until we found exact element or we completed with a leaf node
Step 8: If we reach to the node with search value, then display "Element is found" and
terminate the function.
Step 9: If we reach to a leaf node and it is also not matching, then display "Element not found"
and terminate the function.
UNIT 4 Page 19
16CS3202 - Data Structures II Yr CSE / III Sem
Balanced factor:
The balanced factor BF (T) of a node in binary tree is defined as to be h L – hR where HL and HR
are heights of left and right sub trees of T.
The AVL tree follows the property of binary search tree. In fact AVL trees are basically binary
search tress with balance factor as -1, 0 or +1.
After insertion of any node in an AVL tree if the balance factor of any node becomes other than
-1, 0 or +1then it is said AVL property is violated.
BalanceFactor = height(left-subtree) − height(right-subtree)
Insertion:
1) Left sub tree of Left child of pivot node SINGLE RIGHT ROTATION
2) Right sub tree of Right child of pivot node SINGLE LEFT ROTATION
3) Left sub tree of Right child of pivot node LEFT RIGHT ROTATION
4) Right sub tree of Left child of pivot node RIGHT LEFT ROTATION
AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of rotations −
a) Left rotation
b) Right rotation
c) Left-Right rotation
d) Right-Left rotation
UNIT 4 Page 20
16CS3202 - Data Structures II Yr CSE / III Sem
The first two rotations are single rotations and the next two rotations are double rotations. To have
an unbalanced tree, we at least need a tree of height 2. With this simple tree, let's understand them one
by one.
Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree,
then we perform a single left rotation −
In our example, node A has become unbalanced as a node is inserted in the right subtree of A's
right subtree. We perform the left rotation by making Athe left-subtree of B.
Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree.
The tree then needs a right rotation.
As depicted, the unbalanced node becomes the right child of its left child by performing a right
rotation.
Left-Right Rotation
Double rotations are slightly complex version of already explained versions of rotations. To
understand them better, we should take note of each action performed while rotation. Let's first check
how to perform Left-Right rotation. A left-right rotation is a combination of left rotation followed by
right rotation.
State Action
A node has been inserted into the right subtree of the left
subtree. This makes C an unbalanced node. These scenarios
cause AVL tree to perform left-right rotation.
UNIT 4 Page 21
16CS3202 - Data Structures II Yr CSE / III Sem
We shall now right-rotate the tree, making B the new root node
of this subtree. C now becomes the right subtree of its own left
subtree.
Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a combination of right rotation
followed by left rotation.
State Action
A node has been inserted into the left subtree of the right
subtree. This makes A, an unbalanced node with balance factor
2.
UNIT 4 Page 22
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 4 Page 23
16CS3202 - Data Structures II Yr CSE / III Sem
Insert 3
Insert 5
Insert 11
While inserting the new node 11 as right sub tree of right child the Balance Factor
condition -1,0,+1 is not satisfying in the node 3, so we going for the single rotation RR
Insert 8
UNIT 4 Page 24
16CS3202 - Data Structures II Yr CSE / III Sem
Insert 4,1
Insert 12
Insert 7
UNIT 4 Page 25
16CS3202 - Data Structures II Yr CSE / III Sem
Insert 2
Insert 6
While inserting the new node 6 as right sub tree of left child the Balance Factor
condition -1,0,+1 is not satisfying in the node 11 & 8, so we going for the single
rotation LL
UNIT 4 Page 26
16CS3202 - Data Structures II Yr CSE / III Sem
Insert 10
While inserting the new node 10 as right sub tree of left child the Balance Factor
condition -1,0,+1 is not satisfying in the node 11, so we going for the Double rotation
RL
UNIT 4 Page 27
16CS3202 - Data Structures II Yr CSE / III Sem
3.9 B-Tree
B-Tree was developed in the year of 1972 by Bayer and McCreight with the name Height
Balanced m-way Search Tree. Later it was named as B-Tree.
B – Tree is specialized multiway tree used to store the records in a disk. There are number of
sub trees to each node. So that the height of the tree is relatively small. So that only small number of
nodes must be read from disk to retrieve an item.
B-Tree of Order m has the following properties:
Here, number of keys in a node and number of children for a node is depend on the order of the B-
Tree. Every B-Tree has order.
a) All the leaf nodes are on the bottom level and all the leaf nodes must be at same level.
b) The node which has n children possess (n – 1) keys and All nodes except root must have
at least [m/2]-1 keys.
c) The keys in each node are in ascending order.
d) The node should have at least two children, for every node, child [0] has only keys
which are less then node. Key [0] similarly, node. Child [1] has only keys which are greater than node.
Key [0].
e) All the internal nodes except root node have at least ceil (m/2) non empty children. The
ceil is a function such that ceil (3.4) = 4, ceil (9.3) = 10.
f) If the root node is a non-leaf node, then it must have at least 2 children and each leaf
node must contain at least ceil (m/2) – 1 keys.
g) All the key values within a node must be in Ascending Order.
For example, B-Tree of Order 5 contains maximum 4 key values in a node and maximum 5
children for a node.
Insertion:
We will construct a B – tree of order 5 following numbers:
3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12, 20, 26, 4, 16, 18, 24, 25, 19
The internal node should have at least 3 non empty children and each leaf node must contain at
least 2 keys.
UNIT 4 Page 28
16CS3202 - Data Structures II Yr CSE / III Sem
Insert 78
Insert 21
Insert 14
Insert 11
Insert 97
The Condition fails as the order of B – tree is 5, so 4 keys and 5 child should be there. So we going to
split the B- tree into two half
UNIT 4 Page 29
16CS3202 - Data Structures II Yr CSE / III Sem
Insert 85
Insert 74
Insert 63
The Condition fails as the order of B – tree is 5, so 4 keys and 5 child should be there. So we going to
split the B- tree into two half
UNIT 4 Page 30
16CS3202 - Data Structures II Yr CSE / III Sem
Insert 45
Insert 42
Insert 57
The Condition fails as the order of B – tree is 5, so 4 keys and 5 child should be there. So we going to
split the B- tree into two half
Insert 20
UNIT 4 Page 31
16CS3202 - Data Structures II Yr CSE / III Sem
Insert 16
Insert 19
The Condition fails as the order of B – tree is 5, so 4 keys and 5 child should be there. So we going to
split the B- tree into two half
Insert 32
Insert 32
& 32
The Condition fails as the order of B – tree is 5, so 4 keys and 5 child should be there. So we going to
split the B- tree into two half
UNIT 4 Page 32
16CS3202 - Data Structures II Yr CSE / III Sem
The Condition fails as the order of B – tree is 5, so 4 keys and 5 child should be there. So we going to
split the B- tree into two half
UNIT 4 Page 33
16CS3202 - Data Structures II Yr CSE / III Sem
3.10 Heaps
3.10.1 Binary Heap
Heap is a complete binary tree or a almost complete binary tree in which every parent node be
either greater or lesser than its child nodes.
In this Heap can be MIN or MAX.
MAX Heap in the tree in which each node is greater than or equal to the value of the
children node
MIN Heap in the tree in which each node is lesser than or equal too the value of the
children nodes.
MAX Heap:
In MAX Heap every parent node is greater than or equal to than its child node.
18
13 4
11 10
MIN Heap:
In MIN Heap every parent node is lesser than its children.
13 14
12 20
Parental Property
Parent node being greater or lesser in heap so its called parental property.
Heap having two important property
Heap should satisfy following two conditions.
It should be a complete binary tree or almost a complete binary tree
Its should satisfy the parental property
UNIT 4 Page 34
16CS3202 - Data Structures II Yr CSE / III Sem
18 18
18
13 4 13 4
13 4
11 10
11 10
In A:Binary tree
satisfying properties, its level 2 and 1 but left and right child is present for each level.
10
A B C
In B: Binary tree doesn't satisfying the 2nd property than the leaves at level 3 and level 1 are
not adjacent. So its not complete binary tree.
In C: Binary tree doesn't satisfying the 1st property than 2nd level but only right child is
present.
UNIT 4 Page 35
16CS3202 - Data Structures II Yr CSE / III Sem
18
18 13 4 11 10
13 4
11 10
To insert the element 19 in the given tree
18 18
13 4 13 4
11 10 11 10 19
18
18
13 4
13 19
11 10 19
11 10 4
UNIT 4 Page 36
16CS3202 - Data Structures II Yr CSE / III Sem
18 19
13 19 13 18
11 10 4 11 10 4
19 19
13 18 13 18
4 4 4
11 10 13 4 18 13 18 13 18
11 10 4
11 10 19 11 10 19 11 10
13 18
UNIT 4 11 10 Page 37
16CS3202 - Data Structures II Yr CSE / III Sem
18
13 4
11 10
UNIT 4 Page 38
16CS3202 - Data Structures II Yr CSE / III Sem
Binomial Heap:
A Binomial Heap is a set of Binomial Trees where each Binomial Tree follows Min Heap
property. And there can be at-most one Binomial Tree of any degree.
UNIT 4 Page 39
16CS3202 - Data Structures II Yr CSE / III Sem
(00001101), hence 3 Binomial Trees. We can also relate degree of these Binomial Trees with positions
of set bits. With this relation we can conclude that there are O(Logn) Binomial Trees in a Binomial
Heap with ‘n’ nodes.
UNIT 4 Page 40
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 4 Page 41
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 1
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 2
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 3
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 4
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 5
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 6
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 7
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 8
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 9
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 10
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 11
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 12
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 13
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 14
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 15
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 16
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 17
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 18
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 19
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 20
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 21
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 22
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 23
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 24
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 25
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 26
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 27
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 28
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 29
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 30
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 31
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 32
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 33
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 34
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 35
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 36
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 37
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 38
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 39
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 40
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 41
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 42
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 43
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 44
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 45
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 46
16CS3202 - DATA STRUCTURES - NOTES
UNIT 4 Page 47
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT - V
SORTING, SEARCHING AND HASHING
5.1 Sorting algorithms:
5.1.1 Insertion sort
5.1.2 Selection sort
5.1.3 Shell sort
5.1.4 Bubble sort
5.1.5 Quick sort
5.1.6 Merge sort
5.1.7 Radix sort
5.2 Searching:
5.2.1 Linear search
5.2.2 Binary Search
5.3 Hashing
5.4 Separate chaining
5.5 Open addressing
5.6 Rehashing
5.7 Extendible hashing
5.1 SORTING
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange
data in a particular order. Most common orders are in numerical or lexicographical order.
The importance of sorting lies in the fact that data searching can be optimized to a very high level, if
data is stored in a sorted manner. Sorting is also used to represent data in more readable formats.
Following are some of the examples of sorting in real-life scenarios.
Telephone Directory − the telephone directory stores the telephone numbers of people sorted by
their names, so that the names can be searched easily.
Dictionary − the dictionary stores words in an alphabetical order so that searching of any word
becomes easy.
Types of Sorting Techniques
There are many types of Sorting techniques, differentiated by their efficiency and space requirements.
Following are some sorting techniques which we will be covering in next sections.
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Shell Sort
5. Quick Sort
6. Merge Sort
7. Radix Sort
UNIT 5 Page 1
16CS3202 - Data Structures II Yr CSE / III Sem
5.1.1 Insertion Sort
It is a simple Sorting algorithm which sorts the array by shifting elements one by one. Following are some
of the important characteristics of Insertion Sort.
1. It has one of the simplest implementation
2. It is efficient for smaller data sets, but very inefficient for larger lists.
3. Insertion Sort is adaptive, that means it reduces its total number of steps if given a partially sorted
list, hence it increases its efficiency.
4. It is better than Selection Sort and Bubble Sort algorithms.
5. Its space complexity is less. Like Bubble Sorting, insertion sort also requires a single additional
memory space.
6. It is a Stable sorting, as it does not change the relative order of elements with equal keys
How Insertion Sort Works?
We take an unsorted array for our example.
It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-
list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted after
swapping.
UNIT 5 Page 2
16CS3202 - Data Structures II Yr CSE / III Sem
By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.
So we swap them.
We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
This process goes on until all the unsorted values are covered in a sorted sub-list.
Program for Insertion Sort
#include <stdlib.h>
#include <iostream.h>
using namespace std;
//member functions declaration
void insertionSort(int arr[], int length);
void printArray(int array[],int size);
UNIT 5 Page 3
16CS3202 - Data Structures II Yr CSE / III Sem
int main() {
int array[5]= {5,4,3,2,1};
insertionSort(array,5);
return 0;
}
void insertionSort(int arr[], int length) {
int i, j ,tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
printArray(arr,5);
}
}
void printArray(int array[], int size){
cout<< "Sorting tha array using Insertion sort... ";
int j;
for (j=0; j < size;j++)
for (j=0; j < size;j++)
cout <<" "<< array[j];
cout << endl;
}
UNIT 5 Page 4
16CS3202 - Data Structures II Yr CSE / III Sem
Consider the following depicted array as an example.
For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14 is
stored presently, we search the whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list,
appears in the first position of the sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.
We find that 14 is the second lowest value in the list and it should appear at the second place. We swap
these values.
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array.
Following is a pictorial depiction of the entire sorting process −
UNIT 5 Page 5
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 5 Page 6
16CS3202 - Data Structures II Yr CSE / III Sem
for (i = 0; i < n; ++i)
{
for (j = i+1; j < n; ++j)
{
//Comparing consecutive data and switching values if i > j.
if (arr[i] > arr[j])
{
arr[i] = arr[i]+arr[j];
arr[j] = arr[i]-arr[j];
arr[i] = arr[i]-arr[j];
}
}
// Value at i will be minimum of all the value above this index.
}
}
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
SelectionSort(arr, n);
// Display the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
UNIT 5 Page 7
16CS3202 - Data Structures II Yr CSE / III Sem
5.1.3 Shell Sort
Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm
avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to
the far left.
This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts the less
widely spaced elements. This spacing is termed as interval. This interval is calculated based on Knuth's
formula as −
Knuth's Formula
h=h*3+1
where − h is interval with initial value 1
We compare values in each sub-list and swap them (if necessary) in the original array. After this step, the
new array should look like this –
Then, we take interval of 2 and this gap generates two sub-lists - {14, 27, 35, 42}, {19, 10, 33, 44}
UNIT 5 Page 8
16CS3202 - Data Structures II Yr CSE / III Sem
We compare and swap the values, if required, in the original array. After this step, the array should look
like this –
Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sort to sort the
array.
Following is the step-by-step depiction −
UNIT 5 Page 9
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 5 Page 10
16CS3202 - Data Structures II Yr CSE / III Sem
{
// Start with a big gap, then reduce the gap
for (int gap = n/2; gap > 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order
// keep adding one more element until the entire array is
// gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap sorted
// save a[i] in temp and make a hole at position i
int temp = arr[i];
UNIT 5 Page 11
16CS3202 - Data Structures II Yr CSE / III Sem
shellSort(arr, n);
cout << "\nArray after sorting: \n";
printArray(arr, n);
return 0;
}
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
UNIT 5 Page 12
16CS3202 - Data Structures II Yr CSE / III Sem
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the array. After one iteration, the array
should look like this −
To be precise, we are now showing how an array should look like after each iteration. After the second
iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely sorted.
Algorithm
We assume list is an array of n elements. We further assume that swap function swaps the values of the
given array elements.
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
UNIT 5 Page 13
16CS3202 - Data Structures II Yr CSE / III Sem
end BubbleSort
Program:
#include <iostream>
using namespace std;
// Sort arr[] of size n using Bubble Sort.
void BubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1; ++j)
{
// Comparing consecutive data and switching values if value at
j > j+1.
if (arr[j] > arr[j+1])
{
arr[j] = arr[j]+arr[j+1];
arr[j+1] = arr[j]-arr[j + 1];
arr[j] = arr[j]-arr[j + 1];
}
}
// Value at n-i-1 will be maximum of all the values below this index.
}
}
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
BubbleSort(arr, n);
UNIT 5 Page 14
16CS3202 - Data Structures II Yr CSE / III Sem
// Display the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
UNIT 5 Page 15
16CS3202 - Data Structures II Yr CSE / III Sem
using namespace std;
// Swapping two values.
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
// Partitioning the array on the basis of values at high as pivot value.
int Partition(int a[], int low, int high)
{
int pivot, index, i;
index = low;
pivot = high;
// Getting index of pivot.
for(i=low; i < high; i++)
{
if(a[i] < a[pivot])
{
swap(&a[i], &a[index]);
index++;
}
}
// Swapping value at high and at the index obtained.
swap(&a[pivot], &a[index]);
return index;
}
// Random selection of pivot.
int RandomPivotPartition(int a[], int low, int high)
{
int pvt, n, temp;
n = rand();
// Randomizing the pivot value in the given subpart of array.
pvt = low + n%(high-low+1);
// Swapping pvt value from high, so pvt value will be taken as pivot while
partitioning.
UNIT 5 Page 16
16CS3202 - Data Structures II Yr CSE / III Sem
swap(&a[high], &a[pvt]);
return Partition(a, low, high);
}
// Implementing QuickSort algorithm.
int QuickSort(int a[], int low, int high)
{
int pindex;
if(low < high)
{
// Partitioning array using randomized pivot.
pindex = RandomPivotPartition(a, low, high);
// Recursively implementing QuickSort.
QuickSort(a, low, pindex-1);
QuickSort(a, pindex+1, high);
}
return 0;
}
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
QuickSort(arr, 0, n-1);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
UNIT 5 Page 17
16CS3202 - Data Structures II Yr CSE / III Sem
Merge Sort follows the rule of Divide and Conquer. In merge sort the unsorted list is divided into N
sublists, each having one element, because a list consisting of one element is always sorted. Then, it
repeatedly merges these sublists, to produce new sorted sublists, and in the end, only one sorted list is
produced.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
We know that merge sort first divides the whole array iteratively into equal halves unless the atomic
values are achieved. We see here that an array of 8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original. Now we divide these two arrays
into halves.
We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note the color
codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted manner. We
see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target list of 2 values we put
10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two data values, and merge them into a
list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
UNIT 5 Page 18
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 5 Page 19
16CS3202 - Data Structures II Yr CSE / III Sem
while (j <= high)
{
temp[k] = a[j];
k++;
j++;
}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{
a[i] = temp[i-low];
}
}
// A function to split array into two parts.
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
// Split the data into two half.
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
UNIT 5 Page 20
16CS3202 - Data Structures II Yr CSE / III Sem
MergeSort(arr, 0, n-1);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
5.2 SEARCHING
Searching is an operation or a technique that helps finds the place of a given element or value in
the list. Any search is said to be successful or unsuccessful depending upon whether the element that is
being searched is found or not. Some of the standard searching technique that is being followed in data
structure is listed below:
UNIT 5 Page 21
16CS3202 - Data Structures II Yr CSE / III Sem
Linear Search or Sequential Search
Binary Search
Example:
The list given below is the list of elements in an unsorted array. The array contains 10 elements. Suppose
the element to be searched is ’46’, so 46 is compared with all the elements starting from the 0 th element
and searching process ends where 46 is found or the list ends.
The performance of the linear search can be measured by counting the comparisons done to find out an
element.
Program for Linear Search:
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
int main()
{
int arr_search[MAX_SIZE], i, element;
cout << "Simple C++ Linear Search Example - Array\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Searching : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_search[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++)
{
cout << "\t" << arr_search[i];
UNIT 5 Page 22
16CS3202 - Data Structures II Yr CSE / III Sem
}
cout << "\nEnter Element to Search : ";
cin>>element;
/* for : Check elements one by one - Linear */
for (i = 0; i < MAX_SIZE; i++)
{
/* If for Check element found or not */
if (arr_search[i] == element)
{
cout << "\nLinear Search : Element : " << element << " : Found :
Position : " << i + 1 << ".\n";
break;
}
}
if (i == MAX_SIZE)
cout << "\nSearch Element : " << element << " : Not Found \n";
getch();
}
UNIT 5 Page 23
16CS3202 - Data Structures II Yr CSE / III Sem
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the
value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array,
so we also know that the target value must be in the upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high-low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is more than what we are looking for. So, the value
must be in the lower part from this location.
We compare the value stored at location 5 with our target value. We find that it is a match.
UNIT 5 Page 24
16CS3202 - Data Structures II Yr CSE / III Sem
Binary search halves the searchable items and thus reduces the count of comparisons to be made to very
less numbers.
Program for Binary Search:
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
int main()
{
int arr_search[MAX_SIZE], i, element;
int f = 0, r = MAX_SIZE, mid;
cout << "Simple C++ Binary Search Example - Array\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Searching : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_search[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++)
{
cout << "\t" << arr_search[i];
}
cout << "\nEnter Element to Search : ";
cin>>element;
while (f <= r)
{
mid = (f + r) / 2;
if (arr_search[mid] == element)
{
cout << "\nSearch Element : " << element << " : Found : Position : " <<
mid + 1 << ".\n";
break;
}
else if (arr_search[mid] < element)
f = mid + 1;
else
r = mid - 1;
}
if (f > r)
UNIT 5 Page 25
16CS3202 - Data Structures II Yr CSE / III Sem
cout << "\nSearch Element : " << element << " : Not Found \n";
getch();
}
5.3 HASHING
• It is a technique used for performing insertions, deletions and search operation in constant average time
by implementing Hash table data structure.
• Instead of comparisons, it uses a mathematical function
Types of hashing
1.Static hashing
– the hash function maps search key value to a fixed set of locations
2.Dynamic hashing
– the hash table can grow to handle more items at run time.
Hash table
• The hash table data structure is an array of some fixed size table, containing the keys. A key value is
associated with each record. A hash table is partitioned into array of buckets. Each bucket has many slots
and each slot holds one record
Hashing functions
• A hashing function is a key-to-address transformation which acts upon a given key to compare the
relative position of the key in the hash table.
• A key can be a number, string, record, etc.
• A simple hash function
– Hash (Key) = (Key) Mod (Table-size)
– For example, if the key is 24 and the table size is 5, then
– Hash (24) = 24 % 5 = 4
– The key value “24” is placed in the relative location “4” in the hash table
Hash function
• A good Hash Function should
– Minimize collisions
– Be easy and quick to compute
– Distribute keys evenly in the hash table
– Use all the information provided in the key
• Routine for simple Hash Function
Hash(char *key, int Table_size)
UNIT 5 Page 26
16CS3202 - Data Structures II Yr CSE / III Sem
{
int Hash_value = 0;
while(*key != „10‟)
Hash_value = Hash_value + *key;
*key++;
return (Hash_value % Table_size);
}
Methods of Hashing Function
1. Mid square method
2. Modulo division or division remainder
3. Folding method
4. Pseudo random number generator method
5. Digit or character extraction method
6. Radix transformation
Index Slot
0 4
For example:
UNIT 5 Page 27
16CS3202 - Data Structures II Yr CSE / III Sem
– Map the key 4 into a hash table of size 4
– H(4) = 4 % 4 = 0
3. Folding method
This method involves splitting keys into two or more parts each of which has the same length as
the required address and then adding the parts to form the hash function
Two types
o Fold shifting method
o Fold boundary method
Fold shifting method
o key = 123203241
o Partition key into 3 parts of equal length.
o 123, 203 & 241
o Add these three parts
o 123+203+241 = 567 is the hash value
Fold boundary method
o Similar to fold shifting except the boundary parts are reversed
o 123203241 is the key
o 3 parts 123, 203, 241
o Reverse the boundary partitions
o 321, 203, 142
o 321 + 203 + 142 = 666 is the hash value
4. Pseudo random number generator method
This method generates random number given a seed as parameter and the resulting random number
then scaled into the possible address range using modulo division. The random number produced can be
transformed to produce a hash value.
6. Radix transformation
UNIT 5 Page 28
16CS3202 - Data Structures II Yr CSE / III Sem
In this method , a key is transformed into another number base
Example :
Map the key (8465)10 using base 15
Now (8465)10 = (2795)15
Now the hash value is 2795
Applications of Hash tables
Database systems
Symbol tables
Data dictionaries
Network processing algorithms
Browse caches
COLLISION
Collision occurs when a hash value of a record being inserted hashes to an address that already contain a
different record. (i.e) when two key values hash to the same position.
Example : 37, 24 , 7
Index Slot
2 37
4 24
• 37 is placed in index 2
• 24 is placed in index 4
• Now inserting 7
• Hash (7) = 7 mod 5 = 2
• 2 collides
Collision Resolution strategies
The process of finding another position for the collide record is called Collision Resolution strategy.
Two categories
1. Open hashing - separate chaining
UNIT 5 Page 29
16CS3202 - Data Structures II Yr CSE / III Sem
Each bucket in the hash table is the head of a linked list. All elements that hash to same
value are linked together.
2. Closed hashing - Open addressing, rehashing and extendible hashing.
Collide elements are stored at another slot in the table.
It ensures that all elements are stored directly into the hash table.
L →Next = Newcell;
UNIT 5 Page 30
16CS3202 - Data Structures II Yr CSE / III Sem
}
}
}
Position Find(int key, Hashtable H)
{
Position P;
List L;
L=H→TheLists[Hash(key,H →Tablesize)];
P = L →Next;
while(P!=NULL && P →Element !=key)
P=P →Next;
return P;
}
Advantages and Disadvantages
• Advantages
– More number of elements can be inserted as it uses array of linked lists.
•
– Collision resolution is simple and efficient.
• Disadvantages
– It requires pointers that occupy more space.
– It takes more effort to perform search, since it takes time to evaluate the hash function and also
to traverse the list.
UNIT 5 Page 31
16CS3202 - Data Structures II Yr CSE / III Sem
= Hash(X) + i mod Tablesize
Example: To insert 42,39,69,21,71,55 to the hash table of size 10 using linear probing
1. H0(42) = 42 % 10 = 2
2. H0(39) = 39 %10 = 9
3. H0(69) = 69 % 10 = 9 collides with 39
H1(69) = (9+1) % 10 = 10 % 10 = 0
4. H0(21) = 21 % 10 = 1
5. H0(71) = 71 % 10 = 1 collides with 21
0 69 69 69 69
1 21 21 21
2 42 42 42 42 42 42
3 71 71
5 55
9 39 39 39 39 39
• Advantages
– It doesn‟t require pointers
• Disadvantages
– It forms clusters that degrades the performance of the hash table
b) Quadratic probing
• Based on quadratic function i.e., F(i) = i
UNIT 5 Page 32
16CS3202 - Data Structures II Yr CSE / III Sem
• Hi(x) =Hash(X) + F(i) mod Table size
Example: To insert 89, 18, 49, 58, 69 to the hash table of size 10 using quadratic probing
1. H0(89) = 89 %10 =9
2. H0(18) = 18 %10 =8
3. H0(49) = 49 %10 =9 collides with 89
4. H1(49) = (9+12) % 10 = 10 % 10 = 0
H2(58) = (8 + 22) % 10 = 12 % 10 = 2
H2(69) = (9 + 22) % 10 = 13 % 10 = 3
0 49 49 49
2 58 58
3 69
5 55
8 18 18 18 18
9 89 89 89 89 89
Limitations:
• It faces secondary clustering that is difficult to find the empty slot if the table is half full.
UNIT 5 Page 33
16CS3202 - Data Structures II Yr CSE / III Sem
C) Double Hashing
• It uses the idea of applying a second hash function to the key when a collision occurs.
• The result of the second hash function will be the number of positions from the point of
collision to insert.
• F(i) = i * Hash2(X)
• Hi(x) = (Hash(X) + F(i)) mod Tablesize
Hi(x) = (Hash(X) + i * Hash2(X) ) mod Tablesize A popular second hash function is
Hash2(X) = R – (X % R)
where R is a prime number
• Insert : 89, 18, 49, 58, 69 using Hash2(X) = R – (X % R) and R = 7
• Open addressing hash table using double hashing
0 69
3 58 58
6 49 49 49
8 18 18 18 18
9 89 89 89 89 89
1. H0(89) = 89 % 10 = 9
2. H0(18) = 18 % 10 = 8
UNIT 5 Page 34
16CS3202 - Data Structures II Yr CSE / III Sem
H1(49) = ((49 % 10 ) + 1 * (7- (49 % 7)) ) % 10
=16 % 10 = 6
4. H0(58) = 58 % 10 = 8 collides with 18
5.6 REHASHING
It is a closed hashing technique.
If the table gets too full, then the rehashing method builds new table that is about twice as big and
scan down the entire original hash table, comparing the new hash value for each element and
inserting it in the new table.
Rehashing is very expensive since the running time is O(N), since there are N elements to rehash
and the table size is roughly 2N
Rehashing can be implemented in several ways like
a. Rehash , as soon as the table is half full
b. Rehash only when an insertion fails
Routine for rehashing
HashTable Rehash(HashTable H)
{
int i, oldsize;
cell *oldcells;
oldcells = H→Thecells;
oldsize = H → Table_size;
H= InitializeTable(2*oldsize);
for (i=0;i<oldsize; i++)
if (oldcells[i].Info==Legitimate)
Insert(oldcells[i].Element.H);
free(oldcells);
return H;
}
Example : Suppose the elements 13, 15, 24, 6 are inserted into an open addressing hash table of size 7 and
if linear probing is used when collision occurs.
UNIT 5 Page 35
16CS3202 - Data Structures II Yr CSE / III Sem
Index Slot
0 6
1 15
3 24
6 13
Index Slot
0 6
1 15
2 23
3 24
6 13
• A new table is created. The size of the new table is 17, as this is the first prime number that is
twice as large as the old table size.
Index Slot
UNIT 5 Page 36
16CS3202 - Data Structures II Yr CSE / III Sem
5
6 6
7 23
8 24
10
11
12
13 13
14
15 15
16
Advantages
• Programmer doesn‟t worry about the table size
• Simple to implement
UNIT 5 Page 37
16CS3202 - Data Structures II Yr CSE / III Sem
Similarly if the key 000000 is to be inserted, then the first leaf is split into 2 leaves.
UNIT 5 Page 38
16CS3202 - Data Structures II Yr CSE / III Sem
• Advantages
– Provides quick access times for insert and find operations on large databases.
• Disadvantages
– This algorithm does not work if there are more than M duplicates
UNIT 5 Page 39