IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

DAA 11 Oct

11 Oct 20261h 31m2 parts

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

Overview

This class bridges tree fundamentals with their memory representations and traversal algorithms. It recaps binary tree terminology, then contrasts linked representation (bi-directional linked list with info/previous/next fields, a start pointer, and an avail list) against sequential representation (array-based, using the k, 2k, 2k+1 indexing strategy). The three depth-first traversals — pre-order, in-order, post-order — are defined and worked through on several trees, and the class shows how to reconstruct a tree from pre-order + in-order or post-order + in-order. It closes by discussing sparse vs. complete trees, skewed (unbalanced but valid) trees, and the resulting search complexity of order n vs. log n. This sets up the upcoming topic of binary search trees.

Learning outcomes

  • Explain the difference between logical organization (data structures) and physical organization (arrays/linked lists) of memory.
  • Describe the linked representation of a binary tree, including the roles of info, root, avail, and the avail list.
  • Apply the sequential representation formulas for both 1-based and 0-based indexing, and choose between linked and sequential representation based on tree shape.
  • Perform pre-order, in-order, and post-order traversals on a given tree.
  • Reconstruct a binary tree from pre-order + in-order and from post-order + in-order.
  • Explain why in-order is mandatory for reconstruction and why a skewed tree is valid but unbalanced.
  • State the search complexity for balanced vs. unbalanced trees.
  • Relate node count to edge count, and height conventions (edges vs. nodes) to node-count bounds.

Core concepts

Data structures as logical organization

A data structure is a logical organization where rules are imposed so that insertion, deletion, traversal, and searching can be done efficiently. It is not a physical rearrangement: physical memory is 0 to 123 with no jumping, and under the hood it is always a form of array or a form of linked list — "no third form." Stack, queue, tree, graph, and disjoint set are all logical arrangements. This matters because it explains why a non-linear structure like a tree is still stored in linear memory.

Tree terminology: edges and height

A tree is connected and acyclic; every node except the root has exactly one incoming edge, so a tree with $n$ nodes has $n - 1$ edges. The number of edges on the longest path from the root to a leaf is called the height. If height is counted in nodes instead of edges, the same tree has height $h + 1$ — always check the convention, since height-based bounds depend on it.

Linked representation of a binary tree

Done primarily with a bi-directional linked list: the middle part holds the info; the fields are called previous (address of the left child) and next (address of the right child). The first node is a specialized/initial node with a special pointer called the start pointer. Nodes with no left/right child have null pointers (leaf nodes). The variables involved are info, root, and avail; root and avail need to be initialized to start forming the representation. avail is complementary — you can skip it and just have root information.

Avail list

The avail list indicates the starting pointer for the empty locations in memory. When an operation releases empty space (possible in a linked list, not in an array), it is added on top of the available list. The avail pointer/empty spaces are linked in a one-way list: the info part is null and there is a pointer to the next — an aggregate of all empty space in memory. When memory is needed, we delete from the top and give it out; when memory is freed, we add it on top, so insertion and deletion are order of 1. It is a single address pointer, always unidirectional (not bi-directional), and separate in purpose from the left/right fields. Behind the avail there is always a stack. The fact that the avail list started at 8 indicates that the last deletion happened at location 8.

Sequential representation

The root is stored at the first location (index 1). If index 0 is used, the formulas change accordingly (just reduce). Null is used to indicate the empty tree. When the tree is complete or near to complete, prefer sequential representation because of easier implementation; when the tree is sparse (not enough nodes but having enough height), choose the linked representation.

Complete binary trees

A complete binary tree is one in which every level except possibly the last is completely filled, and the last level is filled from left to right. This is distinct from a perfect tree (all levels completely filled) and from a full/proper tree (every node has 0 or 2 children).

Traversal vs. searching

Traversal is the mechanism of visiting all the nodes exactly once; it is an extended form of searching but better kept separate. Searching is the operation of identifying a specified (dedicated) key. In post-order and in-order, the root is reached first but the print (info) operation is not engaged until the policy is met — the root is only redirected through. Visiting/traversing means performing the print operation, not the redirection decision. Implementation is stack-based: root and left child are pushed onto the stack and the top element is always processed, so the top element will be the left side.

The three traversals

  • Pre-order: root, left, right. "Pre" means the root should come first. Process the root, traverse the left in pre-order, traverse the right in pre-order; this decision is taken at every node.
  • In-order: left, root, right. "In" means the root should appear in between.
  • Post-order: left, right, root. "Post" means the root should appear after left and right.

Key observations: in pre-order the first node is always the root; in post-order the last node is always the root; in in-order the root appears in between the left and right chain. For a binary search tree with distinct keys, the in-order traversal produces the keys in ascending order.

Tree reconstruction

To get back the tree, two combinations are possible — pre-order + in-order, or post-order + in-order. In-order is mandatory: it is used to identify the structure of the tree, while pre-order or post-order is used to identify the root. With pre-order + in-order the tree is generated from left to right; with post-order + in-order it is generated from right to left.

Sparse trees and skewed trees

A sparse tree: for any given subtree, if there is a null pointer or no child in that subtree, it is called sparse — most nodes do not have both left and right children, or have only one child. A skewed tree (e.g., skewed toward the right) is still a valid tree — not a "wrong" tree; it is simply unbalanced. The correct terminology is balanced vs. unbalanced, not right vs. wrong. In a skewed/unbalanced tree the complexity of searching may reach the order of n; if perfectly balanced it reaches log of n.

Deletion in trees

Unlike a linked list (where you join the previous node to the next, skipping the deleted node), in a tree you do not immediately remove the node — its left or right child takes over its position and pointers are adjusted; there are different mechanisms (replace by in-order successor, pre-order successor, etc.). Deletion is a replacement, so there is no change in the links. Insertion and deletion for every tree will be covered later.

Complete binary trees and traversal questions

If a complete binary tree is given, you are not going to modify any structure of the tree — you just do the traversal (pre-order, post-order, in-order). Converting an existing (partial) tree into a complete tree is not suggested, because you would have to do release and insert operations; it cannot simply be shifted.

Formulas and maths

  • Parent at $k$, left child at $2k$, right child at $2k + 1$ — 1-based sequential representation indexing; used to place/retrieve nodes in an array when the root is at index 1.
  • Root at $0$, left child at $2k + 1$, right child at $2k + 2$ — 0-based sequential representation indexing; used when the array starts at index 0.
  • A tree with depth $d$ requires $2^{d}$ elements — sequential representation space requirement; used to size the array for a tree of given depth.
  • Level: $\log n + 1$ — number of levels for $n$ elements; the slide appears to show $\log 2 n + 1$ without brackets, likely a bracket issue (to be checked); effectively the log of $(n+1)$ together.
  • Number of nodes at depth $d$: $2^{d+1}$ — this much memory will be engaged although you will not occupy it; one or two may be used by a later program.
  • Search complexity: order of $n$ for an unbalanced tree; $\log n$ if perfectly balanced — used to compare search cost across tree shapes.
  • Edges in a tree with $n$ nodes: $n - 1$ — every node except the root has exactly one incoming edge.
  • Maximum nodes in a binary tree of height $h$ (height in edges): $2^{h+1} - 1$ — levels $0..h$ each hold $2^{\text{level}}$ nodes, and the geometric sum gives $2^{h+1} - 1$; check $h = 0$: $2^{1} - 1 = 1$.
  • Minimum height (in edges) for $n$ nodes: $\lceil \log_2(n+1) \rceil - 1$ — the smallest $h$ whose perfect tree holds at least $n$ nodes.

Worked examples

Sequential representation example: storing a tree with 9 nodes in an array required 14 locations (nodes placed at positions 1, 2, 3, 4, 5, 7, 12, 13, 14 with gaps at 6, 8, 9, 10, 11). If null entries for successors of terminal nodes are included, 29 spaces would actually be needed, while only 9 entries are engaged. For millions of entities, large amounts of space remain unoccupied because of the sparseness of the tree.

Traversal example 1 (tree with nodes A, B, C, D, E, F): - Pre-order: A, B, D, E, C, F - In-order: D, B, E, A, C, F - Post-order: D, E, B, F, C, A

Traversal example 2 (larger tree with nodes A–L): - Pre-order: A, B, D, E, F, C, G, H, J, L, K - In-order: D, B, F, E, A, G, C, L, J, H, K - Post-order: D, F, E, B, G, L, J, K, H, C, A

Traversal example 3 (small tree): pre-order A, B, D, E, C; in-order D, B, E, A, C; post-order D, E, B, C, A.

Corrected pre-order example (marked wrong in class): pre-order is 1, 2, 4, 7, 9, 5, 3, 6, 8 — visit the root first, then the left (which is also a root), and so on; the right part is 3, 6, 8.

Reconstruction from pre-order + in-order: - Pre-order: 1, 2, 4, 7, 9, 5, 3, 6, 8 - In-order: 7, 9, 4, 2, 5, 1, 3, 6, 8 - First node of pre-order (1) is the root; in in-order, 7, 9, 4, 2, 5 lie on the left and 3, 6, 8 on the right. Next root is 2 (left part 7, 9, 4; right part 5). Next root is 4 (left 7, 9; no right child since those nodes are already visited). Next root is 7 (no left child; right child 9). Then 9, then 5 (both sides already traversed). Then 3, with 6 and 8 as right child; then 6 with 8 as right child. The original tree is recovered.

Reconstruction from post-order + in-order: - Post-order: 9, 7, 4, 5, 2, 8, 6, 3, 1 - In-order: 7, 9, 4, 2, 5, 1, 3, 6, 8 - The root lies last in post-order: 1 (left part 7, 9, 4, 2, 5; right part 3, 6, 8). Next root is 3 (left already covered, right is 6, 8). Next is 6, then 8, then 2 (left 7, 9, 4; right 5). Next is 5 (both ends covered), then 4 (left children 7 and 9), and so on.

Storing sparse trees: instead of following k, 2k, 2k + 1, store pre-order and in-order in two different arrays (9 elements in locations 1–9) and retrieve the tree whenever required — useful when the tree is very sparse.

Complete binary tree example (built for traversal): insert 1, 3, 4, 7, 8, 9, 10, 11, 12, 14, 15, and 16. Pre-order of this complete tree: 1, 3, 7, 11, 12, 8, 14, 15, 4, 9, 16, 10.

Partial tree variant: remove one branch and make 16 the right one; converting such a partial tree into a complete tree is not suggested (requires release/insert operations; cannot be shifted).

Sequential storage walkthrough (k, 2k, 2k+1): location 1 = root; 2 and 3 = its children; location 4 = a node, 5 = a node, 6 = no node available (null). For 77: left field is null, right field has a pointer, so location 6 is skipped and 90 is stored at the seventh location. For 11: left field is null, so the eighth location is empty; 15 is stored at 9, 25 at 10; 11 is again empty; 12 and 13 empty; 14 is filled; 15 empty, and so on.

15 vs. 29: for the tree with root 29 (slide 13, "item with t of 29"), the count is 29 if all successors including empty left/right children (null pointers) are considered; it is 15 if null pointers are not considered. In an MCQ this will be explicitly stated ("consider the null pointer or not"). In terms of memory we usually also hold the information of what the last part is / what is null, so 29 is the usual answer.

Depth example: depth is 3 (1, 2, 3); edges: 47–77, 77–90, 90–15 — 3 edges. Depth counts only the edges (excluding the root).

Height bound example: a binary tree with 12 nodes has minimum height 3 in edges — a perfect tree of height 3 holds $2^{4} - 1 = 15 \ge 12$ nodes, while height 2 holds only $2^{3} - 1 = 7 < 12$; the formula $\lceil \log_2(12+1) \rceil - 1 = 3$ agrees.

BST insertion example: insert 50, 30, 70, 20, 40, 60, 80 into an empty BST; the post-order traversal is 20, 40, 30, 60, 80, 70, 50 (left subtree, right subtree, then root).

Avail list example: top is 8 (no info part), next available space at 9, then 4, then 14, then 15, then 16, and so on. If you want 10 spaces it provides 10 locations, not consecutive — here and there but linked together; without linking you would have to explicitly search all empty spaces. If a fourth memory index is freed after others, the fourth will be used first (stack behavior).

Questions asked in class

  • Q (Mohit): asked for moderator privileges / mic access. A: The moderator was added and mic access enabled.
  • Q: In the post-order reconstruction, how did the transition happen from one root to the next (e.g., from 1 to 3)? A: Roots are picked from the post-order because the last node is always the root — first 1, then 3. The position of 3 in in-order says what is on the left and right, but since 1 is already traversed, the left child is not considered; only the right chain (6 and 8) is considered. If a node is already traversed/covered, movement in that direction stops.
  • Q: How to confirm that the reconstructed tree covers maximum of the tree / is there a way to know you get the same tree? A: You get exactly the same tree as the one from which the post-order and in-order were calculated; there is no difference, no loss of information, if you follow the same process with exact information.
  • Q: The right-most part of the pre-order (1, 3, 6, 8) looks like a skewed/invalid tree — is it invalid? A: It is a skewed tree (skewed toward the right) with imbalances, but it is a valid tree — not a "wrong" tree. It is not a complete tree; it is sparse. It is not memory-efficient to store in an array, but it is a tree. Searching complexity may reach order of n for such a tree; log of n if perfectly balanced.
  • Q: What use cases exist for this? A: The use case will be discussed later; the point now is that blank spaces will be there if you try to push a sparse tree into memory.
  • Q: If a tree is a non-linear data structure, but we store data points in linear data structures like arrays or linked lists, what is the benefit? A: Memory is best represented as an array (from 1 to 1023 in the earlier example); storing elements in an array mirrors how memory is available. These data structures are the logical organization, not the physical organization.
  • Q: What does sparse tree mean? A: For any given subtree, if there is a null pointer or no child in that subtree, it is called sparse — most nodes do not have both left and right children, or have only one child.
  • Q: Do info, root, avail need initialization? A: Yes; you need some root and avail to start forming the representation; avail can be skipped and you can start with just root information.
  • Q: Post-order/in-order: how can we traverse without visiting the root, since we only have the pointer to the root? A: You do reach the root, but the print function is not engaged until there is a child on the left/right; you go down to the leftmost part, and if there is no child you initiate the print statement. In pre-order you print the root first, then check left and right children, print them, and so on. So we visit the root node but don't look into its info part unless the criterion of the traversal type is met.
  • Q: Doesn't this violate "every node visited exactly once," since in post-order we come back to it? A: The instructor agreed the student had a point: in post-order and in-order there is just redirection as per the policy, and the print statement happens only when the policy is met; "visiting" means performing the print operation, not the redirection decision. Analogy given: visiting someone's house and only being redirected to a location without any operation being performed on the person.
  • Q: Slide 7: log n + 1 calculation — A: for a complete binary tree the number of elements is always odd; the formula is effectively log of (n + 1) together; the slide's log 2 n + 1 without brackets is likely a bracket issue, to be checked.
  • Q: Depth formula 2^(d+1) gives an even number while elements are odd — is that okay? A: Yes: that much memory is engaged although not all occupied; one or two may be used by a later program.
  • Q: Real-life scenario for an element with no parent? A: Not possible — linking must be there

Glossary

  • Edge: a link between a parent and a child; a tree with $n$ nodes has $n - 1$ edges.
  • Height: the number of edges on the longest path from the root to a leaf; counted in nodes it is $h + 1$.
  • Complete binary tree: every level except possibly the last is completely filled, and the last level is filled from left to right.
  • Perfect tree: all levels completely filled; holds $2^{h+1} - 1$ nodes at height $h$.
  • Full/proper tree: every node has either 0 or 2 children.
  • In-order traversal: left, root, right; on a BST with distinct keys it yields the keys in ascending order.