Tree vs Array vs Linked List When Hierarchy Matters in DSA Javascript - Complexity Comparison
When working with data that has levels or branches, like family trees or company charts, choosing the right structure matters.
We want to see how fast we can find or visit items when the order and connections are important.
Analyze the time complexity of searching for a value in different data structures that keep hierarchy.
// Searching for a value in an array
function searchArray(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return true;
}
return false;
}
// Searching in a linked list
function searchLinkedList(head, target) {
let current = head;
while (current !== null) {
if (current.value === target) return true;
current = current.next;
}
return false;
}
// Searching in a tree (depth-first)
function searchTree(node, target) {
if (node === null) return false;
if (node.value === target) return true;
for (let child of node.children) {
if (searchTree(child, target)) return true;
}
return false;
}
This code shows how we look for a value in an array, a linked list, and a tree, each with different ways to keep hierarchy.
Look at what repeats when searching:
- Primary operation: Checking each element or node's value.
- How many times:
- Array: Once per element, up to n times.
- Linked List: Once per node, up to n times.
- Tree: Visits nodes recursively, up to n times total.
As the number of items (n) grows, the search checks more places:
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the number of items, no matter the structure.
Time Complexity: O(n)
This means the time to find something grows in a straight line with the number of items you have.
[X] Wrong: "Trees always search faster than arrays or linked lists because they have branches."
[OK] Correct: If the tree is not sorted or balanced, you may still visit every node, making it just as slow as the others.
Understanding how different structures handle hierarchy and search helps you choose the right tool and explain your choices clearly in interviews.
"What if the tree was a binary search tree? How would the time complexity of searching change?"
Practice
Solution
Step 1: Understand the hierarchy requirement
A family tree has a parent-child relationship with multiple children per parent, which is a hierarchical structure.Step 2: Match data structure to hierarchy
Trees naturally represent hierarchical data with nodes and branches, allowing multiple children per node.Final Answer:
Tree -> Option DQuick Check:
Hierarchy with multiple children = Tree [OK]
- Choosing Array which is linear, not hierarchical
- Choosing Linked List which supports only one next node
- Confusing Stack with hierarchical structure
Solution
Step 1: Understand linked list node structure
A linked list node typically has a value and a pointer to the next node (next).Step 2: Check each option for correct syntax
const node = { value: 5, next: null }; correctly uses an object with 'value' and 'next' keys. Others either use arrays or swap keys incorrectly.Final Answer:
const node = { value: 5, next: null }; -> Option AQuick Check:
Linked list node = object with value and next [OK]
- Using arrays instead of objects for nodes
- Swapping value and next keys
- Using Array constructor incorrectly
const tree = {
value: 'root',
children: [
{ value: 'child1', children: [] },
{ value: 'child2', children: [
{ value: 'grandchild1', children: [] }
] }
]
};
console.log(tree.children[1].children[0].value);Solution
Step 1: Trace the tree object structure
The root has two children: 'child1' and 'child2'. 'child2' has one child: 'grandchild1'.Step 2: Access the nested value
tree.children[1] is 'child2', its children[0] is 'grandchild1', so accessing value gives 'grandchild1'.Final Answer:
"grandchild1" -> Option AQuick Check:
tree.children[1].children[0].value = "grandchild1" [OK]
- Confusing child indices (0 vs 1)
- Assuming children is a single node, not array
- Accessing wrong property name
const node1 = { value: 1, next: null };
const node2 = { value: 2, next: null };
node1.next = node2;
node2.next = node1;
console.log(node1.next.next.value);Solution
Step 1: Analyze the linked list connections
node1.next points to node2, and node2.next points back to node1, creating a circular link.Step 2: Understand the effect of circular links
Accessing node1.next.next.value goes from node1 to node2, then back to node1, causing infinite traversal if looped.Final Answer:
It creates an infinite loop by linking nodes circularly -> Option CQuick Check:
Circular links cause infinite loops [OK]
- Ignoring circular reference causing infinite loop
- Assuming next.next is null
- Thinking it's a syntax error
const company = [
{ name: 'CEO', reports: [
{ name: 'Manager1', reports: [] },
{ name: 'Manager2', reports: [
{ name: 'Employee1', reports: [] }
] }
] }
];
Which data structure would be best to convert this into for efficient traversal and why?Solution
Step 1: Understand the nested array structure
The company hierarchy has multiple levels with managers and employees reporting to them, forming a hierarchy.Step 2: Choose data structure matching hierarchy and traversal needs
Trees represent hierarchical data with nodes and children, making traversal and queries efficient and natural.Final Answer:
Convert to a Tree because it naturally represents hierarchical relationships with multiple children. -> Option BQuick Check:
Hierarchical data = Tree for efficient traversal [OK]
- Choosing Linked List which is linear, not hierarchical
- Choosing flat Array losing hierarchy info
- Confusing Stack with data structure for storage
Ready to prove it?
Pass the test and this pattern moves unknown
