IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

DAA 18 Oct

18 Oct 20261h 25m2 parts

Source: IITP-big-files\Sem-1\ECS5101-DAA\Lectures\2026-10-18 0000 DAA 18 Oct\summary\notes.md

Overview

This class continues the study of binary search trees (BSTs) by confronting the problem of skewness, which degrades search from order log n to order n. The AVL tree is introduced as a balanced BST that restores logarithmic search by enforcing a balance factor of 0, 1 or −1 at every node. The class covers the balance factor, why search and traversal leave it intact while insertion and deletion disturb it, and the four rotation types (LL, RR, LR, RL), with detailed worked examples of LL and RR and a direct treatment of LR. Deletion rotations (R0, R1, R−1) are introduced at the end.

Learning outcomes

  • State the BST property and explain why BSTs disallow duplicate elements and how this removes redundant entries.
  • Explain how sorted insertion produces left-skewed or right-skewed trees and why search then becomes order n.
  • Define the balance factor and state the allowed AVL values (0, 1, −1).
  • Explain why search and traversal preserve balance while insertion and deletion violate it.
  • Identify the node to rotate on (the first node from the point of insertion whose balance factor is violated).
  • Name and distinguish the four rotation types LL, RR, LR, RL, reading names right to left.
  • Perform LL and RR rotations and verify the resulting balance factors.
  • Perform an LR rotation both directly and as RR followed by LL.
  • Build an AVL tree by successive insertions, rotating as needed.
  • Name the three deletion rotations R0, R1, R−1 and the balance-factor condition that selects each.
  • State that inorder traversal of a BST visits Left, Root, Right and yields the keys in ascending order.

Core concepts

BST (binary search tree / binary sorted tree)

A tree where all entries in the left side are less than the root, which is further less than the right entry. This property maintains searching time of order log n. It matters because it is the foundation on which AVL is built; every AVL tree is first a BST.

Inorder traversal of a BST

Inorder visits Left, Root, Right. Because of the BST property, the inorder traversal of a BST produces the keys in ascending order (for distinct keys).

Duplicates and redundant entries

Insertion and deletion both require a search first (deletion's search is mandatory; insertion must check whether the element exists). Because BST does not allow duplicate elements, redundant entries can be removed from a list, a database, or anywhere — BST helps avoid/remove redundant entries.

Skewness problem

Because left < root < right, elements arriving in sorted increasing or decreasing order give a left-skewed or right-skewed tree. Searching then reaches the order of linear search, i.e., order n — building the BST and searching at order n is pointless; better to go with linear search. The log n complexity is reached only if the height of the left subpart is comparable with the right subpart.

AVL tree

A balanced binary search tree that solves the skewness problem and avoids complexity becoming equivalent to linear. It must first be a BST. We always maintain the BST property and on top of it add the balance factor — then it becomes an AVL tree. AVL is a subset / special case of BST; the BST property can never be violated in an AVL tree.

Balance factor

Difference of left height minus right height. In an AVL tree the allowed balance factor is 0, 1 or −1 (no modular sign; it is height of left tree minus height of right tree). In the general/modular discussion the balance factor could be 0 or 1 using a modular sign, but for AVL it is specifically 0, 1 or −1.

Search and traversal in AVL

They do not change the structure of the tree, so BST search and traversal techniques remain intact for AVL trees; search and traversal do not violate the balance factor property.

Insertion/deletion in AVL

These disturb the balance factor, so rotations are needed to restore the property. Rotations are only required when the balance factor is not in the range 0, 1 or −1; otherwise no need to rotate.

Rotation target node

Rotations are performed on the first node from the point of insertion whose balance factor is violated (the node closest to the point of insertion). If that rotation does not balance the tree, further rotations are performed; rotations may be performed in multiple rounds. The rotation does not have to go up to the root; it is localized to that particular subtree. If many nodes violate (e.g., 10–20), the one closest to the point of insertion is picked first.

Four rotation types

LL, RR, LR, RL. Names are read from right to left: the first term says where the point of insertion is, the second says which subtree we are targeting. - Insert into the left of left subtree → LL rotation - Insert into the right of right subtree → RR rotation - Insert into the right of left subtree → LR rotation - Insert into the left of right subtree → RL rotation

LL rotation (clockwise / right rotation)

B goes up; A becomes the right child of B; the right child of B becomes the left child of A. (A and B are representative nodes — any nodes can be there.) Rotation happens toward the right because the insertion was on the left.

RR rotation (anticlockwise / left rotation)

The reverse of LL. The right child of A (B) becomes the parent of A; A becomes the left child of B; the left child of B becomes the right child of A. Rotation happens in the left direction because the insertion was in the right of the right subtree.

Mirror images, single and double rotations

LL and RR are mirror images of each other; LR and RL are similar in nature but mirror images of each other. LL and RR are single rotations; LR and RL are double rotations, since LR can be accomplished by RR followed by LL, and RL by LL followed by RR.

Direct LR rotation

LR (and RL) can also be performed directly in one step, without going through the two rotations; the choice is up to you. A is the first node (from the insertion) whose balance factor is disturbed; C is the node closest to the insertion. C goes up, A becomes the left child of C; C's left child becomes the right child of B, and C's right child becomes the left child of A. This is a two-way rotation. Via RR + LL: first RR — C goes up, B comes down (C's left child becomes the right child of B); then LL — C replaces A and A becomes the right side.

Deletion rotations

There are three primary rotations in deletion — R0, R1 and R−1 — chosen by the balance factor of node B: if B's balance factor is 0, perform R0; if 1, perform R1; if −1, perform R−1.

Balancing intuition

If the left height increases (insertion below a left node), rotate toward the right to decrease the left height; if the right height increases, rotate toward the left. The main agenda of AVL is to ensure left and right heights are comparable.

Practical note

In the current scenario, BST/AVL has shifted toward B-trees/B+ trees, which maintain only the indexes in RAM; the BST/AVL material is background for the next trees.

Formulas and maths

  • Balance factor = height of left tree − height of right tree. Symbols: left/right tree heights of a node. Used to test whether a node satisfies the AVL condition.
  • AVL allowed balance factor: 0, 1 or −1. Symbols: the three legal values. Used as the condition for "no rotation needed."
  • BST search time: order of log n (when balanced/comparable heights); order of n (when skewed). Symbols: n = number of elements. Used to motivate balancing.
  • Height for balance factor (Q&A method): for a given node, take the maximum number of nodes in any path on the left side and the maximum number of nodes in any path on the right side (including the leaf), then subtract. Symbols: path node counts including the leaf. Used to compute balance factors by hand.
  • Deletion rotation selection: B's balance factor 0 → R0; 1 → R1; −1 → R−1. Symbols: B = the node whose balance factor is examined. Used to pick the deletion rotation.
  • Inorder traversal order: Left, Root, Right; on a BST this lists the keys in ascending order. Symbols: the three visits per node. Used to output a BST's keys in sorted order.

Worked examples

Example tree ACGD (balance factor illustration): - Leaf node: left height 0, right height 0 → balance factor 0. - G: left height 1, right height 0 → balance factor 1. - C: left height 1, right height 2 → balance factor −1.

Inserting E into the ACGD tree: - E follows BST: goes to C (go right), then G (go left), then D (go right), then inserted. - Balance factors: A = 0 (leaf); E = 0; D: left height 0, right height 1 → 0 − 1 = −1; G: left height 2, right height 0 → 2; C: left height 1, right height 3 → 1 − 3 = −2. - Two nodes violate (−2 and 2); rotation is performed on the first node from the point of insertion, which is G, not C.

LL rotation example 1 (insert 36): - Tree: 96, 85, 110, 64, 90. Balance factors: 64 = 0, 90 = 0, 85 = 0, 110 = 0, 96: left height 2, right height 1 → 1. - Insert 36: 96 → left, 85 → left, 64 → left. So insertion is in the left of left subtree. - New balance factors: 90 = 0, 110 = 0; 64: 1 − 0 = 1; 85: 2 − 1 = 1; 96: 3 − 1 = 2 (violation). - A = 96, B = 85. After rotation: 85 goes up, 96 becomes the right child of 85, and 85's right child becomes the left child of A. - After rotation: 36 = 0, 90 = 0, 110 = 0, 64 = 1, 96 = 0, 85 = 0 (2 − 2). Property restored.

LL rotation example 2 (insert 18): - Tree: 85, 64, 96, 36, 90, 110. Balance factors: 36 = 0, 90 = 0, 110 = 0, 96 = 0, 64 = 1, 85 = 0. - Insert 18: 85 → left, 64 → left, 36 → left. Resultant tree: 85, 64, 96, 36, 18, 90, 110. - Balance factors: 18 = 0, 36 = 1, 64 = 2 − 0 = 2 (violation), 90 = 0, 110 = 0, 96 = 0, 85 = 3 − 2 = 1. - A = 64, B = 36. Rotation around 64: 36 goes up, 64 becomes the right child, right child of B (none) becomes the left child of 64; 18 and the rest of the structure remain intact. - After rotation all balance factors are 0 or 1 − 1 = 0; 85 = 2 − 2 = 0.

RR rotation example 1 (insert 65): - Tree: 34, 26, 44, 40, 56. Balance factors: 0, 0; 44 = 1 − 1 = 0; 26 = 0; 34 = 1 − 2 = −1. - Insert 65: 34 → right, 44 → right, 56 → right (right of right subtree). - New balance factors: 65 = 0; 56 = 0 − 1 = −1; 40 = 0; 44 = 1 − 2 = −1; 26 = 0; 34 = 1 − 3 = −2 (violation). - Rotation: 44 becomes the parent of 34; the left child of 44 (40) becomes the right child of 34. - After rotation the balance factor is either 0, 1 or −1.

RR rotation example 2 (insert 81): - Tree: 44, 34, 56, 26, 40, 65. Insert 81 → tree: 44, 34, 56, 26, 40, 65, 81. - Balance factors: 56 = −2 (violating point); 44 = 2 − 3 = −1. - Rotation along 56: 65 goes up; there is no left child of 65, so nothing to add; result: 44, 34, 26, 40, 65, 56, 81. - After rotation: balance factors 0, 0, 1 − 1 = 0, 0, 1 − 1 = 0, and 44 = 2 − 2 = 0. Now it is an AVL tree.

In-class exercise (insert 17 and 85): - Starting tree: 44, 34, 65, 26, 40, 56, 81. - Insert 17: 44 → 34 → 26 → inserted. Balance factors: 17 = 0, 26 = 1, 34 = 1 (not 2 − 1), 65 = 0, 56 = 0, 81 = 0, 44 = 3 − 2 = 1. No violation → no rotation. - Insert 85: 44 → 65 → 81 → inserted. Resultant tree: 44, 34, 65, 26, 40, 56, 81, 17, 85. - Balance factors: 17 = 0, 26 = 1, 40 = 0, 56 = 0 (leaf nodes hold balance factor 0), 34 = 1, 81 = 0 − 1 = −1, 65 = 1 − 2 = −1, 44 = 3 − 3 = 0. Nothing to rotate — all balance factors are in the range 0, 1 or −1.

LR example (44, 30, 76, 16, 39; insert 37): - Balance factors: 37 → 0, 39 → +1, 16 → 0, 30 → 1 − 2 = −1, 76 → 0, 44 → 3 − 1 = 2. Insertion is on the right of the left subtree, so C = 39, B = 30, A = 44. - Direct LR: 39 goes up, 44 comes down; 39's left child becomes the right child of B; 39's right child (none existed) would become the left child of A. Result is balanced. - RR + LL: First RR on the portion around 39/30: 39 goes up, 30 comes down, 39's left child becomes the right child of 30. Then LL with respect to 39 and 44: 39 goes up, 44 comes down, 76 stays as is, with 30, 16 and 37 in place. Same result as direct LR.

RL identification (insert 10, 30, 20): - Insert 10: single node, balance factor 0. - Insert 30: right child of 10; 30 = 0, 10 = −1. - Insert 20: 10 → right to 30, then left to 20. The insertion is in the left of the right subtree → RL rotation (not RR, not LR).

Building an AVL tree (insert 64, 1, 14, 26, 13, 110, 98, 85): - Insert 64: single node, balance factor 0. - Insert 1: left child of 64; 1 has balance factor 0, 64 has +1. - Insert 14: goes right of 1; 14 has balance factor 0, 1 has −1, and 64 has left height 2, right height 0 → balance factor 2 → violation. Insertion is on the right of the left subtree → LR rotation: 14 goes up, 1 stays, 64 is placed; 14 has no left/right children so they remain blank. - Insert 26: 14 right, 64 left; balance factors 0, 0, 1, and 1 − 2 = −1 above → no violation. - Insert 13: goes to 14, then left to 1, then right of 1. Balance factors 0, 0, −1, 0; 64 is +1 and 14 is 0 → no violation. - Insert 110: 14 right, 64 right. - Insert 98: 14 right, 64 right, 110 left. Balance factors: 64 → 1 − 2 = −1; 14 → 2 − 3 = −1 → no problem. Only one rotation performed so far. - Insert 85: 14 right, 64 right, 110 left, 98 left. Balance factors: 64 → −2 and 14 → −2 (two violating nodes). Consider the first one; insertion is on the left of the left subtree → LL rotation: 98 goes up, 85 comes as left child, 110 becomes the right child. Resulting tree: 14 with 1 (and 13), 64 with 26, 98 with 85 and 110. Balance factors: 64 → −1, 14 → −1 → tree is balanced, no further rotation needed.

Deletion setup (R0 example): - A tree with node B, heights h on both sides, and a node x deleted from the right of the right subtree; the tree's balance factor is disturbed.

Questions asked in class

Parambir (on the LL example with 18 inserted): asked to explain the rotation where the imbalance happened at 64. Answer: the rotation is around 64 — a clockwise rotation of a single step; 36 moves up and becomes the child, and 36's position is taken over. The rotation does not have to happen right up to the root; it happens at the nearest node which has the imbalance (64 has balance factor 2). It will not rotate 64 up to 85 or rotate the whole tree — it is localized to that particular subtree. In the earlier example the balance factor of 96 was disturbed, so we moved to the first node that was disturbing; if it happens locally, we perform it locally and do not go to the root, because the tree may be very large.

Lalith: asked whether every time we do an insertion we have to check and balance. Answer: yes — keep inserting, but before insertion of the new node check the balance factor; if rotation is required, rotate it, then insert the new node. It does not mean we insert all the nodes and then start balancing; every time we insert, we have to balance. If we