retagged by
11,165 views
14 votes
14 votes
Consider a complete binary tree with $7$ nodes. Let $A$ denote the set of first $3$ elements obtained by performing Breadth-First Search $\text{(BFS)}$ starting from the root. Let $B$ denote the set of first $3$ elements obtained by performing Depth-First Search $\text{(DFS)}$ starting from the root.

The value of $\mid A-B \mid $ is _____________
retagged by

3 Answers

Best answer
15 votes
15 votes

Complete binary tree property has this property that every level until last is fully filled and last level is filled from left to right.

So, when we have a complete binary tree with $7$ nodes,

  • $\text{level} _1$ has $1$ node ( root )
  • $\text{level} _2$ has $2$ nodes.
  • $\text{level} _3$ has $4$ nodes.

BFS goes level by level. So first three elements (say Set A) $=\text{level} _1$ nodes $(1)+ \text{ level} _2(2)$ nodes.

DFS is go by connected manner. So first three elements (say Set B ) $= \text{level} _1$ nodes (1)$+$ one node from $\text{level} _2$ nodes $+$ one node from $\text{level} _3$ nodes which is connected to the previously chosen $\text{level} _2$ node.

$A – B =$ the remaining node from the set of $\text{level} _2$ nodes.

$\implies |A – B| = 1.$

selected by
22 votes
22 votes

The answer is $1$. See the image below.

$A=\{1,2,3\}$

and $B=\{1,2,4\}$ or $B=\{1,2,5\}$ or $B=\{1,3,6\}$ or $B=\{1,3,7\}$

for all cases, $|A-B|=1$

 

 

 

edited by
2 votes
2 votes

Set A will contain = { root node , 2nd level all node i.e 2 node}

Set B = { root node , one node from 2nd level , one node from 3rd level }

when we perform Set Difference operation :

A-B = one node from 2nd level.

and cardinality will be

|A-B| = 1

Answer:

Related questions

14 votes
14 votes
4 answers
1
Arjun asked Feb 18, 2021
8,887 views
​​​​​Let $H$ be a binary min-heap consisting of $n$ elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the...
20 votes
20 votes
6 answers
2
Arjun asked Feb 18, 2021
9,435 views
Consider the following deterministic finite automaton $\text{(DFA)}$The number of strings of length $8$ accepted by the above automaton is ___________
13 votes
13 votes
3 answers
3
Arjun asked Feb 18, 2021
4,864 views
If $x$ and $y$ are two decimal digits and $(0.1101)_2 = (0.8xy5)_{10}$, the decimal value of $x+y$ is ___________