Skip to main content

GATE CSE 2021Data Structures › Trees & Binary Search TreesVerified

GATE CSE 2021 PYQ: Consider a complete binary tree with 7 7 7 nodes. Let A A A denote the set of first 3 3 3 elements obtained…

Consider a complete binary tree with 7 nodes. Let AA denote the set of first 3 elements obtained by performing Breadth-First Search (BFS)\text{(BFS)} starting from the root. Let BB denote the set of first 3 elements obtained by performing Depth-First Search (DFS)\text{(DFS)} starting from the root.

The value of ∣A−B∣\mid A-B \mid is _____________

    Answer

    1

    Approach

    Model the complete binary tree with 7 nodes: it is a perfect binary tree with levels of sizes 1, 2, and 4. Compare the first three nodes produced by BFS (level-by-level) versus DFS (path-oriented), then compute the set difference.

    BFS's first three nodes contain both level-2 nodes, while DFS's first three nodes contain only one level-2 node. Removing the common elements leaves exactly one level-2 node, so the cardinality is 1.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.