Skip to content

Commit

Permalink
Update Rotations Contents
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyhuynh committed Oct 30, 2024
1 parent 19f9d3d commit c46b7eb
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions DataStructure/16_TreeApplications/Rotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

In an **AVL Tree**, rotations are necessary to maintain the height balance after insertion or deletion. The balance factor of a node is calculated as:

## Balance Factor
```
Balance Factor = height(left subtree) - height(right subtree)
```
Expand All @@ -21,7 +22,7 @@ Below is a guide on when to use each case based on the **balance factor** and th

---

#### **1. Right Rotation (LL Rotation)**
### **1. Right Rotation (LL Rotation)**

**Use When:**
- The imbalance occurs in the **left subtree** of the **left child** (left-heavy tree).
Expand All @@ -45,7 +46,7 @@ Inserting 10, then 5, then 2 causes an LL imbalance.

---

#### **2. Left Rotation (RR Rotation)**
### **2. Left Rotation (RR Rotation)**

**Use When:**
- The imbalance occurs in the **right subtree** of the **right child** (right-heavy tree).
Expand All @@ -69,7 +70,7 @@ Inserting 10, then 15, then 20 causes an RR imbalance.

---

#### **3. Left-Right Rotation (LR Rotation)**
### **3. Left-Right Rotation (LR Rotation)**

**Use When:**
- The imbalance occurs in the **left subtree** of the **right child** (mixed imbalance).
Expand All @@ -96,7 +97,7 @@ Inserting 10, then 5, then 8 causes an LR imbalance.

---

#### **4. Right-Left Rotation (RL Rotation)**
### **4. Right-Left Rotation (RL Rotation)**

**Use When:**
- The imbalance occurs in the **right subtree** of the **left child** (mixed imbalance).
Expand Down Expand Up @@ -153,10 +154,6 @@ Inserting 10, then 15, then 13 causes an RL imbalance.

---

By understanding the nature of the imbalance (which subtree the new node is added to), you can decide which rotation or combination of rotations is needed to restore the AVL tree's balance.

---

## **1. Right Rotation (LL Rotation)**
Occurs when there is a left-heavy imbalance.

Expand Down

0 comments on commit c46b7eb

Please sign in to comment.