5101 - Design & Analysis of Algo
← 5101 - Design & Analysis of Algo
session: "5101-design-analysis-of-algo/2026-10-18 0000 DAA 18 Oct"
title: "DAA 18 Oct"
date: "2026-10-18 00:00"
duration: "1h 25m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 39666
generated: "2026-11-23T23:59:32+05:30"
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Symbols: left/right tree heights of a node. Used to test whether a node satisfies the AVL condition.
Symbols: the three legal values. Used as the condition for "no rotation needed."
Symbols: n = number of elements. Used to motivate balancing.
Symbols: path node counts including the leaf. Used to compute balance factors by hand.
Symbols: B = the node whose balance factor is examined. Used to pick the deletion rotation.
Example tree ACGD (balance factor illustration):
Inserting E into the ACGD tree:
LL rotation example 1 (insert 36):
LL rotation example 2 (insert 18):
RR rotation example 1 (insert 65):
RR rotation example 2 (insert 81):
In-class exercise (insert 17 and 85):
LR example (44, 30, 76, 16, 39; insert 37):
Building an AVL tree (insert 64, 1, 14, 26, 13, 110, 98, 85):
Deletion setup (R0 example):
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
One line per segment, [hh:mm:ss] timestamp first.
loading…