BURHAN
BURHAN
BURHAN
BURHAN KHAN
SUBMITTED TO:
MAM TAHREEM
REG NO:
FA21-BCS-216
SUBJECT:
DSA
QUESTION 1
i. AVL Tree
Self-balancing binary search trees known as AVL trees have left and right subtree heights that
deviate by no more than one for each node. This balancing characteristic enables both efficient
operations and a balanced tree structure.
Insertion
Start at the root and traverse the tree to find the appropriate position for the new node
based on its value.
Perform a standard binary search tree insertion by comparing the value of the new node
with the current node and going left or right accordingly.
After inserting the new node, check the balance factor of each node encountered during
the traversal. The balance factor of a node is defined as the difference between the
heights of its left and right subtrees.
Right-Right (RR) Rotation: If the balance factor of the node is less than -1 and
the new node is inserted in the right subtree of the right child, perform a left
rotation.
Left-Right (LR) Rotation: If the balance factor of the node is greater than 1 and
the new node is inserted in the right subtree of the left child, perform a left
rotation followed by a right rotation.
Right-Left (RL) Rotation: If the balance factor of the node is less than -1 and the
new node is inserted in the left subtree of the right child, perform a right rotation
followed by a left rotation.
Deletion
Start at the root and traverse the tree to find the node to be deleted based on its value.
Perform a standard binary search tree deletion by considering different cases:
If the node from which the key is removed falls below the minimum number of keys,
perform rebalancing steps:
Borrow a key from a sibling node (left or right) if the sibling has more than the
minimum number of keys.
If both siblings have only the minimum number of keys, merge the node with one
of its siblings, reducing the height of the tree.
Update the parent node accordingly and repeat the rebalancing process if
necessary.
QUESTION 2
HASHING
Hashing is a technique used in computer science to map data of arbitrary size to a
fixed-size value. It is commonly used for efficient storage and retrieval of data in
data structures called hash tables. Hashing involves using a hash function to
convert the input data into a unique hash code or hash value.
HASH TABLE
Hash tables are data structures that provide fast insertion, deletion, and retrieval
operations, using an array of buckets or slots to store key-value pairs. The hash
code determines the index of the bucket where the value will be stored.
HASH FUNCTION
A hash function takes an input (such as a string, number, or object) and produces
a fixed-size output, typically a hash code. The key properties of a good hash
function are:
Deterministic: For the same input, the hash function should always
produce the same output.
Uniformity: The hash function should distribute the inputs uniformly
across the range of possible hash values.
Efficiency: The hash function should be computationally efficient to
calculate.
Two strategies for avoiding and resolving collisions in hashing.
Separate Chaining.
Open Addressing.