chapter

    Hierarchical Data Structures Notes for Cmi M.sc. and Ph.d. Computer Science

    Hierarchical Data Structures notes for CMI M.Sc. and Ph.D. Computer Science: 36 study cards covering concepts, formulas, shortcuts and exam traps, plus solved

    Hierarchical Data Structures notes

    What forces trees to exist

    Intuition

    What forces trees to exist

    A tree models any situation where each item has one parent and no chain of command loops back.

    The problem it solves

    Consider a company where every employee reports to exactly one manager and no manager is their own boss's boss. The reporting lines form a tree. The analogy breaks when an employee has two managers or when reporting loops exist, since trees forbid both.

    Our running example. Take a root r with children a, b, c. Let a have children d, e. Let b have child f. Node c is a leaf. The graph has seven vertices and six edges. It is connected and has no cycle, so it is a tree.

    The idea made exact

    A tree is an undirected graph that is connected and contains no cycle. A graph is connected when a path exists between every pair of vertices. A cycle is a closed walk with no repeated vertex except the start and end.

    Connectivity rules out scattered pieces. Acyclicity rules out loops. Together they force a single hierarchical structure with no redundancy.

    undirected graph connected acyclic

    Equivalent characterizations of trees

    Derivation

    Equivalent characterizations of trees

    For a graph G with n vertices and m edges, the following are equivalent: (T1) G is a tree. (T2) G is connected and . (T3) G is acyclic and . (T4) Every pair of vertices is joined by exactly one simple path.

    Prove (T1) implies (T2) by induction on n.

    assumption: undirected graph method: induction on n
    1. Base case n = 1. A single vertex has m = 0 edges. Then m = 1 - 1 holds. definition of edge count
    2. Leaf existence for n ≥ 2. Since G is connected, every vertex has degree at least one. Take a longest simple path v_0, v_1, ..., v_k. If v_k had a neighbor u not on the path, we would extend the path. If u were on the path, we would form a cycle. Both contradict our choice, so deg(v_k) = 1. maximality of path
    3. Remove the leaf. Delete v_k and its edge. The remaining graph G' has n - 1 vertices and m - 1 edges, and stays connected and acyclic. leaf removal preserves both properties
    4. Apply the hypothesis. By induction m - 1 = (n - 1) - 1, so m = n - 1. inductive step closes
    Result
    Check

    Our seven-node tree has six edges, and 6 = 7 - 1.

    Rooted tree terminology

    Derivation

    Rooted tree terminology

    Fix a root r in a tree T. Direct every edge away from r.

    assumption: tree T with chosen root r
    1. Parent and children. For v ≠ r, its parent p(v) is the unique neighbor on the path to r. The other neighbors of v are its children. unique path to root
    2. Ancestors and descendants. Ancestors of v are v, p(v), p(p(v)), up to r. Descendants of v are v and all vertices reachable from v away from r. directed away from root
    3. Leaves and internal nodes. A leaf has no children. An internal node has at least one child. In our tree, c, d, e, f are leaves; r, a, b are internal. child count
    4. Depth. The depth d(v) is the number of edges on the path from r to v. We set d(r) = 0. In our tree, depths are d(r)=0, d(a)=d(b)=d(c)=1, d(d)=d(e)=d(f)=2. distance from root
    5. Height. The height h(v) is the length of the longest downward path from v to a leaf. We set h(leaf) = 0. In our tree, heights are h(c)=h(d)=h(e)=h(f)=0, h(a)=h(b)=1, h(r)=2. longest downward path
    6. Height of the tree. The height of T equals h(r), which equals the maximum depth over all vertices. definition at root
    Result
    Check

    In our tree, max depth is 2 and h(r) = 2.

    33 more cards in this chapter

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Hierarchical Data Structures Notes for CMI M.Sc. and Ph.D. Computer Science

    Hierarchical Data Structures notes for CMI M.Sc. and Ph.D. Computer Science: 36 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    What forces trees to exist

    Intuition

    What forces trees to exist

    A tree models any situation where each item has one parent and no chain of command loops back.

    The problem it solves

    Consider a company where every employee reports to exactly one manager and no manager is their own boss's boss. The reporting lines form a tree. The analogy breaks when an employee has two managers or when reporting loops exist, since trees forbid both.

    Our running example. Take a root r with children a, b, c. Let a have children d, e. Let b have child f. Node c is a leaf. The graph has seven vertices and six edges. It is connected and has no cycle, so it is a tree.

    The idea made exact

    A tree is an undirected graph that is connected and contains no cycle. A graph is connected when a path exists between every pair of vertices. A cycle is a closed walk with no repeated vertex except the start and end.

    Connectivity rules out scattered pieces. Acyclicity rules out loops. Together they force a single hierarchical structure with no redundancy.

    undirected graph connected acyclic

    Equivalent characterizations of trees

    Derivation

    Equivalent characterizations of trees

    For a graph G with n vertices and m edges, the following are equivalent: (T1) G is a tree. (T2) G is connected and . (T3) G is acyclic and . (T4) Every pair of vertices is joined by exactly one simple path.

    Prove (T1) implies (T2) by induction on n.

    assumption: undirected graph method: induction on n
    1. Base case n = 1. A single vertex has m = 0 edges. Then m = 1 - 1 holds. definition of edge count
    2. Leaf existence for n ≥ 2. Since G is connected, every vertex has degree at least one. Take a longest simple path v_0, v_1, ..., v_k. If v_k had a neighbor u not on the path, we would extend the path. If u were on the path, we would form a cycle. Both contradict our choice, so deg(v_k) = 1. maximality of path
    3. Remove the leaf. Delete v_k and its edge. The remaining graph G' has n - 1 vertices and m - 1 edges, and stays connected and acyclic. leaf removal preserves both properties
    4. Apply the hypothesis. By induction m - 1 = (n - 1) - 1, so m = n - 1. inductive step closes
    Result
    Check

    Our seven-node tree has six edges, and 6 = 7 - 1.

    Rooted tree terminology

    Derivation

    Rooted tree terminology

    Fix a root r in a tree T. Direct every edge away from r.

    assumption: tree T with chosen root r
    1. Parent and children. For v ≠ r, its parent p(v) is the unique neighbor on the path to r. The other neighbors of v are its children. unique path to root
    2. Ancestors and descendants. Ancestors of v are v, p(v), p(p(v)), up to r. Descendants of v are v and all vertices reachable from v away from r. directed away from root
    3. Leaves and internal nodes. A leaf has no children. An internal node has at least one child. In our tree, c, d, e, f are leaves; r, a, b are internal. child count
    4. Depth. The depth d(v) is the number of edges on the path from r to v. We set d(r) = 0. In our tree, depths are d(r)=0, d(a)=d(b)=d(c)=1, d(d)=d(e)=d(f)=2. distance from root
    5. Height. The height h(v) is the length of the longest downward path from v to a leaf. We set h(leaf) = 0. In our tree, heights are h(c)=h(d)=h(e)=h(f)=0, h(a)=h(b)=1, h(r)=2. longest downward path
    6. Height of the tree. The height of T equals h(r), which equals the maximum depth over all vertices. definition at root
    Result
    Check

    In our tree, max depth is 2 and h(r) = 2.

    Acyclic alone does not make a tree

    Wrong belief

    Acyclic alone does not make a tree

    Any graph with no cycle is a tree.

    Why it feels right

    Trees are cycle-free, so acyclicity should suffice.

    Smallest counterexample

    Take vertices {u, v} with no edges. The graph has no cycle, since no closed walk exists at all. But it is not connected: no path joins u to v. Therefore it is not a tree. It is a forest of two one-vertex trees.

    Counter-rule

    A tree must be connected and acyclic. Acyclicity alone yields a forest, which is a disjoint union of trees.

    How it shows up in options

    One option reads "a graph is a tree if and only if it is acyclic". Reject it and choose the option that demands both conditions.

    More notes in this unit