IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

DAA 24 Oct

24 Oct 20261h 17m2 parts

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

Overview

This class reviews the BST property and balance factor, then works through AVL rotations for both insertion (LL, RR, LR, RL) and deletion (R0, R1, R−1), including several numeric deletion examples. It then motivates and introduces M-way trees, B trees and B+ trees as structures for indexing and multi-level indexing in secondary storage, and shows how an M-way tree node is formed (including a hand-drawn 3-way tree). Searching and insertion in an M-way tree are demonstrated with examples, while detailed insertion/deletion in M-way trees is deferred to the next class. The class sits at the transition from balanced binary trees (AVL) to multi-way balanced search structures (B/B+ trees), ahead of heaps, graphs and dynamic programming.

Learning outcomes

  • State the BST property and compute the balance factor of any node, including after insertions and deletions.
  • Identify the first node (from the point of insertion/deletion) that violates the AVL property and classify the required rotation as LL, RR, LR or RL.
  • Perform LL, RR, LR and RL rotations step by step.
  • Classify deletion rotations as R0, R1 or R−1 from the balance factor of node B, and map them to LL/LR.
  • Explain why M-way trees are needed for secondary-storage indexing and multi-level indexing.
  • Describe an M-way tree node (addresses and keys), the relation key = m − 1 and address = m, and the ordering/subtree-range properties.
  • Search for a key in an M-way tree and insert a key where space permits.
  • Count the maximum number of nodes at a given level of a full M-way tree.

Core concepts

BST property and balance factor

The BST property: left is always less than the root, which is always less than the right; you need not check the whole tree — just pick any three nodes (root, left, right); if left < root < right holds everywhere, the whole tree from root to leaf is a binary search tree. The balance factor is the difference between left height and right height; with the mod value it can be 0 or 1, without the mod (left height minus right height) it can be 0, 1 or −1. This is the invariant that AVL rotations restore, so it underpins everything that follows.

Operations and what changes structure

The operations are insertion, deletion, searching and traversal. In traversal you have to "visit" the node; in searching you identify a specific item of interest. Searching and traversal do not modify the tree structure, so the criteria used in a BST are the same for an AVL tree; insertion and deletion change the balance factor (the difference in left and right height gets disturbed). This distinction explains why only insertion and deletion trigger rotations.

Rotations for insertion (LL, RR, LR, RL)

The rotation happens on the first node (from the point of insertion) that violates the balance factor property; that node acts as the centre. Types by insertion position: insert on the left of the left subtree → LL; insert on the right of the right subtree → RR; insert on the left of the right subtree, or on the right of the left subtree → LR and RL. - LL steps: designate the first node from the point of insertion that violates the AVL/balance-factor property as A; its left child is B. Make B the parent of A and A the right child of B. Further, the right child of A, if any, becomes the left child of B. (The transcript also says "left child of E" in one place — [unclear]; the consistent statement is: B's right child becomes A's left child.) - RR: mirror image of LL. A is the first violating node; A's right child is marked B. A becomes the left child of B, and B's left child becomes the right child of A. - LR: a double rotation — LR is nothing but RR followed by LL. Identify three nodes A, B and C (insertion is on the right of the left subtree). C becomes the root; A becomes the right child of C; the left child of C is the right child of B; the right child of C becomes the left child of A. (The transcript also notes A's left child is B and B's right child is C — the "resemblance" of LL and RR inside LR.) - RL: just a mirror image, as with LL and RR.

Deletion rotations R0, R1, R−1

Deletion also changes the AVL property, so three types of rotation are performed — R−1, R0 and R1 — named by the balance factor of node B (the left child of A, where A is the first node violating the property): balance factor of B = 0 → R0; = 1 → R1; = −1 → R−1. Equivalences to remember: insertion on LL is equivalent to deletion on RR (and removing something from the right is equivalent to insertion on the left); R0 is equivalent to LL (B has balance factor 0); R1 is also equivalent to LL (B has balance factor 1); R−1 is equivalent to LR (B has balance factor −1). Important note: A is not always the root node; it can be any intermediate node that violates the AVL tree property — the "A is the root" scenario is not applicable when the tree is very large (the examples use small trees).

Motivation: indexing and multi-level indexing

In secondary storage (primarily the hard disk), data is stored in sectors/blocks and an index is maintained so retrieval is easy. Like using a book's index page to jump to a chapter/page instead of searching the entire book, an index gives direct/frequent access to data and keeps search time short. If the index itself is very large, another index over it (e.g., chapter-level, then topic-level) is kept — this is multi-level indexing. Since multiple values must be maintained at different levels, a single element per node (as in a BST) is not enough, so nodes with m children are used. BST and other structures discussed so far are stored in internal memory and support internal information retrieval; to favor retrieval and manipulation of data in external memory (storage devices such as hard disks), specialized data structures are required — M-way search tree, B tree and B+ tree allow different retrieval patterns for secondary storage, and the height of the tree is close to log of n, so search is managed accordingly.

M-way tree, B tree, B+ tree

An M-way tree is a tree whose node can have m children (m could be 2, 3, 4, 5, 7, whatever we want); each node does not hold a single value like a BST but could hold a set of elements, an array, or even an entire index. A B tree is a balanced M-way tree; a B+ tree is a variant of the B tree (there is a slight difference between the two). In the 5-way example, the maximum allowed child is 5 and the minimum is always 2; if the way is not given, identify any node having the maximum number of children — that number is the way of the tree. Each node has an address pointer field and a key field: for a 5-way tree, addresses can be 5 and keys can be 4, i.e., key = m − 1 and address = m; keys in all nodes are arranged in ascending order. A tree drawn from scratch fills all nodes on one level before going down, so its formation looks different from a resultant tree produced by multiple insertions and deletions.

M-way search tree properties

  • Empty tree: no key and no address/child pointer.
  • Order: for some integer m known as the order of the tree, each node is of degree which can reach a maximum of m — i.e., each node has at most m child nodes.
  • Node representation: a node may be represented as a0 k1 a1 k2 a2 … k−1 a−1, where ki are the keys and a0 are pointers to subtrees. If a node has k children (k ≤ m), it can have k − 1 keys (e.g., 5 children → maximum 4 keys).
  • Key ordering: k of the i-th node is less than k of the (i+1)-th node (ascending order).
  • Subtree property: all key values in the subtree pointed by ai are less than k(i+1); all keys in the subtree pointed by a(m−1) are greater than a(m−1). Example: the tree pointed by 18 on the left is less than 18; on the right greater than 18 but less than 44. Values stored at address A1 lie between 18 and 44; at A2 between 44 and 76; at A3 between 76 and 198.
  • Merging intuition: BSTs merged — the middle one holds values greater than one side but less than the other.

Searching and insertion in an M-way tree

Searching is similar to BST: start at the root, fetch the element in the root, get a direction key for where to move. Maximum comparisons per node is m − 1, much less than the total number of nodes in the tree, so search time again tends toward log n. As in BST and AVL, the m-way tree (described as an extended variant of the binary search tree) has no duplicate element; before inserting, first search whether the element is available; if not available, add it, otherwise deny. Insertion can happen on a node up to when there is sufficient space; if there is a downward key, first explore that path; if not found, add there.

Discussion: RAM, VRAM and caching

Keys such as 46, 20, 54, 60 may be indexes or locations of data to fetch; if data is stored at some location y pointed to by 60, removing 60 means there is no pointer to the data, and the data is automatically removed. If a BST is totally in the leftward direction, it becomes a linear search; AVL restores the height on every insertion or deletion. VRAM is also part of the hard disk; such logical arrangements help — without them we would never get VRAM in our PC; it is an ensemble/combined approach to provide extra space by creating a logical arrangement to get more RAM in memory, with data whose immediate requirement is not there pushed toward VRAM and pulled toward RAM. Caching and logical organization are related, but caching is managed by different properties, primarily aligned with scheduling rather than tree structures; caching concerns the frequency of the data being witnessed.

Formulas and maths

  • Balance factor = left height − right height; allowed values 0, 1, −1 (with mod: 0 or 1). Symbols: left height and right height are the heights of a node's left and right subtrees. Used to test whether a node satisfies the AVL property.
  • Balance factor range: 0, 1 or −1. Symbols: the three permitted values of the balance factor. Used as the AVL invariant that rotations restore.
  • m-way tree: key = m − 1; address = m. Symbols: m is the way/order of the tree (maximum children per node); key is the maximum number of keys per node; address is the number of address/child pointers per node. Used to size an M-way tree node.
  • If a node has k children (k ≤ m), it can have k − 1 keys. Symbols: k is the actual number of children of a node, m the maximum. Used to check node occupancy (e.g., 5 children → maximum 4 keys).
  • Maximum comparisons per node during search: m − 1. Symbols: m is the way of the tree. Used to bound the work per node when searching an M-way tree.
  • Search time tends toward log n. Symbols: n is the number of elements. Used to state the search complexity of balanced M-way structures.
  • AVL discussion: search will not reach order of n; it will be somewhere order of n or n plus one or n plus two (as stated). Symbols: n is the number of elements. Used to describe how the balance factor keeps search near logarithmic rather than linear.
  • Maximum nodes at level L of a full M-way tree of order m: m^L (root at level 0). Symbols: m is the way/order of the tree (maximum children per node), L the level with the root at level 0. Used to count the capacity of a level, e.g. order 3 at level 3 gives 3^3 = 27 nodes.

Worked examples

Balance factor computations

  • Leaf nodes (7, 24, 60): 0.
  • Node 18: left child 1, right child 0 → 1 − 0 = 1.
  • Node 20: left child 2, right child 2 → 2 − 2 = 0.
  • Node 23: no left child, one right child → 0 − 1 = −1.
  • Node 54: 0 − 1 = −1.
  • Node 46: +1.
  • After deleting 60: node 54 has balance factor 0; node 46 has 3 − 1 = +2.
  • After removing 24 and then 23: node 20's balance factor becomes 2 − 0 = 2.
  • When removing 39: node 37 has balance factor 2.

LL rotation (insertion)

Inserting 36 — 85 goes up, 96 comes down; 85's right child becomes the left child of 96. Three steps: 85 goes up; 96 becomes 85's right child; the right child of 85 becomes the left child of 96.

RR rotation

Inserting 65 — 34 becomes the first node violating the AVL property, designated A; its right child is designated B; the rotation is performed accordingly.

Deletion, R0

Tree with nodes 7, 24, 60, 18, 20, 23, 54, 46 (balance factors computed as above). Delete 60 (first identify the node per BST, then delete). Node 46 becomes the first node from the point of deletion with balance factor +2; designate its left child as B. B's balance factor is 0, so the rotation is R0, equivalent to LL. After the rotation, all balance factors are in the range 0, 1, −1.

Deletion, R1 (remove 24, then 23)

Remove 24 — node 20's balance factor does not change (18, 17, 23). Then remove 23 — node 20's balance factor becomes 2, and the left child B has balance factor 1, so R1 is performed. R1 is again a resemblance of LL: B goes up, A comes down.

Deletion, R1 (remove 39)

Node 37 gets balance factor 2; 26 goes up, 37 comes down, and the right child of 26 becomes the left child of 37. This is LL; it is called R1 because B's balance factor is 1 (if it were 0 it would be R0).

Deletion, R−1 (remove 52)

B has balance factor −1 and A has 2; identify C on the right side of B and perform the LR rotation: C goes up, A comes down, the left child of C becomes the right child of B, and the right child of C becomes the left child of A.

M-way tree formation by merging BSTs

Start from BSTs such as {17, 16, 21} and {22, 23, 21, 27} (all valid BSTs). Merge them: 13's left child is kept; merging with 17 gives a pointer holding two values, 15 and 16; node 23 holds two values, 21 and 22 (binary trees don't allow duplicates, hence 22); 27 is also there. The result has 4 children — a 4-way tree with at most 3 elements per node, values in increasing order (13, 17, 23). M-way search tree property from the example: it is like a merger where the common points hold the BST property — values on the left side are less than the node's value and values on the right side are greater; the child between 13 and 17 holds values greater than 13 and less than 17 (15, 16); the child between 17 and 23 holds values greater than 17 and less than 23 (21, 22); values greater than 23 are stored to the right.

5-way tree example

Nodes with 1, 2, 3, 4, 5 children; keys 18, 44, 76, 198, 262, 186, 144, 80, 90, 7, 6, 146 appear in the discussion. Maximum allowed child 5, minimum 2; keys per node 1 to 4. Child can vary from 2 to 5 and key from 1 to 4 (e.g., a node with 2 children is equivalent to a BST node; nodes with 3 keys/4 children, 1 key/2 children, 2 keys/3 children appear). Subtree range: left children of 18 are less than 18; for 198, left children are less than 198 and greater than 76, right children are greater than 198.

Searching 186

At the node with 44, 76 (and 80), 186 lies between 76 and 90, so we get an address and reach the next node; there 144 is greater, and 186 is identified.

Inserting 6

18 says 6 could be placed on its left; at the node with 7, 6 goes on the left because 7 has no left child; the node's key count is only two (capacity 4), so 6 is directly inserted. (Inserting 146 was mentioned as an insertion example; details deferred to the next class.)

Hand-drawn 3-way tree

Values 17, 32, 45, 73, 29, 44, 48, 47, 41 (unique values, no duplicates). Three-way tree → maximum keys 2, maximum children 3. - 17 added first (root); 32 added to the root (space was there); root is then full. - 45 comes and goes to the right of 32. - 73 comes and is accumulated there because maximum allowed keys are two. - 29 is placed (greater than 17, less than 32 — left side). - 44: the node says "I am full", so 44 becomes a left child. - 48: 45 says it can be on its right. - 47: 45 says go to my right, 73 says you can go to my left, so 47 is added there (just before 48). - 41 is inserted (in that position). - General rule stated: fill one level, then change the level, so a compact structure is formed.

Maximum nodes at level 3 of a full 3-way tree

For a full M-way tree of order 3 with the root at level 0, the maximum number of nodes at level 3 is 3^3 = 27.

Questions asked in class

  • Q (Anuja): Request to go to the deletion slide and explain the R0/R1 deletion — can it be done with R0 also? A: Yes, we can do it with R0 also; the example being discussed is the one where 60 is deleted.
  • **Q