Module-5 DS 2024
Module-5 DS 2024
Module 5
Hashing, Priority Queues, BST
Hashing
• The time required to search for an key in other
searching techniques depends on the number of
elements in the collection
• Hashing or hash addressing is independent of the
number n
Terminology
• Hashing will be oriented towards file management
• We assume that there is a file F on n records with s
set K of keys which uniquely determine the records
in F
• F is maintained in memory by a table T of m
memory locations and L is the set of memory
addresses of the locations in T
• The general idea is to use the key to determine the
address of a record, but care must be taken so that
a great deal of space is not wasted
• This takes a form of a function H from the set K of
keys into the set L of memory addresses.
• Such a function H: K L is called a hash function or
hashing function
• Hash function may not yield distinct values : it is
possible that two different keys k1 and k2 will yield
the same hash address. This situation is called
collision and some method must be used to resolve
it
• Hash function
• Collision resolutions
Hash functions
• Two principle criteria used in selecting a hash
function H: K L are:
• The function H should be very easy and quick to
compute
• The function H should as far as possible, uniformly
distribute the hash addresses throughout the set L
so that there are minimum number of collisions
Static hashing
• Division
• Mid-Square
• Folding
• Digit Analysis
Converting keys into integers
Unsigned int stringToInt (char *key)
{
Int number=0;
While(*key)
Number += *key++;
Return number;
}
• Simple additive approach
Unsigned int StingToInt ( char *key)
{
Int number = 0;
While ( *key)
{
Number += *key++;
If(*key) number +=((int) *key++) <<8;
}
Retunr number;
}
Overflow handling
• Open addressing
• Chaining
• Four open addressing methods
• Linear probing or linear open addressing
• Quadratic probing
• Rehashing
• Random probing
Binary Search Tree
• Heap
• a min (max) element is deleted. O(log2n)
• deletion of an arbitrary element O(n)
• search for an arbitrary element O(n)
• Binary search tree
• Every element has a unique key.
• The keys in a nonempty left subtree (right subtree) are
smaller (larger) than the key in the root of subtree.
• The left and right subtrees are also binary search trees.
Examples of Binary Search Trees
20 30 60
12 25 5 40 70
10 15 22 2 65 80
Searching a Binary Search Tree
tree_pointer search(tree_pointer root,
int key)
{
/* return a pointer to the node that
contains key. If there is no such node,
return NULL */
}
return NULL; O(h)
}
Insert Node in Binary Search Tree
30 30 30
5 40 5 40 5 40
2 2 80 2 35 80
Insert 80 Insert 35
Insertion into A Binary Search Tree
void insert_node(tree_pointer *node, int num)
{tree_pointer ptr,
temp = modified_search(*node, num); if (temp ||
!(*node)) {
ptr = (tree_pointer) malloc(sizeof(node)); if (IS_FULL(ptr)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
ptr->data = num;
ptr->left_child = ptr->right_child = NULL;
if (*node)
if (num<temp->data) temp->left_child=ptr; else temp-
>right_child = ptr;
else *node = ptr;
}
}
Deletion for A Binary Search Tree
1
leaf 30
node
5 80
T1 T2
1
2
2
T1 X
T2
Deletion for A Binary Search Tree
non-leaf
40 40
node
20 60 20 55
10 30 50 70 10 30 50 70
45 55 45 52
52
Before deleting 60 After deleting 60
1
2
T1
T2 T3
1
2‘
T1
T2’ T3
Priority Queue
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);
deleteRoot(array, 4);
printArray(array, size);
}
Double Ended Priority Queue
Introduction to the double-ended priority queue
•A DEPQ permits actions like inserting and deleting items with priorities
at both ends of the queue.
•In spite of where they are in the queue, the elements with higher
priorities are always processed first.
•The DEPQ is a valuable data structure for scheduling, task
management, and resource allocation applications because of this.
•Several data structures, including binary heaps, Fibonacci heaps, and
balanced binary search trees, can be used to build DEPQs.
•The needs of the particular application and the trade-offs between
time and space complexity determine the best data structure to use.
Operations are performed on a double-ended priority queue
(DEPQ):
// Printing Options
cout << "\nDEPQ Operations:" << endl;
cout << "1. Insert an Element" << endl;
cout << "2. Get Minimum Element" << endl;
cout << "3. Get Maximum Element" << endl;
cout << "4. Delete Minimum Element" <<
endl;
cout << "5. Delete Maximum Element" <<
endl;
cout << "6. Size" << endl;
cout << "7. Is Empty?" << endl;
cout << "0. Exit" << endl;
}