CS301-lec36.ppt

Verified academic study resource for CS301 (Data Structures) under the Computer Science at Virtual University of Pakistan. This PPT document (Official Lecture Handouts & Highlighted Notes) covers Complete Course Handouts (Lectures 1 to 45) spanning approximately 450 pages across 8 curriculum modules, and is indexed for student examination review with instant in-browser preview and 100% free direct download.

Course
CS301 – Data Structures
Faculty
Computer Science
Format
PPT
Type
Handout
Coverage
Complete Course Handouts (Lectures 1 to 45)
Tags
SLIDES

Virtual University examiners construct 100% of objective MCQs and subjective marking schemes directly from the official CS301 handouts.

What this file covers — CS301 syllabus roadmap

  1. Core Architecture, Syntax & Primitive Data Structures — Lectures 1 to 6 (Pages 1 to 65). 15% Objective Marks (8–10 MCQs)
  2. Conditional Branching, Loops & Logical Control Flow — Lectures 7 to 12 (Pages 66 to 140). 18% Exam Marks (MCQs + 2-Mark Short Questions)
  3. Memory Management, Pointers, Arrays & Functions — Lectures 13 to 18 (Pages 141 to 215). 22% Exam Marks (High Yield for Midterms)
  4. Midterm Consolidation, Strings & Algorithm Synthesis — Lectures 19 to 22 (Pages 216 to 280). Midterm Exam Culmination (Lectures 1–22 Scope)
  5. Object-Oriented Paradigms: Classes & Data Abstraction — Lectures 23 to 29 (Pages 281 to 360). 16% Final-Term Marks (Core OO Concepts)
  6. Inheritance Hierarchies & Dynamic Polymorphism — Lectures 30 to 36 (Pages 361 to 435). 20% Final-Term Marks (Virtual Functions & V-Tables)
  7. Templates, Standard Library & Persistent File I/O — Lectures 37 to 41 (Pages 436 to 510). 14% Final-Term Marks (Generic Code & Streams)
  8. Final-Term Examination Dossier & Capstone Synthesis — Lectures 42 to 45 (Pages 511 to 585). Final-Term Exam Synthesis (Lectures 23–45 Scope)

Frequently asked questions

Where can I download CS301-lec36.ppt for free?

You can preview and download "CS301-lec36.ppt" (CS301 PPT, ~450 pages) directly from the MIHORA VU Study Library (https://study.mihora.tech/doc/cs301-lec36-ppt) with 100% free high-speed downloads and zero registration requirements.

What syllabus chapters and lectures are covered in this CS301 file?

This document covers Complete Course Handouts (Lectures 1 to 45) across 8 distinct lecture units (Lectures 1 to 6, Lectures 7 to 12, Lectures 13 to 18, Lectures 19 to 22, Lectures 23 to 29, Lectures 30 to 36, Lectures 37 to 41, Lectures 42 to 45), featuring comprehensive definitions, solved objective questions, and examiner marking rubrics.

What is the passing criteria for CS301 under the VU 20% Two-Half Rule?

Under official Virtual University regulations, students must score at least 20% in the First Half (Quizzes, Assignments, GDBs, and Midterm combined) AND independently at least 20% in the Final-Term examination paper. Scoring under 20% in the final exam results in an automatic F grade regardless of earlier marks.

Are CS301 solved past papers and handouts verified for 2026 exams?

Yes. All CS301 materials on MIHORA VU—including Moaaz, Waqar Siddhu, and team archives—are reviewed against the current VU syllabus to help students prepare for 2026 Midterm, Final-Term, and online quiz examinations.

Inside this file

Running Time Analysis

   Union is clearly a constant time operation.
   Running time of find(i) is proportional to
    the height of the tree containing node i.
   This can be proportional to n in the worst
    case (but not always)
   Goal: Modify union to ensure that heights
    stay small

Lecture No.36

Data Structures

 Dr. Sohail Aslam

Union by Size

   Maintain sizes (number of nodes) of all
    trees, and during union.
   Make smaller tree the subtree of the larger
    one.
   Implementation: for each root node i,
    instead of setting parent[i] to -1, set it
    to -k if tree rooted at i has k nodes.
   This also called union-by-weight.

Union by Size
union(i,j):
   root1 = find(i);
   root2 = find(j);
   if (root1 != root2)
      if (parent[root1] <= parent[root2])
      {
         // first tree has more nodes
         parent[root1] += parent[root2];
         parent[root2] = root1;
      }
      else { // second tree has more nodes
         parent[root2] += parent[root1];
         parent[root1] = root2;
      }

Union by Size

        1        2          3         4        5         6        7   8

                 -1    -1       -1   -1   -1   -1   -1       -1
                1      2     3     4    5    6      7        8
   Eight elements, initially in different sets.

Union by Size

        1       2         3         4        5             7   8

                                    6

                -1   -1       -1   -2   -1   4   -1   -1
                1    2        3    4    5    6   7    8
   Union(4,6)

Union by Size

        1       2              4        5             7   8

                3              6

                -1   -2   2   -2   -1   4   -1   -1
                1    2    3   4    5    6   7    8
   Union(2,3)

Union by Size

                2                 4        5             7   8

                3        1        6

                4   -2   2       -3   -1   4   -1   -1
                1   2        3   4    5    6   7    8
   Union(1,4)

Union by Size

                                 4        5             7   8

                2       1        6

                3

                4   4   2       -5   -1   4   -1   -1
                1   2       3   4    5    6   7    8
   Union(2,4)

Union by Size

                                 4                         7   8

                2       1        6       5

                3

                4   4   2       -6   4       4   -1   -1
                1   2       3   4    5       6   7    8
   Union(5,4)

Analysis of Union by Size

   If unions are done by weight (size), the depth of
    any element is never greater than log2n.

Analysis of Union by Size

   Intuitive Proof:
       Initially, every element is at depth zero.
       When its depth increases as a result of a
        union operation (it’s in the smaller tree), it is
        placed in a tree that becomes at least twice
        as large as before (union of two equal size
        trees).
       How often can each union be done? -- log2n
        times, because after at most log2n unions,
        the tree will contain all n elements.

Union by Height

   Alternative to union-by-size strategy:
    maintain heights,
   During union, make a tree with smaller
    height a subtree of the other.
   Details are left as an exercise.

Sprucing up Find

  So far we have tried to optimize union.
  Can we optimize find?
  Yes, using path compression (or
   compaction).

Sprucing up Find

   During find(i), as we traverse the path
    from i to root, update parent entries for all
    these nodes to the root.
   This reduces the heights of all these
    nodes.
   Pay now, and reap benefits later!
   Subsequent find may do less work

Sprucing up Find

   Updated code for find

     find (i)
     {
        if (parent[i] < 0)
          return i;
        else
          return parent[i] = find(parent[i]);
     }

Path Compression

  Find(1)                                                 7
                              13
                                             8        3        22        6
                      4             5

             9
                      31 32        11   30                     10
        2
                 35
                                                 20       16        14       12
    1       13

  17 18 19

Path Compression

  Find(1)                                                 7
                              13
                                             8        3        22        6
                      4             5

             9
                      31 32        11   30                     10
        2
                 35
                                                 20       16        14       12
    1       13

  17 18 19

Path Compression

  Find(1)                                                 7
                              13
                                             8        3        22        6
                      4             5

             9
                      31 32        11   30                     10
        2
                 35
                                                 20       16        14       12
    1       13

  17 18 19

Path Compression

  Find(1)                                                 7
                              13
                                             8        3        22        6
                      4             5

             9
                      31 32        11   30                     10
        2
                 35
                                                 20       16        14       12
    1       13

  17 18 19

Path Compression

  Find(1)                                                 7
                              13
                                             8        3        22        6
                      4             5

             9
                      31 32        11   30                     10
        2
                 35

More CS301 files

All CS301 files · Midterm Prep 2026 · MCQs Bank