0% found this document useful (0 votes)
7 views16 pages

ds-5 Removed

The document covers various hashing techniques, including static and dynamic hashing, and their properties. It explains different hash functions such as division, midsquare, and folding methods, along with chained hashing and its pros and cons. Additionally, it discusses leftist trees, min-leftist trees, double-ended priority queues, min-max heaps, and optimal binary search trees, providing examples and C function declarations where applicable.

Uploaded by

gautampl444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

ds-5 Removed

The document covers various hashing techniques, including static and dynamic hashing, and their properties. It explains different hash functions such as division, midsquare, and folding methods, along with chained hashing and its pros and cons. Additionally, it discusses leftist trees, min-leftist trees, double-ended priority queues, min-max heaps, and optimal binary search trees, providing examples and C function declarations where applicable.

Uploaded by

gautampl444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MODULE V

1. What is Hashing? Discuss the properties of a good hash function. Explain the following
hash function with proper example(10)
a. Division
b. Midsquare
c. Folding
2. Explain static and dynamic hashing in detail(8)
3. What is chained hashing? Discuss its pros and cons. Construct the hash table to insert the
keys: 7, 24, 18, 52, 36, 54, 11, 23 in a chained hash table of 9 memory locations. Use h(k)
= k mod m(8)
4. What is dynamic hashing? Explain the following techniques with examples: (8)
i) Dynamic hashing using directories ii) Directory less dynamic hashing
5. Consider a hash table of size 10. Using linear probing, insert the keys 72, 27, 36, 24, 63,
81, 92, and 101 into the table (10)
6. Consider a hash table of size 10. Using quadratic probing, insert the keys 72, 27, 36, 24,
63, 81, and 101 into the table. Take c1 = 1 and c2 = 3 (10)
7. Define the leftist tree. Give its declaration in C. Check whether the given binary tree is a
leftist tree or not. Explain your answer(5)

8. Define min Leftist tree. Meld the given min leftist trees(5)

9. Define double ended priority queue and min-max heap. Explain Insertion and Deletion in
a min-max heap with example(8)
10. What is Optimal Binary Search Tree? Write a c function to find an Optimal Binary
Search Tree.
1. What is Hashing? Discuss the properties of a good hash function. Explain the
following hash function with proper example(8)
a. Division Method
b. Midsquare Method
c. Folding Method
Hashing
• Hashing is a technique to convert a range of key values into a range of indexes of an array.
In general we use modulo operator to get a range of key values. Hashing technique use a
special function called the Hash function which is used to map a given value with a
particular key for faster access of elements.
• Each Item can be located in a Bin/Bucket/Shelf according to its key value (number)
• Most simple Hash Method/Function
int hash(int key)
{
return key % SIZE;
}
Hash Function
• A hash function is any function that can be used to map a data set of an arbitrary size to a
data set of a fixed size, which falls into the hash table. The values returned by a hash
function are called hash values, hash codes, hash sums, or simply hashes. There are many
hash functions that use numeric or alphanumeric keys.
Properties of good Hash Function
• Hash function should be simple to compute
• Number of collision should be less
• The hash function uses all the input data
• The hash function "uniformly" distributes the data across the entire set of possible hash
values
• The hash function generates very different hash values for similar strings
Types of Hash functions
o Division Method
o Mid Square Method
o Folding Method
i) Modular method /Division Method :
• It is the most simple method of hashing an integer x. This method divides x by size of
hash table M and then uses the remainder obtained. In this case, the hash function can
be given as
h(x) = x mod M
• Example: To calculate the hash values of keys 1234 and 5462. Solution Setting M =
97, hash values can be calculated as:
h(1234) = 1234 % 97 = 70
h(5642) = 5642 % 97 = 16
• Mid-square Method
o The mid-square method is a good hash function which works in two steps:
o Step 1: Square the value of the key. That is, find k2.
o Step 2: Extract the middle r digits of the result obtained in Step 1.
o In the mid-square method, the same r digits must be chosen from all the keys.
o Therefore, the hash function can be given as:
▪ h(k) = s where s is obtained by selecting r digits from k2.
o Example: To calculate the hash value for keys 1234 and 5642 using the mid-square
method:
▪ The hash table has 100 memory locations.
▪ Solution: Note that the hash table has 100 memory locations whose indices
vary from 0 to 99.
▪ This means that only two digits are needed to map the key to a location in
the hash table, so r = 2.
• When k = 1234, k2 = 1522756, h (1234) = 27
• When k = 5642, k2 = 31832164, h (5642) = 21
• Folding Method:
o The folding method works in the following two steps:
o Step 1: Divide the key value into a number of parts. That is, divide k into parts k1,
k2, ..., kn, where each part has the same number of digits except the last part which
may have lesser digits than the other parts.
o Step 2: Add the individual parts. That is, obtain the sum of k1 + k2 + ... + kn. The
hash value is produced by ignoring the last carry, if any. Note that the number of
digits in each part of the key will vary depending upon the size of the hash table. .
o Example: Given a hash table of 100 locations, calculate the hash value using
folding method for keys 5678 and 34567
o Solution: Since there are 100 memory locations to address, we will break the key
into parts where each part (except the last) will contain two digits. The hash values
can be obtained as shown below: Key: 34567, Parts: 34,56 and 7, Sum:97 so, Hash
value: 97 Key: 5678, Parts: 56 and 78, Sum:134 Hash value: 34 (ignore carry)

2. Explain static and dynamic hashing in detail(8)

Static Hashing
• In static hashing, the identifiers are stored in a fixed size table called a hash table
• When a search key is given hash function always computes the same address
• Hashing technique in which the table(bucket) size remains the same (Fixed during
compilation time) is called static hashing
• A static hashing scheme is one where the size of the hash table is fixed
• Various techniques of static hashing are linear probing and chaining
• As the size is fixed, this type of hashing consist handling overflow of elements
(Collision)efficiently
• Example of hash functions: h(k) = k MOD m (k is key & m is size of hash table)
• Disadvantages
o Table size is fixed and hence cannot accommodate data growth
o Collisions increases as data size grows
o May require rehashing of all keys when chains or overflow buckets are full

Dynamic Hashing

• If we allocate a large portion of memory to hold the table, we waste space. Yet, if we allocate a
minimal amount of memory, we will have to restructure the entire file when the data exceeds the
capacity of the hash table. This is a very time-consuming process.
• Dynamic hashing, also referred to as extendible hashing, retains the fast retrieval time of
conventional hashing, while extending the technique so that it can accommodate dynamically
increasing and decreasing file size without penalty.
• In this hashing scheme the set of keys can be varied & the address space is allocated
dynamically.
• If a file F is collection of record a record R is key & the data stored in pages (buckets) then
space utilization:
Number of records/(Number of pages*Page capacity)
• Dynamic hashing schemes are able to resize the hash table on demand without needing to
rebuild the entire table. The schemes perform this resizing in different ways that can either
maximize reads or writes
• Dynamically increases the size of the hash table as collision occurs. There are two types:
1) Dynamic hashing using directory or (Extendible hashing) : uses a directory that grows
or shrinks depending on the data distribution. No overflow buckets
2) Directory less Dynamic hashing or(Linear hashing): No directory. Splits buckets in
linear order, uses overflow buckets.
3. What is chained hashing? Discuss its pros and cons. Construct the hash table to insert
the keys: 7, 24, 18, 52, 36, 54, 11, 23 in a chained hash table of 9 memory locations. Use
h(k) = k mod m.
Chained Hashing
• Chained hashing, also known as separate chaining, is a technique used to resolve
collisions in hash tables
• When two or more keys hash to the same index (known as a collision), chained hashing
handles these collisions by maintaining a linked list of all elements that hash to the
same index
Pros of Chained Hashing:
1.Simple Implementation: Chained hashing is relatively easy to implement.
2.Efficient Insertion and Deletion: Insertion and deletion operations are efficient in chained
hashing.
3.Dynamic Data Sets: Chained hashing allows for dynamic resizing of the hash table..
4.Adaptability: Chained hashing can handle a wide range of input data distributions

Cons of Chained Hashing:


1.Memory Overhead: Chained hashing can have a higher memory overhead compared to
other collision resolution techniques.
2.Cache Inefficiency: cache inefficiency, particularly for large hash tables with long linked
lists.
3.Performance Degradation:
4.Poor Worst-Case Performance: Chained hashing does not guarantee constant-time
performance for lookup operations in the worst case
Construct the hash table to insert the keys: 7, 24, 18, 52, 36, 54, 11, 23 in a chained hash
table of 9 memory locations. Use h(k) = k mod m.
Initial hash table
4. What is dynamic hashing? Explain the following techniques with examples:
a) Dynamic hashing using directories
b) Directory less dynamic hashing

Dynamic Hashing
• If we allocate a large portion of memory to hold the table, we waste space. Yet, if we allocate a
minimal amount of memory, we will have to restructure the entire file when the data exceeds the
capacity of the hash table. This is a very time-consuming process.
• Dynamic hashing, also referred to as extendible hashing, retains the fast retrieval time of
conventional hashing, while extending the technique so that it can accommodate dynamically
increasing and decreasing file size without penalty.
• If a file F is collection of record a record R is key & the data stored in pages (buckets) then
space utilization:
Number of records/(Number of pages*Page capacity)
a) Dynamic hashing using directories
• Uses a directory of pointers to buckets/bins which are collections of records
• The number of buckets are doubled by doubling the directory, and splitting just the bin
that overflowed.
• Directory much smaller than file, so doubling it is much cheaper
b) Directory less dynamic hashing
In contrast to the directory scheme, in which a single page might be pointed at by several directory
entries, in the directory less scheme there must exist a unique page for every possible address.
5. Consider a hash table of size 10. Using linear probing, insert the keys 72, 27, 36, 24,
63, 81, 92, and 101 into the table
Linear Probing:
6. Consider a hash table of size 10. Using quadratic probing, insert the keys 72, 27, 36,
24, 63, 81, and 101 into the table. Take c1 = 1 and c2 = 3
Quadratic Probing Steps
7. Define the leftist tree. Give its declaration in C. Check whether the given binary tree
is a leftist tree or not. Explain your answer(5)
8. Define min Leftist tree. Meld the given min leftist trees(5)

Min Leftist Tree


A min leftist tree is a binary tree that implements a mergeable heap and has the following
properties:
• The root contains the minimum key
• The null path length (npl) of the left child is at least as large as that of the right child for
every node
• The right path is as short as any path in the heap
Meld the given min leftist trees
9. Define double ended priority queue and min-max heap. Explain Insertion and
Deletion in a min-max heap with example(8)

Double ended priority queue


A double-ended priority queue is a data structure that supports the following operations:
• Insert an element with arbitrary key.
• Delete an element with the largest key.
• Delete an element with the smallest key.

Min-Max Heap
A min-max heap is a complete binary tree such that if it is not empty, each element has a
field called key. Alternating levels of this tree are min levels and max levels, respectively. The
root is on a min level. Let x be any node in a min-max heap. If x is on a min level then the
element in x has the minimum key from among all elements in the subtree with root x. We call
this node a min node. Similarly, if x is on a max level then the element in x has the maximum
key from among all elements in the subtree with root X. We call this node a max node
10. What is Optimal Binary Search Tree? Write a c function to find an Optimal Binary
Search Tree.

Optimal Binary Search Tree


An optimal binary search tree (Optimal BST), sometimes called a weight-balanced binary
tree, is a binary search treewhich provides the smallest possible search time (or expected
searchtime) for a given sequence of accesses (or access probabilities)

Two Possible Binary Search Trees


T

• Consider the two search trees, the second tree requires at most three comparisons to
decide whether the identifier we seek is in the tree.
• The first binary tree may require four comparisons, since any identifier that
alphabetically comes after for but precedes void tests four nodes.
• Thus, the second binary tree has a better worst case search time than the first tree.
• Searching for an identifier in the first tree requires one comparison for for, two
comparisons each for do and while, three comparisons for void, and four comparisons
for if.
• If we search for each with equal probability, the average number of comparisons for a
successful search is 2.4. The average number of comparisons for the second tree is only
2.2. Thus, the second tree also has better average behavior.
Function to find an Optimal Binary Search Tree

You might also like