0% found this document useful (0 votes)
4 views

DSA Assignment 1, 2

The document covers various concepts related to hashing functions, collision resolution strategies, extendible hashing, skip lists, trees, and binary search trees (BST). It explains different types of hashing functions like MD5, SHA, CRC, and checksum, as well as collision resolution methods such as open addressing and chaining. Additionally, it includes algorithms for tree traversals (inorder, preorder, postorder), depth-first search, and the properties of BST as an abstract data type.

Uploaded by

Satyam Gawali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DSA Assignment 1, 2

The document covers various concepts related to hashing functions, collision resolution strategies, extendible hashing, skip lists, trees, and binary search trees (BST). It explains different types of hashing functions like MD5, SHA, CRC, and checksum, as well as collision resolution methods such as open addressing and chaining. Additionally, it includes algorithms for tree traversals (inorder, preorder, postorder), depth-first search, and the properties of BST as an abstract data type.

Uploaded by

Satyam Gawali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 1

1. What is a hashing function? Explain any 4 types of hashing functions.


A hashing function is essentially a recipe that takes an input of variable size
(text, numbers, files) and squishes it into a fixed-size output, called a hash value
or hash code. This function works like a digital fingerprint - unique for each input,
but much shorter.
Here are four common types of hashing functions:
 MD5 (Message-Digest Algorithm 5): This was a widely used hashing
function, but it's no longer considered secure for things like password
storage due to its vulnerability to collisions (different inputs resulting in the
same hash).
 SHA (Secure Hash Algorithm): This is a family of hashing functions (SHA-1,
SHA-2, SHA-3) designed for security. SHA-1 is similar to MD5 but more
secure, though it's also being phased out. SHA-2 (with variations like SHA-
256 and SHA-512) is the current go-to for cryptographic hashing.
 CRC (Cyclic Redundancy Check): This type of hashing is less about security
and more about error detection. It's often used for things like file transfers
- the sender and receiver both calculate a CRC hash of the data, and if they
don't match, it means there were errors during transmission.
 Checksum: Similar to CRC, a checksum is a simple hashing function used
for error detection. It can be just adding up the ASCII values of characters
in a file. While not very secure, it's a quick way to check for data corruption.

2. Explain collision resolution strategies.


Collision resolution strategies are used to handle collisions in hash tables,
which occur when two or more keys have the same hash value. The two main
types of collision resolution strategies are open addressing and chaining:
Open addressing - Also known as closed hashing, this strategy uses a probing
function to find an empty slot in the hash table to store the input. The probing
function determines the next slot to try, such as linear probing, quadratic
probing, or double hashing.
Chaining - Also known as open hashing, this strategy stores the input in a linked
list or another data structure that is attached to the original slot. Each slot can
then hold multiple inputs that share the same hash.

3. Assume the size of hash table as 8. The hash function to be used to calculate
the hash value of the data X is X% 8. Insert the following values in hash table:
10, 12, 20, 18, 15. Use linear probing without replacement For handling
collision.
Hash Function Calculation
For each value, we'll calculate the hash value using the provided formula: X % 8.
 10 % 8 = 2
 12 % 8 = 4
 20 % 8 = 4
 18 % 8 = 2
 15 % 8 = 7
Hash Table Insertion
We'll process the elements one by one:
1. 10: Hash value is 2, which is empty. Insert 10 at index 2.
2. 12: Hash value is 4, which is empty. Insert 12 at index 4.
3. 20: Hash value is 4, which is already occupied by 12. We perform linear
probing:
 Check index 5 (4 + 1). It's empty. Insert 20 at index 5.
4. 18: Hash value is 2, which is already occupied by 10. We perform linear
probing:
 Check index 3 (2 + 1). It's empty. Insert 18 at index 3.
5. 15: Hash value is 7, which is empty. Insert 15 at index 7.
Final Hash Table
Here's the resulting hash table after inserting all the elements:
Index Value
0 NULL
1 NULL
2 10
3 18
4 12
5 20
6 NULL
7 15

As you can see, collisions occurred for values 12 and 20 (both hashed to 4) and
10 and 18 (both hashed to 2). Linear probing successfully placed them in the next
available slots using the modulo operation (+ 1) for the probing sequence.

4. What is algorithm of extendible hashing.

Extendible hashing is a dynamic hashing method that uses directories and


buckets to store and hash data. The algorithm is flexible and the hash function
changes based on the data type and situation. The algorithm uses pointers to
store the addresses of the buckets in directories, and each directory has a unique
ID that may change during Directory Expansion. Buckets contain the hashed data.

5. Explain skip list. Give applications of skip list.


A skip list is a probabilistic data structure designed for efficient searching,
insertion, and deletion operations on a sorted list of elements. It offers an
average time complexity of O(log n) for these operations, similar to balanced
search trees but with some advantages.

Applications of Skip Lists:

 In-memory Databases: Skip lists are well-suited for in-memory


databases where fast lookups and insertions are crucial.
 Geospatial Indexing: They can be used for spatial data structures
where efficient retrieval of nearby elements based on location is
important.
 Network Routing: Skip lists can be helpful for implementing routing
algorithms in networks, allowing for efficient path finding.
 Cache Implementation: They can be used in cache invalidation
algorithms where fast lookups and efficient removal of stale entries
are desired.
Assignment 2

1. What is a tree. Give figure of tree.


A tree is a fundamental data structure in computer science that represents a
hierarchical relationship between elements. It simulates the structure of a tree in
nature, where there's a single root at the top, connected to nodes that branch
out further down.

2. Prove that maximum possible nodes in a binary tree of height 2h - 1.

Base Case: When h=1, the Maximum number of notes each one which is the
binary tree of height 1.
Inductive Hypothesis: Assume the maximum number of nodes in a binary tree
of height 2k-1 is 2k.
Indicative Step: We need to show that the maximum number of notes in a
binary tree of height 2(k+1)-1 is 2k+1.
Consider a binary tree of height 2(k+1)-1. The root node has two children,
each of which is a binary tree of height 2k-1. By the Inductive Hypothesis, Each
of these children has a most 2k nodes. Therefore, the root node and its two
children have most 2k+2k+1=2k+1+1 nodes.
However, it is possible that the root node has only one child, or even no
children. In these cases, the maximum number of nodes in the binary tree is
still 2k+1.
Therefore, the maximum number of nodes in a binary tree of height 2(k+1)-1
is 2k+1.
By the principle of mathematical induction, the maximum number of nodes in
a binary tree of height 2h-1 is 2h.
3. What is algorithms and C functions of
i) Recursive inorder traversal
ii) Recursive preorder traversal
iii) Recursive Recursive postorder traversal.

Inorder Traversal

The inorder traversal visits the left subtree, then the root node, and finally
the right subtree. It's commonly used for printing a tree in sorted order if the tree
represents elements with inherent order (like a binary search tree).

void inorder_traversal(struct Node* root) {


if (root != NULL) {
inorder_traversal(root->left);
printf("%d ", root->data);
inorder_traversal(root->right);
}
}
Preorder Traversal
The preorder traversal visits the root node first, then the left subtree, and
finally the right subtree. It's often used for copying the structure of a tree.
void preorder_traversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder_traversal(root->left);
preorder_traversal(root->right);
}
}
Postorder Traversal
The postorder traversal visits the left subtree, then the right subtree, and
finally the root node. It's sometimes used for tree deletion or performing
operations after visiting all child nodes.
void postorder_traversal(struct Node* root) {
if (root != NULL) {
postorder_traversal(root->left);
postorder_traversal(root->right);
printf("%d ", root->data); // Visit the root node last
}
}

4. What is Depth first search.

Depth-first search (DFS) is an algorithm for traversing or searching tree or


graph data structures. The algorithm starts at the root node (selecting some
arbitrary node as the root node in the case of a graph) and explores as far as
possible along each branch before backtracking.

DFS is a recursive algorithm, which means that it calls itself to explore each
branch of the tree or graph. The algorithm maintains a stack to keep track of
the nodes that it has visited and the nodes that it still needs to visit.

To perform a DFS on a tree, the algorithm starts at the root node and
pushes it onto the stack. Then, it pops the node off the stack and explores its
children. If a child node has not been visited yet, the algorithm pushes it onto
the stack. The algorithm repeats this process until the stack is empty.
5. Explain BST as ADT.

An Abstract Data Type (ADT) is a data type that is defined by its operations
and not by its implementation. A binary search tree (BST) is a binary tree data
structure in which the nodes are arranged in a specific order. The left child of
a node contains a value that is less than the node's value, and the right child
of a node contains a value that is greater than the node's value. This specific
order allows for efficient searching and sorting operations.

You might also like