Skip to main content

AVL tree for 10, 20, 30, 35, 50, ...

Short answer

After insertions and rotations, the AVL tree is rooted at 35, with left child 20 (left 10, right 30) and right child 60 (left child 50 with left 40, right child 70 with left 65 and right 80).

Create an AVL balanced tree for the set of data 10, 20, 30, 35, 50, 70, 40, 80, 60, 65 by explaining each rotation rule used.

Insert each key as in a BST. Whenever a node gets balance factor ±2, apply the rotation for its case (LL, RR, LR or RL), stated in the table.

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:

CaseWhere the new node wentFix
LLleft subtree of left childsingle right rotation
RRright subtree of right childsingle left rotation
LRright subtree of left childleft rotation on the child, then right rotation
RLleft subtree of right childright rotation on the child, then left rotation

Step-by-step insertion

StepInsertTree after step (root(left,right))Imbalance / rotation
11010balanced
22010(-,20)balanced
33020(10,30)at 10: RR: left rotate at 10
43520(10,30(-,35))balanced
55020(10,35(30,50))at 30: RR: left rotate at 30
67035(20(10,30),50(-,70))at 20: RR: left rotate at 20
74035(20(10,30),50(40,70))balanced
88035(20(10,30),50(40,70(-,80)))balanced
96035(20(10,30),50(40,70(60,80)))balanced
106535(20(10,30),60(50(40,-),70(65,80)))at 50: RL: right at 70, left at 50

Final AVL tree

35
├─L:20
│  ├─L:10
│  └─R:30
└─R:60
   ├─L:50
   │  └─L:40
   └─R:70
      ├─L:65
      └─R:80

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

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 6 questions →