0
0
DSA Javascriptprogramming~10 mins

Why Trees Exist and What Linked Lists and Arrays Cannot Do in DSA Javascript - Why It Works

Choose your learning style9 modes available
Concept Flow - Why Trees Exist and What Linked Lists and Arrays Cannot Do
Start: Need to store data
Try Array
Fast access by index
Try Linked List
Easy insert/delete
Limitations
Arrays: Fixed size, slow insert/delete
Linked Lists: Slow search, no hierarchy
Need structure for hierarchy & fast search
Use Tree: Nodes with children, fast search & hierarchy
Shows why arrays and linked lists are limited and how trees solve hierarchical data and fast search needs.
Execution Sample
DSA Javascript
const array = [10, 20, 30];
const linkedList = { val: 10, next: { val: 20, next: null } };
// Tree node example
const tree = { val: 10, children: [{ val: 20, children: [] }, { val: 30, children: [] }] };
Shows simple array, linked list, and tree structures to compare their shapes and capabilities.
Execution Table
StepOperationData StructureLimitation FoundVisual State
1Create array with 3 elementsArrayFixed size, slow insert/delete in middle[10, 20, 30]
2Create linked list with 2 nodesLinked ListSlow search, no hierarchy10 -> 20 -> null
3Try to represent hierarchy with array or linked listArray / Linked ListCannot represent parent-child easilyArray: flat [10, 20, 30] Linked List: linear 10 -> 20 -> null
4Create tree with root and childrenTreeNone (solves hierarchy and search)10 ├─ 20 └─ 30
5Search for value 30 in treeTreeFast search by traversing childrenVisited nodes: 10 -> 20 -> 30
6Try to insert new child 40 under 20TreeEasy insert under parent node10 ├─ 20 │ └─ 40 └─ 30
7Try to insert new element in middle of arrayArraySlow, need shift elements[10, 20, 30] -> Insert 25 at index 2 -> [10, 20, 25, 30]
8Try to insert new node in middle of linked listLinked ListNeed to traverse to position, then insert10 -> 20 -> null -> Insert 15 after 10 -> 10 -> 15 -> 20 -> null
9Conclusion: Trees allow hierarchy and efficient search/insertTreeBest for hierarchical data10 ├─ 20 │ └─ 40 └─ 30
10End---
💡 End of demonstration showing why trees are needed beyond arrays and linked lists.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 6After Step 9
array[][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
linkedListnullnull10 -> 20 -> null10 -> 20 -> null10 -> 20 -> null10 -> 20 -> null
treenullnullnull10 ├─ 20 └─ 3010 ├─ 20 │ └─ 40 └─ 3010 ├─ 20 │ └─ 40 └─ 30
Key Moments - 3 Insights
Why can't arrays represent hierarchical data well?
Arrays store data in a flat sequence without parent-child links, so they cannot show hierarchy like trees do (see execution_table step 3).
Why is inserting in the middle of an array slow?
Because arrays need to shift all elements after the insertion point, which takes time (see execution_table step 7).
How do trees allow fast search compared to linked lists?
Trees organize data in branches, so you can skip large parts of data by choosing child nodes, unlike linked lists which require checking each node one by one (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the visual state of the tree?
A10 -> 20 -> 30 -> null
B10 ├─ 20 └─ 30
C[10, 20, 30]
D10 └─ 20 └─ 30
💡 Hint
Check the Visual State column at step 4 in execution_table.
At which step does the linked list first appear with nodes?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Data Structure and Visual State columns for linked list creation.
If we want to insert a new child under node 20, which data structure allows easy insertion?
ATree
BLinked List
CArray
DNone
💡 Hint
See execution_table step 6 for insertion under node 20.
Concept Snapshot
Why Trees Exist and What Linked Lists and Arrays Cannot Do

- Arrays: fast index access but fixed size and slow middle insert/delete
- Linked Lists: easy insert/delete but slow search and no hierarchy
- Trees: hierarchical nodes with children, fast search and flexible insert
- Trees solve limitations of arrays and linked lists for hierarchical data
- Use trees when data has parent-child relationships or needs fast search
Full Transcript
This lesson shows why arrays and linked lists have limits and why trees exist. Arrays store data in a flat list with fast access by index but are fixed size and slow to insert or delete in the middle. Linked lists allow easy insert and delete but searching is slow and they cannot represent hierarchy. Trees organize data in nodes with children, allowing fast search and representing parent-child relationships. The execution steps show arrays, linked lists, and trees being created and their limitations. Trees allow easy insertion of child nodes and fast searching by traversing branches. This explains why trees are used when data needs hierarchy and efficient search beyond what arrays and linked lists can do.