Tree vs Array vs Linked List When Hierarchy Matters
📖 Scenario: Imagine you are organizing a company's employee structure. Each employee has a name and a manager. You want to represent this hierarchy in different ways to understand how data structures like trees, arrays, and linked lists can show relationships.
🎯 Goal: You will create three data structures to represent employees and their managers: a tree, an array, and a linked list. You will then see how each structure shows the hierarchy differently.
📋 What You'll Learn
Create a tree structure using nested dictionaries to represent employees and their subordinates.
Create an array (list) of employee names in order of hierarchy.
Create a linked list where each node contains an employee name and a reference to their manager.
Print each data structure to observe how hierarchy is represented.
💡 Why This Matters
🌍 Real World
Companies often need to represent employee hierarchies to manage reporting lines and responsibilities clearly.
💼 Career
Understanding different data structures to represent hierarchies is useful for software developers working on organizational tools, file systems, or any system with parent-child relationships.
Progress0 / 4 steps
1
Create the employee hierarchy as a tree
Create a variable called employee_tree as a nested dictionary representing this hierarchy: 'CEO' has two subordinates 'CTO' and 'CFO'. 'CTO' has one subordinate 'Dev'. 'CFO' has no subordinates (empty dictionary).
DSA Python
Hint
Use nested dictionaries where keys are employee names and values are dictionaries of their subordinates.
2
Create an array of employees in hierarchy order
Create a list called employee_array with these exact employee names in this order: 'CEO', 'CTO', 'Dev', 'CFO'.
DSA Python
Hint
Use a list with employee names in the order from top to bottom in the hierarchy.
3
Create a linked list of employees with manager references
Create a class called EmployeeNode with attributes name and manager. Then create these nodes: ceo with name 'CEO' and manager=None, cto with name 'CTO' and manager=ceo, dev with name 'Dev' and manager=cto, and cfo with name 'CFO' and manager=ceo.
DSA Python
Hint
Define a class with a constructor that takes name and manager. Create nodes linking each employee to their manager.
4
Print all three data structures to show hierarchy
Print employee_tree, then print employee_array, then print each linked list node's name and their manager.name or null if no manager, in this order: ceo, cto, dev, cfo.
DSA Python
Hint
Use print() to show the tree and array. For linked list nodes, print the employee name and their manager's name or null.
Practice
(1/5)
1. Which data structure is best suited to represent hierarchical relationships like a family tree?
easy
A. Linked List
B. Array
C. Stack
D. Tree
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 D
Quick Check:
Hierarchy = Tree [OK]
Hint: Hierarchy means branching; trees model branching best [OK]
Common Mistakes:
Choosing array for hierarchy
Confusing linked list with tree
Ignoring parent-child relationships
2. Which of the following is the correct way to declare a linked list node in Python?
easy
A. class Node:
def __init__(self, data):
self.data = data
self.prev = None
B. class Node:
def __init__(self, data):
self.data = data
self.next = None
C. class Node:
def __init__(self, data):
self.next = data
self.data = None
D. class Node:
def __init__(self):
self.data = None
self.next = None
Solution
Step 1: Identify linked list node structure
A linked list node stores data and a pointer to the next node.
Step 2: Check each option
class Node:
def __init__(self, data):
self.data = data
self.next = None correctly assigns data and next pointer; others have incorrect attributes or missing data.
Final Answer:
class Node:
def __init__(self, data):
self.data = data
self.next = None -> Option B
Quick Check:
Node has data and next pointer [OK]
Hint: Node must have data and next pointer for linked list [OK]
Common Mistakes:
Swapping data and next attributes
Missing next pointer
Using prev pointer for singly linked list
3. You need to store a company's organizational chart with multiple levels of management and employees. Which data structure is best and why?
A) Array because it stores data contiguously
B) Linked List because it allows easy insertion
C) Stack because it follows last-in-first-out order
D) Tree because it naturally models multiple levels and branches
easy
A. Tree because it naturally models multiple levels and branches
B. Linked List because it allows easy insertion
C. Stack because it follows last-in-first-out order
D. Array because it stores data contiguously
Solution
Step 1: Understand organizational chart structure
It has multiple levels with managers and subordinates, forming a hierarchy.
Step 2: Match data structure to hierarchy
Tree structure models multiple levels and branches naturally, unlike arrays or linked lists which are linear.
Step 3: Eliminate other options
Array is linear and fixed size; linked list is linear; stack is LIFO, not hierarchical.
Final Answer:
Tree because it naturally models multiple levels and branches -> Option A
Quick Check:
Hierarchy needs tree structure [OK]
Hint: Hierarchy with branches = use tree [OK]
Common Mistakes:
Choosing array for hierarchical data
Confusing linked list insertion with hierarchy
Using stack for hierarchical representation
4. What will be the output of the following code that builds a simple tree and prints the root's first child's data?
Root has two children: child1 and child2. root.children[0] is child1, so printing its data outputs 'child1'.
Final Answer:
child1 -> Option A
Quick Check:
First child data = child1 [OK]
Hint: Index 0 child of root is first added child [OK]
Common Mistakes:
Printing root data instead of child
Confusing child1 and child2 order
Assuming error due to children list
5. Find the error in this linked list insertion code:
class Node:
def __init__(self, data):
self.data = data
self.next = None
def insert_at_head(head, data):
new_node = Node(data)
new_node.next = head
head = new_node
return head
head = None
head = insert_at_head(head, 10)
head = insert_at_head(head, 20)
print(head.data)
medium
A. head is not updated outside function
B. new_node.next should be None
C. No error; prints 20
D. Node class missing prev pointer
Solution
Step 1: Analyze insert_at_head function
Function creates new node, points it to current head, updates head to new node, and returns it.
Step 2: Check usage and output
Head is updated by assignment after each insertion. Printing head.data after two insertions prints 20, the last inserted value.
Final Answer:
No error; prints 20 -> Option C
Quick Check:
Head updated correctly, output 20 [OK]
Hint: Function returns new head; assign it back [OK]
Common Mistakes:
Assuming head updates automatically without assignment
Confusing next pointer assignment
Expecting error due to missing prev pointer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.