What is a key limitation of trees that graphs overcome?
Trees cannot model relationships where nodes have multiple parents or cycles. Graphs can represent these complex connections.
Click to reveal answer
beginner
Why do graphs exist in data structures?
Graphs exist to represent complex relationships like networks, where connections can be many-to-many and cycles can occur, which trees cannot model.
Click to reveal answer
intermediate
Explain with an example a situation where a tree structure fails but a graph works.
In a social network, a person can have many friends (multiple parents), and friendships can form cycles. Trees cannot represent this, but graphs can.
Click to reveal answer
beginner
What does it mean when we say trees are 'acyclic' and graphs can have 'cycles'?
Trees have no loops; you cannot start at one node and return to it by following edges. Graphs can have cycles, meaning loops are allowed.
Click to reveal answer
intermediate
Can a tree be considered a special type of graph? Why or why not?
Yes, a tree is a special kind of graph that is connected and has no cycles. But not all graphs are trees because graphs can have cycles and multiple connections.
Click to reveal answer
Which structure can represent multiple parents for a single node?
ALinked List
BTree
CGraph
DArray
✗ Incorrect
Graphs allow nodes to have multiple parents or connections; trees do not.
What is a key feature that trees do NOT have but graphs can have?
ANodes
BCycles
CEdges
DRoot node
✗ Incorrect
Trees are acyclic, meaning they cannot have cycles; graphs can have cycles.
Why can't trees model social networks well?
ABecause social networks have cycles and multiple connections
BBecause trees are too big
CBecause trees have no nodes
DBecause trees are unordered
✗ Incorrect
Social networks have complex connections and cycles, which trees cannot represent.
Which of these is true about trees?
AThey have exactly one path between any two nodes
BThey can have cycles
CNodes can have multiple parents
DThey are always disconnected
✗ Incorrect
Trees have exactly one path between any two nodes because they are connected and acyclic.
Graphs are used when:
AData is linear
BData is strictly hierarchical
COnly one parent per node is allowed
DRelationships are complex with cycles and multiple connections
✗ Incorrect
Graphs model complex relationships including cycles and multiple connections.
Explain why trees cannot model certain real-world relationships and how graphs solve this problem.
Think about social networks or road maps.
You got /4 concepts.
Describe the main differences between trees and graphs in terms of structure and use cases.
Consider hierarchy vs network.
You got /4 concepts.
Practice
(1/5)
1. Why do we need graphs when we already have trees in data structures?
easy
A. Because trees are faster to traverse than graphs.
B. Because graphs can represent connections between nodes in any pattern, including cycles and multiple links.
C. Because trees can only store numbers, not connections.
D. Because graphs use less memory than trees.
Solution
Step 1: Understand tree limitations
Trees are hierarchical structures with no cycles and only one path between nodes.
Step 2: Understand graph capabilities
Graphs can represent any connections including cycles, multiple edges, and complex relationships.
Final Answer:
Because graphs can represent connections between nodes in any pattern, including cycles and multiple links. -> Option B
Quick Check:
Graphs model complex connections, trees cannot [OK]
Hint: Graphs allow cycles and multiple links; trees do not [OK]
Common Mistakes:
Thinking trees can have cycles
Believing graphs use less memory always
Confusing data storage with structure
2. Which of the following is the correct syntax to declare an adjacency list for a graph in C?
easy
A. int graph(5,5);
B. int graph[5][5];
C. int graph[] = {5};
D. int* graph[5];
Solution
Step 1: Understand adjacency list representation
An adjacency list uses an array of pointers to lists (or arrays) of neighbors.
Step 2: Match syntax to adjacency list
"int* graph[5];" declares an array of 5 pointers to int, suitable for adjacency lists.
Final Answer:
int* graph[5]; -> Option D
Quick Check:
Adjacency list uses array of pointers [OK]
Hint: Adjacency list uses array of pointers in C [OK]
Common Mistakes:
Using 2D array for adjacency list (it's adjacency matrix)
Incorrect array declaration syntax
Confusing arrays with function calls
3. Consider the following C code snippet representing a graph adjacency matrix. What will be the output?
Positions with 1 indicate edges: (0,1), (1,0), (1,2), (2,1).
Step 2: Trace nested loops printing edges
Loops print edges where graph[i][j] == 1, so output lines are 0->1, 1->0, 1->2, 2->1.
Final Answer:
0 -> 1
1 -> 0
1 -> 2
2 -> 1 -> Option C
Quick Check:
Edges printed match adjacency matrix 1s [OK]
Hint: Print edges where adjacency matrix value is 1 [OK]
Common Mistakes:
Ignoring symmetric edges
Printing zeros as edges
Confusing indices i and j
4. The following code attempts to create a graph with cycles using a tree structure. What is the main issue?
struct Node {
int val;
struct Node* left;
struct Node* right;
};
// Trying to link nodes to form a cycle
node1->left = node2;
node2->left = node3;
node3->left = node1; // cycle created here
medium
A. Trees cannot have cycles, so this creates an invalid structure.
B. The pointers are not initialized properly causing segmentation fault.
C. The code will compile but not run because of missing return statements.
D. There is no issue; trees can have cycles.
Solution
Step 1: Understand tree structure rules
Trees must not have cycles; each node has exactly one parent except root.
Step 2: Identify cycle creation in code
Linking node3 back to node1 creates a cycle, violating tree properties.
Final Answer:
Trees cannot have cycles, so this creates an invalid structure. -> Option A
Quick Check:
Cycle in tree structure is invalid [OK]
Hint: Trees cannot contain cycles; cycles break tree rules [OK]
Common Mistakes:
Thinking cycles are allowed in trees
Confusing compile errors with logical errors
Ignoring pointer initialization
5. You want to model a social network where users can be friends with multiple users, including mutual friendships and groups. Which data structure best models this, and why can't a tree be used here?
hard
A. Graph, because it supports cycles and multiple connections between nodes.
B. Tree, because it is simpler and faster for all connections.
C. Array, because it stores all users in order.
D. Linked list, because it can represent multiple friendships easily.
Solution
Step 1: Identify social network connection needs
Users can have multiple friends, mutual connections, and cycles (friend loops).
Step 2: Match data structure capabilities
Graphs support cycles and multiple edges; trees cannot have cycles or multiple parents.
Final Answer:
Graph, because it supports cycles and multiple connections between nodes. -> Option A
Quick Check:
Social networks need graphs, not trees [OK]
Hint: Use graphs for mutual and multiple connections [OK]
Common Mistakes:
Choosing tree for cyclic relationships
Confusing arrays or lists with graph structures
Ignoring mutual friendship cycles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.