Tree एक non-linear डेटा स्ट्रक्चर है जिसका इस्तेमाल hierarchical डेटा को organize करने के लिए किया जाता है। Tree को मुख्य रूप से दो तरीकों से represent किया जाता है:-
1:- Linked Representation (Dynamic Representation)
Linked representation में, प्रत्येक node को एक structure के रूप में प्रस्तुत किया जाता है, जिसमें तीन हिस्से होते हैं:
- Data – यह Node में store होने वाली value है।
- Left Pointer – यह Left Child को point करता है।
- Right Pointer यह Right Child को point करता है।
उदाहरण: Binary Tree का Linked Representation
struct Node {
int data; // data
struct Node* left; // left pointer
struct Node* right; // right pointer
};
Linked Representation के फायदे
- Dynamic Size – इसमें नए nodes आसानी से जोड़े जा सकते हैं।
- Memory Efficient – इसमें सिर्फ उतनी ही मेमोरी का इस्तेमाल होता है, जितने nodes हैं।
- Flexible – इससे किसी भी तरह का Tree (Binary, BST, AVL) बना सकते हैं।
2:- Array Representation (Static Representation)
Array Representation में tree को array के रूप में store किया जाता है, जहाँ:
- Root node index 0 होता है।
- Left child का index: 2 * i + 1 होता है।
- Right child का index:2 * i + 2 होता है।
- Parent node का index: (i-1)/2 होता है।
उदाहरण: Binary Tree का Array Representation

Array Representation के फायदे:-
- Random Access – इसमें किसी भी नोड को सीधे एक्सेस कर सकते हैं।
- No Pointers – इसमें extra memory की जरूरत नहीं पड़ती है।
Array Representation के नुकसान:-
- Fixed Size – इसमें पहले से ही array का साइज तय करना पड़ता है।
- Memory Waste – अगर Tree complete नहीं है, तो खाली जगह बर्बाद होती है। जिससे मेमोरी waste होती है।
इसे पढ़ें:-