📖 Scenario: Imagine you are organizing a family reunion and a city map. The family reunion is like a tree structure where each person has one parent, but the city map is like a graph where places connect in many ways.
🎯 Goal: You will create simple data structures to represent a tree and a graph, then check the key difference: whether nodes have multiple parents or cycles.
📋 What You'll Learn
Create a tree structure using an adjacency list with no cycles
Create a graph structure using an adjacency list that can have cycles
Add a variable to track if the graph has cycles
Write a simple function to detect if the graph has cycles
Print the adjacency lists and cycle detection result
💡 Why This Matters
🌍 Real World
Trees are used in family trees, file systems, and organizational charts. Graphs are used in maps, social networks, and web links.
💼 Career
Understanding the difference between trees and graphs helps in software development, data analysis, and solving problems involving networks and hierarchies.
Progress0 / 4 steps
1
Create a tree adjacency list
Create a variable called tree as an object representing a tree adjacency list with these exact entries: 1: [2, 3], 2: [4], 3: [], 4: []
DSA Typescript
Hint
Use an object where keys are node numbers and values are arrays of child nodes.
2
Create a graph adjacency list
Create a variable called graph as an object representing a graph adjacency list with these exact entries: 1: [2, 3], 2: [3], 3: [1]
DSA Typescript
Hint
Graph can have cycles, so node 3 connects back to node 1.
3
Add cycle detection variable and function
Create a variable called hasCycle and set it to false. Then write a function called detectCycle that takes graph and returns true if a cycle exists, otherwise false. For simplicity, return true if node 3 connects back to node 1, else false.
DSA Typescript
Hint
Check if node 3's adjacency list includes node 1 to detect a cycle.
4
Print adjacency lists and cycle detection result
Write console.log statements to print the tree adjacency list, the graph adjacency list, and the hasCycle variable.
DSA Typescript
Hint
Use console.log to print each variable with a label.
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 can have multiple edges between the same nodes, but a graph cannot.
D. A tree has no cycles, but a graph can have cycles.
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.
Step 2: Compare cycle allowance in graphs
Graphs can have cycles, meaning they can contain loops where you can start at a node and return to it by following edges.
Final Answer:
A tree has no cycles, but a graph can have cycles. -> Option D
Quick Check:
Tree no cycles, graph can have cycles = D [OK]
Hint: Trees never loop back; graphs can have loops [OK]
Common Mistakes:
Thinking graphs must have a root node
Believing trees can have cycles
Confusing edge multiplicity rules
2. Which of the following TypeScript declarations correctly represents a tree node structure?
easy
A. interface Node { value: number; neighbors: Node[]; }
B. interface Node { value: number; parent: Node[]; }
C. interface Node { value: number; children: Node[]; }
D. interface Node { value: number; edges: number[]; }
Solution
Step 1: Identify tree node properties
A tree node typically has a value and a list of children nodes, representing a hierarchical structure.
Step 2: Match the correct interface
interface Node { value: number; children: Node[]; } uses children: Node[], which correctly models tree children. Other options use neighbors or parent arrays, which are more graph-like or incorrect.