0
0
DSA Typescriptprogramming~30 mins

Tree vs Array vs Linked List When Hierarchy Matters in DSA Typescript - Build Both Approaches

Choose your learning style9 modes available
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.
Progress0 / 4 steps
1
Create an array of employees with manager IDs
Create a constant array called employeesArray with these exact objects: { id: 1, name: 'CEO', managerId: null }, { id: 2, name: 'CTO', managerId: 1 }, { id: 3, name: 'CFO', managerId: 1 }, { id: 4, name: 'Dev', managerId: 2 }, { id: 5, name: 'Accountant', managerId: 3 }.
DSA Typescript
Hint

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.