You are on page 1of 3

QUESTION #01:

i. AVL Tree:
An AVL tree is a self-balancing binary search tree. It maintains a balance
factor for each node, which is the difference between the heights of its left
and right subtrees. The balance factor is used to ensure that the tree remains
balanced, meaning the heights of the left and right subtrees differ by at most
1.
• Insertion in AVL Trees: When a new node is inserted into an AVL tree, the tree is
checked for balance violations and, if necessary, rebalanced. The steps for inserting a
node into an AVL tree are as follows:
1. Perform a standard binary search tree insertion.
2. Update the balance factors of the nodes on the insertion path.
3. If a balance violation occurs (balance factor becomes -2 or +2), perform the
necessary rotations to restore balance.
4. Continue updating balance factors and performing rotations up the tree until the
root is reached.
• Deletion in AVL Trees: When a node is deleted from an AVL tree, the tree is
checked for balance violations and rebalanced if necessary. The steps for deleting a
node from an AVL tree are as follows:
1. Perform a standard binary search tree deletion.
2. Update the balance factors of the nodes on the deletion path.
3. If a balance violation occurs, perform the necessary rotations to restore balance.
4. Continue updating balance factors and performing rotations up the tree until the
root is reached.
ii. B-tree:
A B-tree is a self-balancing search tree that is optimized for disk access and efficient
retrieval of large amounts of data. It is commonly used in file systems and databases. B-
trees have a variable number of child nodes per parent, allowing for efficient use of disk
blocks.
• Insertion in B-trees: When a new key-value pair is inserted into a B-tree, the tree
is modified to maintain its properties. The steps for insertion in a B-tree are as follows:
1. Perform a search to find the appropriate leaf node for insertion.
2. If the leaf node has room for the new key-value pair, insert it at the appropriate
position.
3. If the leaf node is full, split it into two nodes and promote the middle key to the
parent node.
4. If the parent node is also full, split it recursively.
5. Continue splitting and promoting until a non-full node is reached, or a new root
is created.
• Deletion in B-trees: When a key-value pair is deleted from a B-tree, the tree is
modified to maintain its properties. The steps for deletion in a B-tree are as follows:
1. Perform a search to find the node containing the key to be deleted.
2. If the key is found in an internal node, replace it with the predecessor or
successor key and recursively delete the predecessor/successor.
3. If the key is found in a leaf node, delete it from the leaf.
4. If the deletion causes a node to have fewer keys than the minimum required,
perform a series of rotations and mergers to restore balance.
5. Continue the rotation and merging process up the tree until the root is reached.

QUESTION #02:
Hashing is a technique used in computer science to map data of arbitrary size to fixed-size
values. It involves applying a hash function to the input data, which generates a unique hash
code or hash value. Hashing is widely used in various applications, such as data storage,
retrieval, and cryptography.

Hash Tables:
A hash table is a data structure that uses hashing to efficiently store and retrieve key-value
pairs. It consists of an array (often called a hash table or bucket array) and a hash function. The
hash function is responsible for converting the key into an index in the array, where the
corresponding value is stored. The primary advantage of hash tables is their ability to provide
constant-time average-case complexity for operations like insertion, deletion, and search.

Hash Function:
A hash function takes an input (such as a key) and computes a fixed-size hash value. It should
ideally distribute the hash values uniformly across the array, minimizing the chances of
collisions. A good hash function produces different hash values for different inputs, but
collisions (when two different inputs produce the same hash value) are inevitable due to the
potential infinite input space and the finite size of the hash value.

Strategies for Avoiding and Resolving Collisions:

Separate Chaining:
In separate chaining, each bucket in the hash table is a linked list or other data structure that
can hold multiple elements. When a collision occurs, the new key-value pair is simply appended
to the list at the corresponding bucket. This strategy allows multiple elements to coexist at the
same index, avoiding data loss. However, it may lead to degraded performance if the linked lists
become too long.

Open Addressing:
Open addressing involves finding an alternative position within the hash table to place a
colliding element. When a collision occurs, a sequence of other locations (probed using a
specific technique) is examined until an empty slot is found. This technique ensures that every
element is stored in the hash table itself, without using additional data structures. Common
probing methods include linear probing (checking the next available slot), quadratic probing
(checking slots in a quadratic manner), or double hashing (applying a second hash function to
calculate the next slot). Open addressing can lead to increased clustering, which impacts lookup
performance, but it avoids the need for separate data structures.

You might also like