Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

COMPUTER SCIENCE DEPARTMENT

DATA STRUCTURES AND


ALGORITHMS
ASSIGNMENT # 3

NAME: Malik Huzaifa


CLASS: BSCS-4A
ROLL NO: R1F22UBSCS043
Submitted to:
Prof Saira Mazhar
➢ Definitions
a) Define what a self-balancing tree is. Why are self-balancing trees important in computer science?

A self-balancing tree is a binary search tree (BST) that automatically keeps its height (maximum number of edges from
the root to a leaf) as small as possible after insertions and deletions. This ensures that the operations like insertion,
deletion, and searching can be performed efficiently.

Importance in Computer Science:

Self-balancing trees are crucial in computer science because they maintain O(log n) time complexity for insertion,
deletion, and lookup operations. This efficiency is essential for performance-critical applications like databases, file
systems, and network routers, where maintaining quick access to data is paramount. Without self-balancing
mechanisms, a BST can degenerate into a linear structure (like a linked list) in the worst-case scenario, leading to O(n)
time complexity for operations, which is undesirable.

b) Briefly explain the key properties that make AVL trees self-balancing.

An AVL tree is a type of self-balancing binary search tree named after its inventors, Adelson-Velsky and Landis. It has the
following key properties:

1. Height Balance Property:


Each node in an AVL tree maintains a balance factor, which is the difference in height between its left and right
subtrees. Formally, for every node, the balance factor must be -1, 0, or +1. This constraint ensures that the tree remains
approximately balanced at all times.

2. Rotations:
To maintain the height balance property during insertions and deletions, AVL trees use rotations (single or double) to
rebalance the tree. There are four types of rotations:

• Right Rotation (RR)


• Left Rotation (LL)
• Left-Right Rotation (LR)
• Right-Left Rotation (RL)

These properties ensure that the height of the AVL tree remains O(log n), guaranteeing efficient performance for
standard operations.

➢ Tree Formation
a) Describe the steps involved in forming an AVL tree.

Steps to Form an AVL Tree:

1. Start with an Empty Tree:


Begin with an empty tree where the root is null.

2. Insert Nodes One by One:


Nodes are inserted following the binary search tree property (left child < parent < right child). For each node,
determine its appropriate position in the tree.

3. Update Balance Factors:


After inserting a node, update the balance factors of the nodes along the path from the inserted node to the root.

4. Check for Imbalance:


If the balance factor of any node becomes -2 or +2, the tree is imbalanced at that node.

5. Apply Rotations to Restore Balance:


To restore balance, apply the appropriate rotation(s):
• Single Rotation (when imbalance is due to two nodes on the same side): Perform RR or LL rotation.
• Double Rotation (when imbalance is due to nodes on opposite sides): Perform RL or LR rotation.

6. Repeat the Process:


Continue the process of inserting nodes and rebalancing until all nodes are inserted.

➢ Insertion Process
a) Explain the insertion process in an AVL tree. How does the tree maintain its balance after each insertion?

The insertion process in an AVL tree involves the following steps:

1. Standard BST Insertion:


Insert the new node in the appropriate position following the binary search tree property. This is a recursive process
where the new node is compared with existing nodes to find its correct position.

2. Update Balance Factors:


After insertion, traverse back up the tree from the newly inserted node to the root, updating the balance factors of
each node. This step identifies nodes that have become unbalanced due to the insertion.

3. Identify Imbalance:
Check the balance factor of each node. If a node's balance factor becomes -2 or +2, it indicates an imbalance.

4. Determine Rotation Type:


Depending on the location of the inserted node and the subtree that caused the imbalance, determine the type of
rotation needed:
• LL (Left-Left) Rotation: When the inserted node is in the left subtree of the left child.
• RR (Right-Right) Rotation: When the inserted node is in the right subtree of the right child.
• LR (Left-Right) Rotation: When the inserted node is in the right subtree of the left child.
• RL (Right-Left) Rotation: When the inserted node is in the left subtree of the right child.

5. Perform Rotation:
Apply the necessary rotation to rebalance the tree. The rotation adjusts the nodes' positions to restore balance while
preserving the binary search tree properties.

6. Rebalance the Tree:


After rotation, the tree is rebalanced, ensuring that the height difference between the left and right subtrees of any
node is within the allowed range (-1, 0, or +1).

7. Repeat Until Tree is Balanced:


Continue to check and apply rotations until the entire tree is balanced.

Example of Insertion and Rebalancing:

Suppose we insert nodes into an AVL tree in the order: 10, 20, 30.
1. Insert 10:
- Tree: `10`
- No imbalance.

2. Insert 20:
- Tree: `10 -> 20`
- No imbalance, balance factor of 10 is -1.

3. Insert 30:
- Tree: `10 -> 20 -> 30`
- Imbalance detected at node 10 (balance factor = -2).
- Apply RR rotation on 10.
- Resulting Tree:

20
/\
10 30

- Tree is now balanced.

By following these steps, the AVL tree maintains its balanced structure, ensuring optimal performance for various
operations.
These explanations provide a comprehensive overview of self-balancing trees, particularly AVL trees, and detail the
formation and insertion processes with examples.

You might also like