You are on page 1of 42

Unit 1 Introduction

1. Abstract Data Types


 The data type is basically the type of data that used in different computer program
 It includes integer, float etc
 The integer can take 4-bytes, character 1-byte etc based on the computer
 The abstract datatype is special kind of database that behavior is defined by the set of values
and set of operations
 ADT means to hide how the operation is performed on the data
 It shows only essential information to the users
 In other words the user of on ADT needs only to know that a set of operations are available
for the data type but does not need to know how they are applied
 Many programming language already define some simple ADT’s as integral parts of the
language

Operations of Abstract Data Types


 Operations in Abstract Data types are
 Stack
 Queue
 List

Stack
 FULL()
 Used to check whether stack is full or not
 Empry()
 Used to check whether stack is empty or not
 Push()
 Used to push x into the stack
 Pop()
 Used to get the top most element of the stack
 Size()
 Used to get number of elements present into the stack

Queue
 FULL()
 Used to check whether queue is full or not
 Empry()
 Used to check whether queue is empty or not
 Insert()
 Used to add element into the queue at the rear end
 Delete()
 Used to delete one element from the front end of the queue
 Size()
 Used to get number of elements present into the queue

List
 Size()
 Used to get number of elements present into the list
 Insert
 Used to insert one element into the list
 Remove()
 Used to remove given element from the list
 Get()
 Used to get element at position i
Unit 2 Linear Data structures
1. Stack
 Stack allows operations on data only at one end
 It allows access to the last inserted data only
 The stack is called as LIFO ( Last In First Out ) data
structure
 The operations like push, pop are used only to push item
and pop the item

Basic operations
 The basic operations are
 Push
 Push the element at the top of the stack
 Pop
 Pop an element from the top of the stack
 Peek
 Get the top element of the stack
 isFull
 Check if stack if full
 isEmpty
 Check if the stack is empty

Push
 Insert the element into stack at the top of the storage
 It increments the top index
 The storage is full then error message is shown

Pop
 Pop is used to remove the element from the stack
 It retrieves the element from the top of the storage and decrements the top index

Pseudo code
Program
2. Queue
 Queue is similar to stack
 It is also an abstract data structure
 The stack follows FIFO ( First In First Out )
 The data item inserted first will be accessed first
 The data is inserted into the queue through one end and deleted from it using the other end

Operations
 The operation in queue includes
 Enqueue()
 It is the data manipulation operation that is used to insert elements into the
stack
 Dequeue()
 It is the data manipulation operation that is used to remove elements from
the stack
 Peek()
 It is used to retrieve the front most element
 It is used to check the status of the queue with the help of pointer
 isFull()
 It is used operation verifies whether the queue is full
 isEmpty()
 It verifies the queue is empty
 It is used to check the status of the stack with the help of top pointer
 The operations initialize of queue, usage and permanently delete the data from the memory
 The build in operations carry out data manipulation and check the status of the queue
 Queue use the pointers like
 Front
 Rear
 The front pointer access the data from the front end ( enqueueing ) and the rear pointer
access data from the rear end ( dequeuing )

Pseudo code

Program

Output
3. Difference queue and priory queue
Queue Priority queue
First in first out Order based on priority
Linear data structure Binary heap
Element added at the rear Elements inserted based on priority
Elements removed form the front Highest priority element is removed
No use for priority Uses priority for insertion, deletion
Used in printing jobs Used in dijkstra algorithm
Simple and efficient for basic operations Efficiency depends on underlying
implementation
Implemented using array, linked list Implemented using various data structure
Ordered in sense of arrival Ordered based on priority
Enqueue, dequeue Enqueue and dequeue done based on priority

4. Queue using list

5. Arrays
 Array is a type of linear data structure
 It is defined as the collection of elements
with same or different data structure
 It exist in both single and multiple dimension
 It is used to store multiple elements of
similar data ( similar data type ) together at
one place
 The difference between array index and the
memory address that the array index acts like value to label the elements in the array
 Element - item stored in the array is called an element
 Index - location of element in array has the numerical index that is used to
identify the element
 Array is used as solution for many problems from the small sorting problems to more
complex
 The time complexity of array is O(1)
 Array comes with pointer and an offset value
 The pointer points the location of the memory and the offset value
 Syntax:

 The index of the array start with 0 and ends at n - 1

Operations
 Operations are used to modify the array
 The operations are
 Traverse
 Print all the array elements
 Insertion
 Adds / inserts an element
 Deletion
 Deletes / removes the element
 Search
 Searches an element using the index or value
 Update
 Updates element
 Display
 Displays the content

Insertion
 Insertion is used to add element to the array
 The new element can be added at the beginning, end, using index of array

Program
#include <stdio.h>

int main(){

int LA[3], i;

printf("Array Before Insertion:\n");

for(i = 0; i < 3; i++)

printf("LA[%d] = %d \n", i, LA[i]);

printf("Inserting Elements.. ");

printf("The array elements after insertion :\n"); // prints array values

for(i = 0; i < 3; i++) {

LA[i] = i + 2;

printf("LA[%d] = %d \n", i, LA[i]);

return 0;
}

Deletion
 Delete / remove an element by using index of the array

Program
#include <stdio.h>

void main(){

int LA[] = {1,3,5};

int n = 3;

int i;

printf("The original array elements are :\n");

for(i = 0; i<n; i++)

printf("LA[%d] = %d \n", i, LA[i]);

for(i = 1; i<n; i++) {

LA[i] = LA[i+1];

n = n – 1;

printf("The array elements after deletion :\n");

for(i = 0; i<n-1; i++)

printf("LA[%d] = %d \n", i, LA[i]);

Search
 Searches an element in the array using the key
 Key element sequentially compares every value in the array to check if the key is present in
the array or not

Program
#include <stdio.h>

void main(){

int LA[] = {1,3,5,7,8};

int item = 5, n = 5;

int i = 0, j = 0;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);


}

for(i = 0; i<n; i++) {

if( LA[i] == item ) {

printf("Found element %d at position %d\n", item, i+1);

Traversal
 Traverses through all element of array

Program
#include <stdio.h>

int main(){

int LA[] = {1,3,5,7,8};

int item = 10, k = 3, n = 5;

int i = 0, j = n;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

Update
 Updates an existing element from the array using index

Program
#include <stdio.h>

void main(){

int LA[] = {1,3,5,7,8};

int k = 3, n = 5, item = 10;

int i, j;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

}
LA[k-1] = item;

printf("The array elements after updation :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

Display
 Display all the elements in the entire array using print statement

Program
#include <stdio.h>

int main(){

int LA[] = {1,3,5,7,8};

int n = 5;

int i;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

Unit 3
1. Singly linked list
 Contains nodes which have data part and address part
 The next points to the next node in order of sequence of modes
 It is the unidirectional linked list
 Space complexity: O(n)
 Each node is called as a node
 The node has reference to the next node in the list
 The head and the tail are the pointer used point the first and last node

Usages
 Dynamic memory allocation
 Efficient insertion and deletion at beginning
Pseudo
code
2. Double linked list
 Each node contains data and 2 links Time complexity
 The first links points to the previous node and next link points Access 0(n)
to the next node Search 0(n)
 Space complexity O (n ) Insertion 0(1)
 It is a bi directional liked list Deletion 0(1)
 It can traverse in both directions
 The nodes contain one extra pointer that points the previous node
 It is used for fast insertion and deletion
 We create a node with two pointers that points the previous node and next node

Usages
 Bi directional traversal
 Efficient deletion of node

Pseudo code
3. Palindrome using double linked list
4. Circular linked list
 In this the first and the last nodes are also connected to each other to form circle
 There is no NULL at the end
 It is a unidirectional linked list
 We can traverse it in one direction
 The last node will be pointing to the head node
 It has no beginning or ending
 We can add or remove the data from the list at any time

Pseudo code
Unit
4
1. Tree terminology
 Tree is non linear data structure
 It organizes data in hierarchical structure
 It is the collection of data
 Every individual element is known as node
 It stores the actual data of the particular element and line to next element in the hierarchical
structure

Root
 First node of the tree
 Every tree has root node
 It is the origin of the tree data structure

Edge
 The connecting link between 2 nodes is called as edge
 The tree with n number of nodes will be maximum of n-1 number of edges

Parent
 Predecessor of any node
 It has branch from it to other nodes
 It has child or children

Child
 Descendant of a node
 It has link from its parent nodes
 Any node can have any number of child nodes
 All nodes except root are child nodes

Siblings
 Nodes with same parent are called as siblings
 The nodes with same parent are called sibling

Leaf
 Nodes that do not have child
 Leaf nodes have no child

Internal nodes
 Node which has at least one child
 Nodes other than leaves are called as internal nodes
 The root node is also said to be internal node
 The internal nodes are also called as non terminal nodes

Degree
 The total number of children of node called as degree of the node
 The degree of node is total number of children it has
 The highest degree of node among all the nodes in tree

Level
 The root is said to be at level 0 and the
children of root are at level and children nodes
are at level 1
 Each step from top to bottom is called as level
 The level counts starts from 0 and incremented by 1

Depth
 It goes from the top to the bottom
 The total number of edges from root node to particular node is the depth
 The total number of edges from root node to leaf node is the longest path
 The highest depth of leaf node is said to be the depth of the tree

Height
 The total number of edges from leaf to particular node is the
particular node in the longest path
 The largest Height of the node in a tree is called as the height of the
tree
 It goes from the bottom to the top
 Height of all leaves nodes is 0

Path
 The sequence of nodes and edges from one node to another node
 Length of the path is total number of nodes in the path

Sub tree
 Each child from node forms a subtree
 Every child node will form subtree on its parent node

2. Traversal
Inorder - left root (parent) right
Preorder - root(parent) left right
Postorder - left right root(parent)

3. Huffman coding Time complexity


 The Huffman coding technique compress the string into a smaller Access 0(1)
size Search 0(n)
 It creates a tree using the frequencies of the character and Insertion 0(n)
generates the code for each character Deletion 0(n)
 Once the data is encoded then decoding is done using same tree
 It prevents any ambiguity in the decoding process using prefix code concept

Steps
 Calculate the frequency of each character in the string
 Sort the characters in increasing order of the frequency
 Then store them in priority queue
 Make unique character as leaf node
 Create empty node and assign the minimum frequency to the left child of z and assign the
second minimum frequency to the right of child Z
 Set the value of Z as sum of above 2 minimum frequency
 Remove 2 minimum frequencies from queue and add the sum into it
 Insert node z into the tree
 Repeat the step
 Assign non leaf node to 0 and leaf node as 1
Unit 5
1. Priority queue
 Declare max priority queue
 Set bool process ( priority_queue <int> &q
)
 Check whether q is empty
 Then extract the guest who have v
interlocutors
 The value v is the maximum in queue
 Set v as top and pop
 Queue<int>add
 While v—then check queue is empty if not
then pop them
 Add queue to q
 Check whether c == 2
 Clean the queue for next case
 Standard mechanism for ordering tasks
 It is based on First Come First served basis
 When tasks have importance then it is
priority
 The priority queue tasks using partial ordering based on priority
 The highest priority task is first served
 Heap uses priority queue for structuring data
 It is used to improve the performance

Operations
 There are 2 main operation like
 Insert
 Insert is also known as enqueuer
 The values are dynamically inserted
 The specification of priority is 0 –high,1,2..low)
 DeleteMin
 It is also known as dequeuer
 It finds the minimum element or highest priority in queue
 After finding it, it gets deleted from queue and returned

Implementations
 The implementation are
 Unordered linked list
 0(1) insert
 0(n) delete min
 Ordered linked list
 0(n) insert
 0(1) delete min
 Ordered array
 0(logn+n) insert
 0(n) delete min
 Balanced BST
 O(log2n) for insert and delete min

2. Binary heap
 Heap is a priority queue data structure
 It is a complete binary tree
 The binary heap is a binary tree with 2 properties
 Structure property
 Heap order property

Structure property
 It is a complete binary tree
 Each level is completely filled ( except bottom most level )
 The bottom most level may be partially filled ( from left to right )
 The height of complete binary tree is N elements log 2 N 

Property
 Max heap
 Key present at the root node must be greatest among all keys present at its children
 It should be followed by its sub tree too
 Min heap
 Key present at the root node must be greatest among all keys
present at its children
 It should be followed by its sub tree too

Application
 Heap sort algorithm
 Graph algorithm – prims, dijkstra
 Priority queue implementation

Heap order property


 The heap order property ( minheap )
 For every node x, key ( parent (x)) <= key (x)
 Except root node because it has no parent
 The minimum key always at root ( minheap )
 The maximum key always at root ( maxheap )
 The insert and delete min maintain the heap order property
 Duplicates are allowed in heap
 The elements that do not share ancestor – descendant relationship has no order

Operations
 getMin()
 getMax()
 extractMin()
 insert()
 delete()
 heapify()

getMIn()
 return root element of min heap
 complexity O(1)

getMax()
 return root element of max heap
 complexity O(1)

extractMin()
 removes minimum element from heap
 complexity O(Logn)
 also maintain heap property by calling heapify()

Insert
 The insertion of new element into heap at the
next available slot (hole)
 The element up the heap while heap order
property no satisfied
 Complexity O(Logn)
 The new key is added at the end of the tree
 If the key is greater than the element then do
nothing
 Otherwise call heapify to fix the violation of heap property

Delete

DeleteMin
 The minimum element is always at root
 The heap decreases by one size
 Then move last element into the hole at root
 Percolate down when heap order property not satisfied

Other heap operations


 Decreasekey(p,v)
 Lowers the current value of item p to new priority value v
 Need to percolate up
 IncreaseKey(p,v)
 increase the current value of item p to new priority value v
 need to percolate down
 Remove(p)
 decreaseKey( p,- ∞ )
 then delteMin

Worst case analysis


 height of heap  insert: log 2 N

 insert: 0(log N)
 buildHeap insert : 0(n) for n
inserts
 deleteMin: 0(log N)
 decreaseKey: 0(log N)
 increaseKey: 0(log N)
 remove: 0(log N)

Arrangement
Min heap
 A[(i-1)/2] - return parent
 A[(2*i)+1] - return left child
 A[(2*1)+2] - return left child

3. Applications of binary heap


 operating system scheduling
 processing jobs by priority
 graph algorithm
 for finding shortest path
 event simulation
 looking for next event to happen

Other types
 binomial heaps
 it is a forest of heap
 it satisfies structure property and heap order property
 it is different from binary heap
 the structure is totally different
 its heap property is same as binary heap
 the binomial tree of height is called as BK
 It has 2k nodes
 The number of nodes at depth d = ( dk )
 Each binomial tree contain minimum element at the root of every subtree
 The order of elements across binomial tree is irrelevant
 The binomial heap of n nodes is the forest of binomial trees as dictated by the
binary representation of n
 Each binomial tree is min heap or max heap
 d-heaps
 leftist heaps
 skew heaps
 Fibonacci heaps

4. Minheap
 In minheap the root or the parent must be smaller than its descendant

5. Maxheap
 IN maxheap the root or the parent must be greater than its descendant

5. Operations in heap
* getMin()
* getMax()
* extractMin()
* insert()
* delete()
* heapify()

Applications of heap
* heapsort sorting algorithm
* graph algorithms : prims, spanning, dijkstra shortest algorithm
* priority queue can implement with heap or variety of other method

6. Leftist
 Efficient implementation of meldable priority queues
 The total number of element in 2 rpiroity queues that are melded
 The priority queue then meld operation takes O(n) time
 When meldable is used
 Meld, insert and delete operations take 0 (log n) time
 Minimum(maximum) element found in 0(1) time
 It is the extended binary tree
 All empty nodes are replaced by square node
 The square nodes are known as external nodes
 The original nodes of the binary tree are called internal nodes

7. Height biased leftist tree ( HBLT )


 The leftist tree is binary tree if it is not empty
 It is the shortest ( LeftChild (X)) > shortest (RightChild(x)) for internal node x
 N > 2 shortest ( x ) - 1

8. Weight biased left tree ( WBLT )


 The number of nodes in the subtree is considered
 W(x) is the weight of node x
9. Binomial heap / queue
 it is a forest of heap
 it satisfies structure property and heap order property
 it is different from binary heap
 the structure is totally different
 its heap property is same as binary heap
 the binomial tree of height is called as BK
 It has 2k nodes
 The number of nodes at depth d = ( dk )
 Each binomial tree contain minimum element at the root of every subtree
 The order of elements across binomial tree is irrelevant
 The binomial heap of n nodes is the forest of binomial trees as dictated by the binary
representation of n
 Each binomial tree is min heap or max heap

Insertion
 Create new heap with single node containing the key
 Merge the new heap with existing heap using union operation

Deletion
 Find binomial tree with minimum key
 Remove the minimum node from its binomial tree and update the heap
 Merge the remaining tree if they have same degree

Search
 Iterate through each binomial tree in heap
 Search for the key within each binomial tree
 If key found return value else return null

Algorithm
 Create empty queue
 Insertion
 Create new binomial tree containing only the element to be inserted
 Merge new tree with exiting queue
 Compare degree of roots of tree
 If same degree then merge into single binomial tree with higher degree
 Id different degree add new tree to queue as separate element
 Minimum retrieval
 Find binomial tree with smallest root element
 Deletion
 Remove binomial tree containing minimum element from queue

Eg:

 4 14 24 34 44 54
Unit 6
1. AVL tree
 Self balancing tree Time complexity
 The difference between the height of the left and right subtree Access 0(log n)
is at most 1, 0 -1 Search 0(log n)
 Space complexity: O(n) Insertion 0(log n)
Deletion 0(log n)
Usages
 Self balancing property
 Efficient searching and insertion

Rotation
 There are 4 ways to keep the AVL tree balanced
 Left rotation
 Right rotation
 Left right rotation
 Right left rotation

Left rotation
 When node is added to the right sub tree
 If the tree gets unbalanced then single left rotation is used
Right rotation
 When node is added to the left sub tree
 If the tree gets unbalanced then single right
rotation is used

Left right rotation


 It is the combination of left and right rotation
 First left rotation is takes then right rotation is
executed

Right left rotation


 It is the combination of right and left rotation
 First right rotation is takes then left rotation is executed

Operations
 Insertion
 Deletion
 search

Insertion

Balance factor
Deletion Search

2. Binary search tree


Time complexity
 Also known as ordered or sorted binary tree
Access 0(n)
 The nodes with lesser node value compared with root are
Search 0(n)
stored at the left subtree
Insertion 0(n)
 The nodes with higher value comparted with root are stored at
Deletion 0(n)
right subtree
 Space complexity

Usage
 Efficient searching
 In order traversal

Insertion
Deletion Print

Largest element smallest element

Findlargest(node)

Sum of BST

3. Red black tree


 It is a binary search tree which every node is coloured with either red or black
 It is self balancing binary search tree

Properties
 Root is black
 Every leaf is black in red black tree
 The children of red node are black
 All the leaves have same black depth
 Root to descendant leaf node contains same number of black nodes
 There will be no 2 adjacent red nodes

Rules
 If the tree is empty the new node is root and colour black
 When the tree is not empty then new node as leaf with color red
 If parent is black then exit
 If parent is red then check colour of parents sibling of new node if red then recolour as black
 If color is black then do rotations and recoloring
 If color is not red then recolour to red and check parent and do the same

AVL Red black BST


Searching faster Searching is slower than avl Slower than avl and red
black
Not strictly balanced Strictly balanced Not strictly balanced
No use for color Uses red and black color No use for color
Insertion, removal operation are Faster insertion and removal Insertion and removal is
complex simple
Need more rotation for Needs less rotations Rotations are not needed
balancing
Stores balance for each node Needs 1 bit for information per Does not require any
thus increases storage node additional storage
Provide efficient searching Searching is inefficient Less efficient
Used in map, multi map etc Used in database for faster Used in generic data
retrievals storage
Does not have balance factor Balance factor value around 1, 0, Does not require balance
-1 factor
Takes less processing for Requires more processing time Faster processing than
balancing for balancing other 2
Maximum 2 rotation are Some times rotates more than 2 Rotations are not needed
required times
Unit 7
1. B tree
 It is special type of self balancing tree
 Each node contain more than one key and have more than 2 children
 It is the generalized form of binary search tree
 B tree can store many keys in single node and have multiple child nodes
 It decreases the height significantly allowing faster disk access

Properties
 All leaves have same depth
 All leaves have same level
 Defined by minimum degree t
 T depends on disk block size
 All nodes can contain 2*t-1 keys
 Number of children node = number of keys +1
 All keys are sorted in increasing order
 The child between k1 and k2 range from k1 to k2
 Can grow and shrink form the root
 Time complexity O(log n)
 Insertion of node done at leaf node
 Each node x the keys are stored in increasing order
 There is Boolean value x.leaf which is true if x is leaft
 If n is order of the tree, each internal node can contain at most n-1 keys along with pointer
to each child
 Each node except root have at most n children at least n/2 children
 Root has atleast 2 children and contain minimum of q key

Operations
 Searching
 Start from root node compare k with first key of the node
 K.leaf = true return NULL
 Search the left child of the key
 If it is more than one key the current then search the left child
 Repeat steps 1 to 4 until leaf is reached

Applications
 Database and file systems
 Store blocks of data
 Multilevel indexing

2. B+tree
 It is the variation of B tree
 The data pointers are stored only at the leaf node
 The leaf differs from the structure of internal nodes
 The leaf nodes are linked together to provide ordered access to the search field tot the
records
 The internal nodes are used to guide the search

Features
 Balanced
 Multi level
 Ordered
 Cache friendly
 Disk oriented

B tree B+ tree
Nodes store both keys and values Leaf nodes store value and internal nodes store
indexing
Leaf do not form linked list Leaf forms linked list
Lower order ( fewer keys ) Higher order ( more keys )
Does not allow key duplication Allow key duplication
More disk i/o due to non sequential reads in Better disk access due to sequential reads
internal nodes
Balanced performance for search insert Better performance for range queries
Requires less memory Requires more memory`

3. Trie
 Trie is tree like data structure used for efficient retrieval and storage strings
 It organizes strings by their character n tree like structure
 It is ideal for tasks like autocomplete, spell checking and searching for words with
 It is used to store set of strings in it
 The number of pointer is equal to the number of characters in Time complexity
each node Search 0(n)
 Also known as digital tree or prefix tree Insertion 0(n)
 common prefix Deletion 0(n)
 Space complexity: O (n*m)

Usages
 Efficient prefix searching
 Autocomplete suggestions

Properties
 The root is always null
 Each node have maximum of 26 children
 Each node except root can store one letter of the alphabet

Types
 Standard trie
 Compressed trie
 Suffix trie

Standard trie
 Root node must be null
 Node can have 26 children
 Children are arranged in alphabetical order
 Path from root to leaf node / external node gives string set
 When 2 string have common prefix share the same ancestors

Compressed trie
 Space optimization
 Nodes of degree at least 2
 Obtained by compressing chain of redundant nodes

Suffix trie
 It is a compressed trie for all suffixes of text
 Eg: minimize
 Suffixes: e, ze, ize, mize, imize, nimize,
inimize, minimize
Unit 9
1. Hash table
 The hash table is also known as hash map
 It provides efficient key value pair storage and retrieval
 It uses has function to compute index known as hash code
 It maps the key to specific location in array
 Space complexity 0 ( n+m ) n – size, m – elements inserted

Usages
 Fast data retrieval
 Key value storage
2. Hashing ( Hash function )
 New index is processed using keys
 The element corresponding to key is stored in index
 When hash function produces the same index for multiple elements these elements are
stored in same index by using doubly linked list
 If j is slot for multiple elements and contains pointer to head of list elements
 When no elements is present j contains NIL

Open addressing
 Open addressing store multiple elements into same slot
 Each slot is either filled with single key or NIL

Linear probing
 Linear probing collision is resolved by checking next slot
 H(k,i)=(h’(k)+i) mode m
 i={0,1…}
 h’(k) is new hash function
 collision occur at h(k,0) then h(k,1) is checked
 the value of I is incremented linearly
 linear probing is the cluster of adjacent slots is filled
 when inserting a new element entire cluster must be traversed
 it adds to the time required perform operations on hash table

Quadratic probing
 works similar to linear probing but spacing between slots is increased
 h(k, i) = (h′(k) + c1i + c2i2) mod m
 c1 and c2 are are positive auxillay constants
 I = 0 ,1 ……
 H(k,i)=(h’k+i*I) mod m

Double hashing
 Collision occurs after applying hash function h(k)
 Another hash function is calculated for finding next slot
 h(k, i) = (h1(k) + ih2(k)) mod m
Closed addressing
 Closed hashing includes
 Chaining

Chaining
 The chaining builds a linked list of items whose key hashes have same value
 It adds a linked list to each table position
 The elements with same hash value is stored in a list

Hash functions
 hash function cannot prevent the collisions completely
 it can reduce the number of collision
 functions
 division method
 multiplication method
 universal hashing

Division method
 if k is key and m is the size of the hash table
 it is calculated as h (k ) = h mod m
Multiplication method
 h ( k ) = m(ka mod 1)
 Ka mod 1 gives fractional part kA
 ⌊ ⌋ gives floor value
 A is constant
 The value of A lies between – and 1

Universal hashing
 It is chosen at random independent of keys

Applications
 Constant time lookup and insertion is required
 Cryptographic applications
 Indexing data is required

Algorithm
 Select a hash function
 Declare array size
 Initialize the hash function
 Get the key
 Based on the key put the value at the array
 if the index contains value the use linear probing or quadratic probing to insert the element

Separate chain hashing


 Insertion
 Compute hash index I
 Check whether the linked list at i is
empty
 If empty then create new node
containing the key value pair and add it
to the list
 If not empty then traverse the list and
create new node containing the key
value pair and add it to the list
 Search(key)
 Compute hash index i
 Traverse the linked list at i
 Search the key
 If found the return the value
 Otherwise return key not present
 Delete(key)
 Compute hash index I
 Traverse the linked list at i
 Find the key and remove it
 Otherwise return not found
Impact
Insert first()
 Efficient for small chains
 Lead to performance degradation for large chains due to longer search

Insert last()
 Less prone to performance degradation
 Insertion time increases as the lengths of the chain increases

You might also like