|
4 | 4 | Trees are hierarchical data structures that consist of nodes connected by edges. Each tree has a root node, and every node can have zero or more child nodes. Trees are widely used in computer science for various applications, including representing hierarchical data, parsing expressions, and implementing search algorithms.
|
5 | 5 |
|
6 | 6 | ## Types of Binary Trees:
|
7 | | -Full Binary Tree: A full binary tree is a binary tree where every node has exactly 0 or 2 children. |
| 7 | +Full Binary Tree: A full binary tree is a binary tree where every node has exactly 0 or 2 children.\ |
| 8 | +* Internal nodes always have exactly two children. |
| 9 | +* Leaf nodes may be at different depths. |
| 10 | +* Not necessarily balanced. |
| 11 | +Real-world Use Cases: |
| 12 | +* Represent arithmetic expressions like (a + b) * (c - d). |
| 13 | +Internal nodes = operators (+, -, *, /), leaf nodes = operands (a, b, c, d). |
| 14 | +Always full, since every operator needs exactly two operands. |
| 15 | +* Parsing Trees: Used in compilers to represent syntax structures where binary grammar rules apply. |
| 16 | + |
8 | 17 | ```
|
9 | | - A |
10 | | - / \ |
11 | | - B C |
12 | | - / \ |
13 | | - D E |
| 18 | + A A |
| 19 | + / \ / \ |
| 20 | + B C B C |
| 21 | + / \ / \ |
| 22 | + D E F G |
14 | 23 | ```
|
15 | 24 | Complete Binary Tree: A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
|
| 25 | +* All leaf nodes are as far left as possible. |
| 26 | +* Very efficient memory representation using arrays (no gaps). |
| 27 | +* Not necessarily full or perfect. |
| 28 | +Real-world Use Cases: |
| 29 | + |
16 | 30 | ```
|
17 | 31 | A
|
18 | 32 | / \
|
|
0 commit comments