---
title: "AVL tree for 24, 12, 8, 15, 35, ..."
subject: "Data Structure and Algorithm"
chapter: 6
key: "semester_4/data_structure_and_algorithm:avl-build-24-12-8"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/avl-build-24-12-8
site: BCT Engineering
---

# AVL tree for 24, 12, 8, 15, 35, ...

Data Structure and Algorithm · Chapter 6: Trees

- Asked in: 2068 Magh, 5 marks
- Video: [Lec-59: How to Create AVL tree | LL, RR, LR, RL Rotation in AVL | Data Structure](https://www.youtube.com/watch?v=vEZ3-bdPdUQ) — Gate Smashers
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/avl-build-24-12-8
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-6

## Question

Create an AVL tree from the data: 24, 12, 8, 15, 35, 30, 57, 40, 45, 78.

## Short answer

Final AVL tree: root 24, left child 12 (left 8, right 15), right child 45 (left 35 with children 30 and 40, right 57 with right 78).

## Answer

Insert each key as in a BST and rebalance with rotations when a balance factor becomes ±2.

Balance factor of a node = height(left subtree) − height(right subtree). It must stay −1, 0 or +1. After each insertion, find the nearest unbalanced ancestor of the new node and fix it:

| Case | Where the new node went | Fix |
|---|---|---|
| LL | left subtree of left child | single right rotation |
| RR | right subtree of right child | single left rotation |
| LR | right subtree of left child | left rotation on the child, then right rotation |
| RL | left subtree of right child | right rotation on the child, then left rotation |

#### Step-by-step insertion

| Step | Insert | Tree after step (root(left,right)) | Imbalance / rotation |
|---|---|---|---|
| 1 | 24 | `24` | balanced |
| 2 | 12 | `24(12,-)` | balanced |
| 3 | 8 | `12(8,24)` | at 24: LL: right rotate at 24 |
| 4 | 15 | `12(8,24(15,-))` | balanced |
| 5 | 35 | `12(8,24(15,35))` | balanced |
| 6 | 30 | `24(12(8,15),35(30,-))` | at 12: RR: left rotate at 12 |
| 7 | 57 | `24(12(8,15),35(30,57))` | balanced |
| 8 | 40 | `24(12(8,15),35(30,57(40,-)))` | balanced |
| 9 | 45 | `24(12(8,15),35(30,45(40,57)))` | at 57: LR: left at 40, right at 57 |
| 10 | 78 | `24(12(8,15),45(35(30,40),57(-,78)))` | at 35: RR: left rotate at 35 |

#### Final AVL tree

```text
24
├─L:12
│  ├─L:8
│  └─R:15
└─R:45
   ├─L:35
   │  ├─L:30
   │  └─R:40
   └─R:57
      └─R:78
```

Height = 4, and every node has balance factor −1, 0 or +1.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. After inserting 24, 12, 8, which case occurs at 24 and what is the result?
   A. RR case; a left rotation at 24 makes 12 the root
   B. LR case; double rotation makes 8 the root
   C. No imbalance occurs; 24 remains the root
   D. LL case; a right rotation at 24 makes 12 the root
   Answer: D. 8 goes to the left of 12, which is left of 24, an LL case. A single right rotation gives 12(8,24). (Key point: LL rotation)

2. After inserting 30, node 12 is unbalanced. Which case is it?
   A. LL: rotate right at 12
   B. RR: 30 lies in the right subtree of the right child (24), so rotate left at 12
   C. LR: rotate left at 8, then right at 12
   D. RL: rotate right at 24, then left at 12
   Answer: B. 30 goes 12 -> 24 -> 35 -> left of 35. Both steps from 12 are to the right, so a single left rotation at 12 fixes it. (Key point: RR rotation at 12)

3. Inserting 45 unbalances 57. Which fix is applied?
   A. RR: single left rotation at 57
   B. LL: single right rotation at 57
   C. RL: right rotation at 40, then left rotation at 57
   D. LR: left rotation at 40, then right rotation at 57
   Answer: D. 45 is greater than 40 and less than 57, so it lies in the right subtree of 57's left child: LR, giving 45(40,57). (Key point: LR rotation at 57)

4. What are the root and height of the final AVL tree?
   A. Root 45, height 4
   B. Root 24, height 4
   C. Root 24, height 6
   D. Root 12, height 3
   Answer: B. The last rotation (RR at 35) only changes the right subtree, so 24 stays the root; the longest path 24-45-57-78 gives height 4 (counting levels). (Key point: Final tree)

## Related in Data Structure and Algorithm

- [AVL tree for 10 20 30 50 45 40 8 5 3](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aavl-build-10-20-30) — Data Structure and Algorithm, chapter 6
- [AVL tree for 10, 20, 30, 35, 50, ...](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aavl-build-10-20-30-35) — Data Structure and Algorithm, chapter 6
- [AVL tree for 52, 65, 72, 15, 11, 32, 20](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aavl-build-52-65-72) — Data Structure and Algorithm, chapter 6
- [AVL tree for 3, 2, 1, 4, 5, 6, 7, ...](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aavl-build-3-2-1-4) — Data Structure and Algorithm, chapter 6
- [AVL tree for month names](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aavl-build-months) — Data Structure and Algorithm, chapter 6, asked 2×
- [B-tree insertion algorithm; AVL of months](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abtree-insertion-avl-months) — Data Structure and Algorithm, chapter 6, asked 2×

## Related in other subjects

- [EVA and S-curve; Rs 30,00,000, 24 months](https://www.bctengineer.com/question.md?key=semester_7%2Fproject_engineering%3Aeva-s-curve-24-months-c76) — Project Engineering, chapter 4
- [Volume from central depths 1.85, 2.15, 2.45](https://www.bctengineer.com/question.md?key=semester_3%2Fsurveying_i%3Avolume-central-depths-1-85-2-15-2-45) — Surveying I, chapter 9
