|
| 1 | +## 4. Trees |
| 2 | +A **Tree** is a hierarchical data structure consisting of nodes with a parent-child relationship. It starts with a **root** node and expands into subtrees of children, representing various hierarchical levels. |
| 3 | + |
| 4 | +### Key Characteristics |
| 5 | +- Each node has data and links to child nodes. |
| 6 | +- Commonly represented as a **rooted tree** with a single root node. |
| 7 | + |
| 8 | +### Types of Trees |
| 9 | +- **Binary Tree**: Each node has at most two children. |
| 10 | +- **Binary Search Tree (BST)**: A binary tree with the left child <= root <= right child. |
| 11 | +- **Balanced Trees**: Trees like AVL, Red-Black Trees keep balance for efficient operations. |
| 12 | +- **Heap**: A complete binary tree used for priority queues. |
| 13 | +- **Trie**: A tree for storing strings, useful in search applications. |
| 14 | +- **B-Trees**: Balanced trees used in databases and filesystems. |
| 15 | + |
| 16 | +### Common Operations |
| 17 | +- **Insertion**: |
| 18 | + - Adds nodes based on tree type (e.g., BST properties). |
| 19 | + - Complexity: \(O(\log n)\) for balanced trees; \(O(n)\) for unbalanced. |
| 20 | +- **Deletion**: |
| 21 | + - Removes a node while maintaining tree properties. |
| 22 | + - Complexity: \(O(\log n)\) for balanced trees; \(O(n)\) for unbalanced. |
| 23 | +- **Traversal**: |
| 24 | + - **In-order**: Left, Root, Right (BST sorted order). |
| 25 | + - **Pre-order**: Root, Left, Right (used for copying). |
| 26 | + - **Post-order**: Left, Right, Root (used for deletion). |
| 27 | + - Complexity: \(O(n)\) |
| 28 | +- **Searching**: |
| 29 | + - Complexity: \(O(\log n)\) for balanced trees; \(O(n)\) for unbalanced. |
| 30 | + |
| 31 | +### Applications |
| 32 | +- **Hierarchical Data Representation**: File systems, organizational structures. |
| 33 | +- **Binary Search Trees (BSTs)**: Efficient searching, insertion, and deletion. |
| 34 | +- **Priority Queues**: Implemented using heaps. |
| 35 | +- **Trie**: Used for autocomplete and dictionary implementations. |
| 36 | +- **Databases and Filesystems**: B-trees provide efficient data retrieval. |
| 37 | + |
0 commit comments