# 2. Prerequisite: An Introduction to the Tree Data Structure

<span class="ng-star-inserted">Before we can understand the Heap, we must first be familiar with its underlying structure: the Tree. In computer science, a </span>**<span class="ng-star-inserted">tree</span>**<span class="ng-star-inserted"> is a widely used data structure that simulates a hierarchical structure, with a set of connected nodes.</span>

#### **<span class="ng-star-inserted">Core Components</span>**

<span class="ng-star-inserted">Every tree is composed of a few fundamental components:</span>

- **<span class="ng-star-inserted">Node:</span>**<span class="ng-star-inserted"> The primary entity in a tree that contains data. It is also known as a "vertex".</span>
- **<span class="ng-star-inserted">Edge:</span>**<span class="ng-star-inserted"> A connection or link between two nodes.</span>
- **<span class="ng-star-inserted">Root:</span>**<span class="ng-star-inserted"> The topmost node in a tree. It is the only node that does not have a parent.</span>

<span class="ng-star-inserted">Consider the tree structure below:</span>

- <span class="ng-star-inserted">Nodes: {A, B, C, D, E}</span>
- <span class="ng-star-inserted">Edges: {(A-B), (A-C), (B-D), (B-E)}</span>
- <span class="ng-star-inserted">Root: Node A</span>

#### **<span class="ng-star-inserted">Hierarchical Terminology</span>**

<span class="ng-star-inserted">The relationships between nodes in a tree are described using terminology borrowed from family trees:</span>

- **<span class="ng-star-inserted">Parent:</span>**<span class="ng-star-inserted"> A node that is directly above another node and connected to it. </span><span class="ng-star-inserted">In our example, A is the parent of B and C.</span>
- **<span class="ng-star-inserted">Child:</span>**<span class="ng-star-inserted"> A node that is directly below another node and connected to it. </span><span class="ng-star-inserted">In our example, D and E are children of B.</span>
- **<span class="ng-star-inserted">Leaf:</span>**<span class="ng-star-inserted"> A node that has no children. </span><span class="ng-star-inserted">In our example, C, D, and E are leaf nodes.</span>

#### **<span class="ng-star-inserted">Structure and Depth</span>**

<span class="ng-star-inserted">We can measure the structure of a tree in several ways:</span>

- **<span class="ng-star-inserted">Level:</span>**<span class="ng-star-inserted"> The level of a node is defined by its distance from the root. The root is at </span>**<span class="ng-star-inserted">Level 0</span>**<span class="ng-star-inserted">. Its children are at Level 1, their children are at Level 2, and so on.</span>
- **<span class="ng-star-inserted">Depth/Height:</span>**<span class="ng-star-inserted"> The height of a tree is the number of edges on the longest path from the root to a leaf. </span><span class="ng-star-inserted">Our example tree has a height of 2.</span>

#### **<span class="ng-star-inserted">Focus on the Binary Tree</span>**

<span class="ng-star-inserted">While a node in a tree can have any number of children, our focus for understanding heaps will be on a specific type: the </span>**<span class="ng-star-inserted">Binary Tree</span>**<span class="ng-star-inserted">.</span>

<span class="ng-star-inserted">A </span>**<span class="ng-star-inserted">Binary Tree</span>**<span class="ng-star-inserted"> is a tree data structure in which each node has </span>**<span class="ng-star-inserted">at most two children</span>**<span class="ng-star-inserted">, which are referred to as the </span><span class="ng-star-inserted">left child</span><span class="ng-star-inserted"> and the </span><span class="ng-star-inserted">right child</span><span class="ng-star-inserted">.</span>

<span class="ng-star-inserted">The reason we focus on Binary Trees is that the </span>**<span class="ng-star-inserted">Heap</span>**<span class="ng-star-inserted"> data structure, which is the engine of Heap Sort, is a specialized type of Binary Tree. Mastering this concept is a fundamental step toward understanding heaps.</span>