Tree vs Array vs Linked List When Hierarchy Matters
📖 Scenario: Imagine you are organizing a company's employee structure. Each employee can have subordinates, forming a hierarchy. You want to represent this hierarchy in different ways to understand how data structures work when hierarchy matters.
🎯 Goal: You will create three different data structures to represent the same employee hierarchy: an array, a linked list, and a tree. You will then access and print the hierarchy to see how each structure handles relationships.
📋 What You'll Learn
Create an array representing employees with their direct manager's ID
Create a linked list representing employees in order with their manager's ID
Create a tree structure representing employees and their subordinates
Print the hierarchy from each data structure
💡 Why This Matters
🌍 Real World
Companies often need to represent employee hierarchies to manage reporting and workflows.
💼 Career
Understanding how to model hierarchical data structures is essential for software engineers working on organizational tools, file systems, or any nested data.
Use an array of objects with keys id, name, and managerId.
2
Create a linked list of employees with manager IDs
Create a class called EmployeeNode with properties id, name, managerId, and next (which can be null). Then create a constant called employeesLinkedList that links these employees in order: CEO, CTO, CFO, Dev, Accountant.
DSA Typescript
Hint
Create nodes for each employee and link them using the next property.
3
Create a tree structure representing employees and subordinates
Create a class called EmployeeTreeNode with properties id, name, and subordinates (an array of EmployeeTreeNode). Then create a constant called employeeTree representing the hierarchy: CEO has CTO and CFO as subordinates; CTO has Dev; CFO has Accountant.
DSA Typescript
Hint
Use a tree node class with a subordinates array to hold children nodes.
4
Print the hierarchy from each data structure
Print the employee hierarchy from employeesArray, employeesLinkedList, and employeeTree. For employeesArray, print each employee's name and managerId. For employeesLinkedList, traverse from head and print each employee's name and managerId. For employeeTree, write a recursive function printTree that prints each employee's name with indentation showing hierarchy.
DSA Typescript
Hint
Use loops to print arrays and linked lists. Use recursion with indentation to print the tree hierarchy.
Practice
(1/5)
1. Which data structure is best suited to represent hierarchical relationships like a family tree or company organization?
easy
A. Tree
B. Array
C. Linked List
D. Stack
Solution
Step 1: Understand hierarchy representation
Hierarchies have parent-child relationships, which are naturally modeled by trees.
Step 2: Compare data structures
Arrays and linked lists are linear and do not represent branching well, unlike trees.
Final Answer:
Tree -> Option A
Quick Check:
Hierarchy = Tree [OK]
Hint: Hierarchy means branching, so choose Tree [OK]
Common Mistakes:
Choosing Array because it's simple
Choosing Linked List thinking it can represent hierarchy
Confusing Stack with hierarchical structure
2. Which of the following is the correct TypeScript syntax to declare a simple linked list node with a value and a next pointer?
easy
A. class Node { value: number; next: Node; constructor(value: number) { this.value = value; this.next = null; } }
Hint: Next pointer must allow null for list end [OK]
Common Mistakes:
Not allowing null for next pointer
Initializing next as undefined instead of null
Using any type which loses type safety
3. Given the following TypeScript code, what will be the printed output?
class TreeNode {
value: number;
children: TreeNode[];
constructor(value: number) {
this.value = value;
this.children = [];
}
}
const root = new TreeNode(1);
const child1 = new TreeNode(2);
const child2 = new TreeNode(3);
root.children.push(child1, child2);
console.log(root.children.map(c => c.value));
medium
A. [1, 2, 3]
B. Error: children is not iterable
C. [[2], [3]]
D. [2, 3]
Solution
Step 1: Understand tree structure
Root node has value 1 and two children with values 2 and 3.
Step 2: Analyze console.log output
Mapping children array extracts values [2, 3], root's value 1 is not included.
Final Answer:
[2, 3] -> Option D
Quick Check:
Children values = [2, 3] [OK]
Hint: Mapping children prints their values, not root's [OK]
Common Mistakes:
Including root value in output
Expecting nested arrays instead of flat array
Assuming children is not iterable
4. Identify the bug in this TypeScript code that tries to create a linked list and print its values:
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
let current = head;
while (current !== null) {
console.log(current.value);
current = current.next.next;
}
medium
A. Using console.log inside loop is invalid
B. Incorrect constructor syntax
C. Accessing next.next without checking if next is null
D. Node class missing a previous pointer
Solution
Step 1: Trace the while loop iteration
Loop moves current by current.next.next, skipping nodes and risking null access.
Step 2: Identify null pointer risk
When current.next is null, accessing current.next.next causes runtime error.
Final Answer:
Accessing next.next without checking if next is null -> Option C
Quick Check:
Null check missing before next.next access [OK]
Hint: Always check next before accessing next.next [OK]
Common Mistakes:
Ignoring null checks causing runtime errors
Thinking constructor syntax is wrong
Believing console.log causes errors
5. You need to represent a company's organizational chart where each employee can have multiple direct reports. Which data structure should you use and why?
Choose the best option and explain your reasoning.
hard
A. Tree, because it naturally models hierarchical relationships with multiple children
B. Linked List, because it allows dynamic insertion and deletion of employees
C. Stack, because it supports last-in-first-out operations suitable for reporting
D. Array, because it stores elements in order and is easy to access by index
Solution
Step 1: Analyze the problem requirements
Organizational chart has hierarchy with employees having multiple direct reports (children).
Step 2: Match data structure to hierarchy
Tree structure supports nodes with multiple children, representing managers and their reports well.
Step 3: Evaluate other options
Array and linked list are linear and do not represent branching; stack is for LIFO, not hierarchy.
Final Answer:
Tree, because it naturally models hierarchical relationships with multiple children -> Option A
Quick Check:
Hierarchy with multiple children = Tree [OK]
Hint: Hierarchy with multiple children means Tree is best [OK]
Common Mistakes:
Choosing Array for hierarchy
Thinking Linked List handles multiple children well
Confusing Stack with hierarchical data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.