0
0
DSA Javascriptprogramming~30 mins

Tree vs Array vs Linked List When Hierarchy Matters in DSA Javascript - 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 like arrays, linked lists, and trees work when hierarchy matters.
🎯 Goal: You will create three data structures to represent the same employee hierarchy: an array of employees, a linked list of employees, and a tree of employees. Then, you will write code to display the hierarchy clearly.
📋 What You'll Learn
Create an array called employeesArray with employee names in order
Create a linked list called employeesLinkedList representing the same employees
Create a tree called employeesTree representing the hierarchy with subordinates
Write code to print the array, linked list, and tree hierarchy
💡 Why This Matters
🌍 Real World
Organizing company employees, file systems, or categories often needs hierarchical data structures like trees.
💼 Career
Understanding arrays, linked lists, and trees helps in software development, especially in building user interfaces, databases, and organizational tools.
Progress0 / 4 steps
1
Create an array of employees
Create an array called employeesArray with these exact employee names in order: 'CEO', 'Manager', 'Team Lead', 'Developer', 'Intern'.
DSA Javascript
Hint

Use square brackets [] to create an array and put the employee names as strings inside.

2
Create a linked list of employees
Create a linked list by defining a class called Node with properties name and next. Then create nodes for each employee in this order: 'CEO', 'Manager', 'Team Lead', 'Developer', 'Intern'. Link them so that CEO points to Manager, and so on. Store the head node in a variable called employeesLinkedList.
DSA Javascript
Hint

Create a Node class with name and next. Then create nodes and link them by setting next properties.

3
Create a tree representing the employee hierarchy
Create an object called employeesTree representing the hierarchy: CEO is the root with one child Manager. Manager has one child Team Lead. Team Lead has two children: Developer and Intern. Use the property name for employee name and subordinates as an array of child nodes.
DSA Javascript
Hint

Use nested objects with name and subordinates arrays to build the tree hierarchy.

4
Print the array, linked list, and tree hierarchy
Write code to print the employeesArray as a string joined by ' -> '. Then print the linked list starting from employeesLinkedList by traversing nodes and joining names with ' -> '. Finally, write a recursive function called printTree that prints the employeesTree hierarchy with indentation for subordinates. Call printTree(employeesTree, 0) to display the tree.
DSA Javascript
Hint

Use join(' -> ') for array, a while loop for linked list, and a recursive function with indentation for the tree.