What is the main structural difference between a graph and a tree?
A tree is a special type of graph that is connected and has no cycles, while a graph can have cycles and may not be connected.
Click to reveal answer
beginner
Can a tree have cycles?
No, a tree cannot have cycles. It is an acyclic connected graph.
Click to reveal answer
beginner
Is it possible for a graph to be disconnected?
Yes, a graph can be disconnected, meaning some nodes are not reachable from others.
Click to reveal answer
intermediate
How many edges does a tree with n nodes have?
A tree with n nodes always has exactly n-1 edges.
Click to reveal answer
intermediate
Can a graph have multiple edges between the same two nodes?
Yes, in a multigraph, multiple edges between the same two nodes are allowed, but trees do not have multiple edges between the same nodes.
Click to reveal answer
Which of the following is true about trees?
ATrees can have cycles
BTrees have no cycles and are connected
CTrees can be disconnected
DTrees always have more edges than nodes
✗ Incorrect
Trees are connected graphs without cycles and have exactly n-1 edges for n nodes.
What is a key difference between a graph and a tree?
AGraphs may have cycles and may be disconnected
BTrees can be disconnected
CGraphs cannot have cycles
DTrees always have cycles
✗ Incorrect
Graphs can have cycles and may be disconnected, unlike trees which are connected and acyclic.
How many edges does a tree with 5 nodes have?
A3
B5
C4
D6
✗ Incorrect
A tree with n nodes has n-1 edges, so 5 nodes means 4 edges.
Can a graph be disconnected?
AYes
BNo
COnly if it is a tree
DOnly if it has cycles
✗ Incorrect
Graphs can be disconnected, meaning some nodes are unreachable from others.
Which structure must be acyclic?
AGraph
BMultigraph
CDirected graph
DTree
✗ Incorrect
Trees are acyclic connected graphs by definition.
Explain the key structural differences between a graph and a tree.
Think about connectivity and cycles.
You got /5 concepts.
Describe why a tree is considered a special type of graph.
Focus on what makes trees unique among graphs.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a key structural difference between a tree and a graph?
easy
A. A graph is always directed, but a tree is always undirected.
B. A graph always has a root node, but a tree does not.
C. A tree has no cycles, but a graph can have cycles.
D. A tree can have multiple edges between the same nodes, but a graph cannot.
Solution
Step 1: Understand the cycle property in trees and graphs
A tree is a special type of graph that does not contain any cycles, meaning no path loops back to the same node. Graphs, however, can have cycles.
Step 2: Analyze the options based on cycle presence
A tree has no cycles, but a graph can have cycles. correctly states that trees have no cycles while graphs can have cycles. Other options incorrectly describe root presence, edge multiplicity, or directionality.
Final Answer:
A tree has no cycles, but a graph can have cycles. -> Option C
Quick Check:
Cycle presence = Tree no, Graph yes [OK]
Hint: Trees never loop back; graphs can have loops [OK]
Common Mistakes:
Thinking graphs always have a root node
Believing trees can have cycles
Confusing edge multiplicity rules
2. Which of the following C# declarations correctly represents a tree node class with a list of children nodes?
easy
A. public class TreeNode { public int Value; public TreeNode Parent; }
B. public class TreeNode { public int Value; public List<TreeNode> Children; }
C. public class TreeNode { public int Value; public TreeNode[] Edges; }
D. public class TreeNode { public int Value; public Dictionary Neighbors; }
Solution
Step 1: Identify the correct structure for tree children
A tree node typically holds a list of its children nodes, so a List<TreeNode> is appropriate.
Step 2: Compare options for children representation
public class TreeNode { public int Value; public List<TreeNode> Children; } uses List<TreeNode> for children, which is correct. public class TreeNode { public int Value; public TreeNode Parent; } only has a parent reference, not children. public class TreeNode { public int Value; public TreeNode[] Edges; } uses an array named Edges, which is ambiguous. public class TreeNode { public int Value; public Dictionary Neighbors; } uses a dictionary for neighbors, typical for graphs, not trees.
Final Answer:
public class TreeNode { public int Value; public List<TreeNode> Children; } -> Option B
Quick Check:
Children list = public class TreeNode { public int Value; public List<TreeNode> Children; } [OK]
Hint: Trees store children in lists, not dictionaries [OK]
Common Mistakes:
Using dictionary for tree children
Confusing parent with children references
Using ambiguous 'Edges' array for tree nodes
3. Consider the following C# code snippet representing a graph and a tree. What will be the output when checking if the graph contains a cycle?
class Node {
public int Value;
public List<Node> Neighbors = new List<Node>();
}
bool HasCycle(Node node, HashSet<Node> visited, Node parent) {
visited.Add(node);
foreach (var neighbor in node.Neighbors) {
if (!visited.Contains(neighbor)) {
if (HasCycle(neighbor, visited, node)) return true;
} else if (neighbor != parent) {
return true;
}
}
return false;
}
// Setup
Node a = new Node { Value = 1 };
Node b = new Node { Value = 2 };
Node c = new Node { Value = 3 };
a.Neighbors.Add(b);
b.Neighbors.Add(a);
b.Neighbors.Add(c);
c.Neighbors.Add(b);
c.Neighbors.Add(a); // extra edge creating a cycle
var visited = new HashSet<Node>();
Console.WriteLine(HasCycle(a, visited, null));
medium
A. true
B. false
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand the graph structure and cycle detection
The graph nodes a, b, c are connected with edges forming a cycle because c links back to a, creating a loop.
Step 2: Trace the HasCycle function call
The function uses DFS and detects a back edge when it finds a neighbor already visited that is not the parent, confirming a cycle exists.
Final Answer:
true -> Option A
Quick Check:
Cycle detected = true [OK]
Hint: Cycle if visited neighbor not parent found [OK]
Common Mistakes:
Ignoring the extra edge creating the cycle
Confusing parent node in cycle check
Assuming no cycles in undirected graphs
4. The following C# code attempts to check if a tree has cycles. Identify the bug:
bool IsTree(Node node, HashSet<Node> visited, Node parent) {
if (visited.Contains(node)) return false;
visited.Add(node);
foreach (var neighbor in node.Neighbors) {
if (neighbor != parent) {
if (!IsTree(neighbor, visited, node)) return false;
}
}
return true;
}
medium
A. Returns false on revisiting a node instead of true for cycle detection.
B. Does not add the current node to visited set.
C. Does not check for null neighbors list.
D. Incorrectly compares neighbor with parent node.
Solution
Step 1: Analyze the cycle detection logic
The function returns false if a node is already visited, but this means it treats revisiting as no cycle, which is incorrect.
Step 2: Identify correct return for cycle detection
When a node is revisited (visited.Contains(node) is true), it should return true to indicate a cycle, not false.
Final Answer:
Returns false on revisiting a node instead of true for cycle detection. -> Option A
Quick Check:
Cycle detection return logic incorrect [OK]
Hint: Cycle found if node revisited, return true [OK]
Common Mistakes:
Returning false instead of true on revisiting
Not adding nodes to visited set
Misusing parent node comparison
5. You have a graph represented by adjacency lists. You want to check if it is a tree. Which combination of conditions must be true for the graph to be a tree?
hard
A. The graph is connected and has cycles.
B. The graph has no cycles and has at least N edges.
C. The graph has exactly N edges and is disconnected.
D. The graph is connected and has exactly (N - 1) edges, where N is the number of nodes.
Solution
Step 1: Recall tree properties in graph terms
A tree is a connected graph with no cycles and exactly N-1 edges for N nodes.
Step 2: Evaluate options against tree properties
The graph is connected and has exactly (N - 1) edges, where N is the number of nodes. correctly states the graph must be connected and have N-1 edges. Other options either allow cycles, wrong edge counts, or disconnected graphs.
Final Answer:
The graph is connected and has exactly (N - 1) edges, where N is the number of nodes. -> Option D
Quick Check:
Connected + edges = N-1 [OK]
Hint: Tree = connected + edges = nodes minus one [OK]
Common Mistakes:
Allowing cycles in trees
Confusing edge count with nodes
Ignoring connectivity requirement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.