5101 - Design & Analysis of Algo
← 5101 - Design & Analysis of Algo
session: "5101-design-analysis-of-algo/2026-10-11 0000 DAA 11 Oct"
title: "DAA 11 Oct"
date: "2026-10-11 00:00"
duration: "1h 31m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 49829
generated: "2026-11-23T23:54:51+05:30"
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.
info, root, avail, and the avail list.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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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):
Traversal example 2 (larger tree with nodes A–L):
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:
Reconstruction from post-order + in-order:
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).
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).
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.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.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.
One line per segment, [hh:mm:ss] timestamp first.
loading…