What is the main structural difference between a graph and a tree?
A tree is a special type of graph that has no cycles and exactly one path between any two nodes, while a graph can have cycles and multiple paths between nodes.
Click to reveal answer
beginner
Can a tree have cycles? Why or why not?
No, a tree cannot have cycles because it must have exactly one path between any two nodes, ensuring no loops exist.
Click to reveal answer
beginner
Is it possible for a graph to be disconnected? Explain.
Yes, a graph can be disconnected, meaning some nodes are not reachable from others. A tree, however, is always connected.
Click to reveal answer
beginner
What does it mean for a tree to be 'rooted'?
A rooted tree has one special node called the root, from which all other nodes descend. This gives a clear parent-child hierarchy.
Click to reveal answer
beginner
How do edges differ in a tree compared to a general graph?
In a tree with n nodes, there are exactly n-1 edges with no cycles, while a graph can have any number of edges and may contain cycles.
Click to reveal answer
Which of the following is true about trees?
AThey have no cycles and are connected.
BThey can have cycles and be disconnected.
CThey always have multiple paths between nodes.
DThey have more edges than nodes.
✗ Incorrect
Trees have no cycles and are always connected with exactly one path between any two nodes.
What is a key difference between a graph and a tree?
AGraphs cannot have cycles, trees can.
BTrees have exactly one path between nodes, graphs may have multiple.
CGraphs always have fewer edges than nodes.
DTrees can be disconnected.
✗ Incorrect
Trees have exactly one path between any two nodes, while graphs can have multiple paths and cycles.
Can a graph be disconnected?
AOnly trees can be disconnected.
BNo, graphs are always connected.
CYes, graphs can be disconnected.
DGraphs must have cycles to be disconnected.
✗ Incorrect
Graphs can be disconnected, meaning some nodes cannot be reached from others.
How many edges does a tree with 5 nodes have?
A4
B5
C6
D3
✗ Incorrect
A tree with n nodes always has n-1 edges, so 5 nodes have 4 edges.
What is a 'root' in a tree?
AA node that forms a cycle.
BA node with no children.
CA node connected to all other nodes.
DA special node from which all others descend.
✗ Incorrect
The root is the starting node of a tree, from which all other nodes descend.
Explain the key structural differences between a graph and a tree.
Think about cycles, connectivity, and number of edges.
You got /5 concepts.
Describe why a tree is considered a special type of graph.
Focus on paths and cycles.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a key structural difference between a graph and a tree?
easy
A. A tree has no cycles, but a graph can have cycles.
B. A graph always has a root node, but a tree does not.
C. A tree can have multiple edges between the same nodes, but a graph cannot.
D. A graph is always directed, but a tree is always undirected.
Solution
Step 1: Understand cycle presence in trees and graphs
Trees are acyclic by definition, meaning they do not contain any cycles. Graphs can have cycles or be acyclic.
Step 2: Analyze other options for correctness
Graphs do not always have a root node; trees do have a root. Multiple edges can exist in graphs (multigraphs), but trees do not have multiple edges between the same nodes. Both graphs and trees can be directed or undirected.
Final Answer:
A tree has no cycles, but a graph can have cycles. -> Option A
Quick Check:
Cycle presence = Tree no, Graph yes [OK]
Hint: Trees never have cycles; graphs can have cycles [OK]
Common Mistakes:
Thinking graphs always have a root node
Believing trees can have cycles
Confusing directionality rules
2. Which Rust code snippet correctly declares a tree node struct with a parent and children?
easy
A. struct TreeNode { parent: Option<TreeNode>, children: Vec }
B. struct TreeNode { parent: TreeNode, children: Vec<TreeNode> }
C. struct TreeNode { parent: Box<TreeNode>, children: Vec<TreeNode> }
D. struct TreeNode { parent: Option, children: Vec }
Solution
Step 1: Understand ownership and optional parent in Rust
Parent can be None (root node), so Option is needed. Box is used for heap allocation to avoid recursive size issues.
Step 2: Check children vector type
Children are multiple nodes, so Vec is correct to hold multiple heap-allocated nodes.
5. You want to represent a directed acyclic graph (DAG) in Rust and ensure it has no cycles. Which approach best enforces the key structural difference from a tree?
hard
A. Use a tree struct with parent pointers and children vectors, disallowing cycles by design.
B. Use a graph adjacency list with cycle detection logic during edge insertion.
C. Use a graph with multiple edges allowed and no cycle checks.
D. Use a tree struct but allow cycles by linking nodes arbitrarily.
Solution
Step 1: Understand DAG vs Tree structure
DAGs allow multiple parents but no cycles. Trees have exactly one parent per node except root.
Step 2: Evaluate approaches for cycle prevention
Using adjacency list with cycle detection on edge insertion ensures no cycles, fitting DAG definition. Trees disallow multiple parents, so not suitable for DAGs.
Final Answer:
Use a graph adjacency list with cycle detection logic during edge insertion. -> Option B
Quick Check:
DAG = graph + cycle check [OK]
Hint: Detect cycles on insert to keep DAG acyclic [OK]
Common Mistakes:
Confusing trees with DAGs
Ignoring cycle detection in graphs
Allowing cycles in supposed DAG
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.