Why Trees Exist and What Linked Lists and Arrays Cannot Do in DSA Javascript - Performance Analysis
We want to understand why trees are used instead of just arrays or linked lists.
What problems do trees solve that arrays and linked lists cannot handle efficiently?
Analyze the time complexity of searching for a value in different data structures.
// Search in an array
function searchArray(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return true;
}
return false;
}
// Search in a binary tree (simplified)
function searchTree(node, target) {
if (!node) return false;
if (node.value === target) return true;
return searchTree(node.left, target) || searchTree(node.right, target);
}
The code shows searching in an array and searching in a tree structure.
Look at what repeats when searching.
- Primary operation: Checking each element or node for the target value.
- How many times: In array, up to n times (each element). In tree, up to n nodes visited in worst case.
As the number of items grows, how does searching time grow?
| Input Size (n) | Approx. Operations in Array | Approx. Operations in Tree |
|---|---|---|
| 10 | Up to 10 checks | Up to 10 node visits |
| 100 | Up to 100 checks | Up to 100 node visits |
| 1000 | Up to 1000 checks | Up to 1000 node visits |
Pattern observation: Both grow linearly if tree is unorganized, but trees can be structured to reduce this.
Time Complexity: O(n)
This means searching can take time proportional to the number of items when data is not organized.
[X] Wrong: "Trees always make searching faster than arrays or linked lists."
[OK] Correct: If the tree is not balanced or organized, searching can still take as long as checking every item.
Understanding why trees exist helps you explain when to use them and how they improve searching and organizing data.
"What if the tree is a balanced binary search tree? How would the time complexity of searching change?"
Practice
Solution
Step 1: Understand the limitation of arrays and linked lists
Arrays and linked lists store data linearly, so they cannot naturally represent parent-child or hierarchical relationships.Step 2: Identify the unique feature of trees
Trees allow nodes to have multiple children, representing hierarchical data like file systems or organization charts.Final Answer:
Trees represent hierarchical relationships which arrays and linked lists cannot. -> Option AQuick Check:
Hierarchy needs trees = B [OK]
- Thinking trees use less memory than arrays
- Believing trees are simpler to implement
- Assuming trees always store sorted data
Solution
Step 1: Recall JavaScript object syntax
Objects use curly braces with key-value pairs, like { key: value }.Step 2: Check each option
const node = { value: 5, children: [] }; uses correct object syntax with keys 'value' and 'children'. const node = [5, []]; is an array, not an object. const node = (value: 5, children: []); uses invalid syntax. const node = new TreeNode(5, []); assumes a class TreeNode which is not defined.Final Answer:
const node = { value: 5, children: [] }; -> Option BQuick Check:
JS object syntax = D [OK]
- Using array syntax instead of object
- Using invalid parentheses syntax
- Assuming class exists without definition
const root = { value: 1, children: [ { value: 2, children: [] }, { value: 3, children: [] } ] };
console.log(root.children[0].value);Solution
Step 1: Understand the tree structure
The root node has value 1 and two children with values 2 and 3.Step 2: Access the first child's value
root.children[0] refers to the first child node with value 2, so root.children[0].value is 2.Final Answer:
2 -> Option DQuick Check:
root.children[0].value = 2 [OK]
- Printing root.value instead of child's
- Using wrong index for children
- Confusing children array with object
const root = { value: 10, children: [] };
root.children.push({ value: 20 });
console.log(root.children[0].value);Solution
Step 1: Check the child node structure
The child node added has only 'value' but no 'children' array, which breaks the tree node structure.Step 2: Understand tree node consistency
Each node should have a 'children' property as an array, even if empty, to maintain uniformity.Final Answer:
The child node is missing the 'children' array property. -> Option CQuick Check:
Missing children array in child node = A [OK]
- Thinking push is invalid on arrays
- Believing root shouldn't have children
- Assuming console.log syntax is wrong
Solution
Step 1: Analyze the problem requirements
The organizational chart has a hierarchy where each employee can have many subordinates, forming a tree-like structure.Step 2: Compare data structures
Arrays and linked lists are linear and cannot represent multiple children per node naturally. Trees allow each node to have multiple children, matching the hierarchy.Final Answer:
Use a tree because it naturally models hierarchical relationships with multiple children per node. -> Option AQuick Check:
Hierarchy with multiple children = tree = C [OK]
- Choosing arrays for hierarchical data
- Using linked lists for multi-child nodes
- Confusing stack usage with hierarchy
Ready to prove it?
Pass the test and this pattern moves unknown
