DSA Assignment 1, 2
DSA Assignment 1, 2
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.
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).
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.