Tree Fundamentals & Binary Tree Varieties
Hierarchical terminology, mathematical relationship between edges and nodes (E = V - 1), full vs complete vs perfect binary trees.
Topics Covered:
63. Tree Terminology & Anatomy • 64. Fundamental Mathematical Tree Theorems • 65. Structural Classification of Trees • 66. Binary Tree Anatomy • 67. Complete Binary Tree • 68. Full (Strict) Binary Tree • 69. Perfect Binary Tree • 70. Balanced Trees • 71. Degenerate (Skewed) Tree
Trees represent computing's primary hierarchical abstraction, modeling nested relationships, recursive sub-problems, and logarithmic search boundaries across memory and storage systems. Unlike linear structures (arrays and linked lists) that enforce a single predecessor and successor, trees organize data through parent-child hierarchies that balance storage flexibility with efficient multi-way branching. Understanding tree topology, topological invariants, node degrees, and structural tree varieties forms the prerequisite for mastering self-balancing search trees, spatial indices, and heap architectures.
Learning Objectives
#- Formulate the graph-theoretic definition of a rooted tree and verify the edge-vertex invariant .
- Distinguish between node depth, node height, subtree height, and tree diameter across hierarchical structures.
- Prove the Full Binary Tree theorem relating leaf count to internal node count ().
- Differentiate the 5 canonical binary tree varieties: Full, Complete, Perfect, Balanced, and Degenerate (Skewed).
- Implement zero-pointer array mapping for Complete Binary Trees via arithmetic child/parent indexing ().
Topic 63: Tree Terminology & Anatomy
#1. Graph-Theoretic Formalism
#Formally, a Tree is an undirected, connected, acyclic graph. When directed away from a distinguished origin, it is called a Rooted Directed Tree, defined by three invariant properties:
- Unique Root: There exists exactly one node with in-degree .
- Single Predecessor: Every other node has an in-degree of strictly (exactly one parent).
- Acyclic Connectivity: For every node , there exists a unique simple directed path from root to .
2. Comprehensive Anatomy of a Sample Tree
#Consider a hierarchical tree containing 9 nodes with root :
- connects to children and .
- connects to children and .
- connects to child .
- connects to children and .
- connects to child .
The table below catalogs every node's topological measurements and anatomical classification:
| Node | Parent | Children | In-Degree () | Out-Degree () | Depth from Root | Height to Leaf | Topological Role |
|---|---|---|---|---|---|---|---|
A | None () | 0 | 2 | 0 | 3 | Root Node () | |
B | 1 | 2 | 1 | 2 | Internal Node (Ancestor of ) | ||
C | 1 | 1 | 1 | 2 | Internal Node (Sibling of ) | ||
D | 1 | 0 | 2 | 0 | Leaf Node () | ||
E | 1 | 2 | 2 | 1 | Internal Node | ||
F | 1 | 1 | 2 | 1 | Internal Node | ||
G | 1 | 0 | 3 | 0 | Leaf Node (Terminal) | ||
H | 1 | 0 | 3 | 0 | Leaf Node (Terminal, Sibling of ) | ||
I | 1 | 0 | 3 | 0 | Leaf Node (Terminal) |
3. Metric Definitions & Path Semantics
#- Depth of Node (): The number of edges on the unique path from root down to node . By definition, .
- Height of Node (): The number of edges on the longest simple downward path from node to any leaf in its subtree. For all leaves, .
- Height of Tree (): The height of the root node: . For the tree above, .
- Degree of Node (): In rooted trees, this refers to the out-degree (the number of children attached to ).
- Degree of Tree (): The maximum degree across all nodes: . For binary trees, .
- Subtree: A tree consisting of a node and all of its descendants, retaining all connecting edges.
Topic 64: Fundamental Mathematical Tree Theorems
#Theorem 1: The Edge-to-Vertex Invariant
#For any connected tree containing nodes:
Proof:
In any rooted tree, every node except the root has exactly one incoming edge connecting it to its parent. Because the root has zero incoming edges, the total number of edges equals the sum of in-degrees of all non-root nodes:
Theorem 2: Maximum Nodes at Level
#In any binary tree, level (where the root is defined as level ) contains at most:
Proof (By Mathematical Induction):
- Base Case (): At level , there is only the root node. . The base case holds.
- Inductive Step: Assume level has at most nodes. Each node in a binary tree has at most children. Therefore, the number of nodes at level is at most:By induction, level contains at most nodes for all .
Theorem 3: Maximum Nodes in a Binary Tree of Height
#A binary tree of height can contain at most:
Conversely, for a binary tree with nodes, its height is lower-bounded by:
Theorem 4: The Full Binary Tree Theorem ()
#In any non-empty full (strict) binary tree where every node has either 0 or 2 children:
Proof:
- Total nodes .
- Every internal node contributes exactly 2 outgoing edges, while leaves contribute 0 outgoing edges:
- By Theorem 1, :
- Subtracting from both sides:
Topics 65–71: Binary Tree Varieties
#A Binary Tree is a specialized tree in which every node has at most two children, designated as the Left Child and the Right Child. Binary trees are categorized into five distinct structural archetypes based on balance and level fullness.
1. Structural Comparison Matrix
#| Binary Tree Variety | Structural Invariant | Height Bound | Leaf Node Count | Pointerless Array Storage? | Primary Algorithmic Application |
|---|---|---|---|---|---|
| Full (Strict) Binary Tree | Every node has degree ; never 1 child | to | No | Huffman coding trees, expression parsing | |
| Perfect Binary Tree | All internal nodes have 2 children; all leaves at depth | Yes () | Complete parallel divide-and-conquer networks | ||
| Complete Binary Tree | All levels filled except last; last filled left-to-right | Yes (Heap array) | Binary Heaps, Priority Queues | ||
| Balanced Binary Tree | For all nodes, | No (Pointers needed) | AVL Trees, Red-Black Trees, Map/Set | ||
| Degenerate (Skewed) | Every internal node has exactly 1 child | (worst-case) | No | Degenerate BST (linked list behavior) |
2. Complete Binary Tree: The Zero-Pointer Array Mapping
#Complete Binary Trees possess an exceptional architectural property: they can be stored contiguously in a flat array without allocating any pointer variables (left, right, parent). The tree hierarchy is maintained entirely through arithmetic index relationships.
For an -element Complete Binary Tree mapped into zero-based array via level-order indexing:
- Root Element: Stored at index .
- Left Child of Node :
- Right Child of Node :
- Parent of Node ():
Array Mapping Trace: 6-Node Complete Tree ()
| Array Index () | Stored Value | Tree Level | Left Child Index () | Right Child Index () | Parent Index () | Node Topology Type |
|---|---|---|---|---|---|---|
0 | A | Level 0 | (Value B) | (Value C) | None | Root Node |
1 | B | Level 1 | (Value D) | (Value E) | (Value A) | Internal Node |
2 | C | Level 1 | (Value F) | Out of bounds | (Value A) | Internal Node |
3 | D | Level 2 | Out of bounds | Out of bounds | (Value B) | Leaf Node |
4 | E | Level 2 | Out of bounds | Out of bounds | (Value B) | Leaf Node |
5 | F | Level 2 | Out of bounds | Out of bounds | (Value C) | Leaf Node |
Hardware & Cache Advantage:
Because the tree nodes reside in contiguous memory, traversing parent-child relationships incurs zero pointer-dereference latency. Sequential memory reads maximize L1 CPU cache line utilization and eliminate the 16-to-24 byte per-node pointer overhead required by traditional dynamic tree node allocations.
Module 01 Summary & Key Takeaways
#- Topological Tree Invariant: A tree of nodes contains strictly edges and zero cycles. Every non-root node has an in-degree of exactly 1.
- Depth vs Height: Node depth counts edges from the root down to the node; node height counts edges on the longest downward path from the node to a leaf. The tree height is the height of its root.
- Full Binary Tree Relation: In any full binary tree (where each node has either 0 or 2 children), the number of leaves is always one greater than the number of internal nodes: .
- Complete Binary Tree Array Mapping: Complete binary trees map into flat arrays with zero pointer overhead using arithmetic indexing (), forming the physical basis for binary heaps.
- The Cost of Imbalance: A degenerate (skewed) binary tree has height , collapsing tree operations from logarithmic to linear linked list scans. Maintaining balance is essential for performance.
References & Academic Attribution
#- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to Algorithms (4th ed.), Appendix B.5: Trees & Chapter 12: Binary Search Trees. MIT Press.
- Knuth, D. E. (1997). The Art of Computer Programming, Volume 1: Fundamental Algorithms (3rd ed.), Section 2.3: Trees. Addison-Wesley.
- Sedgewick, R., & Wayne, K. (2011). Algorithms (4th ed.), Section 3.2: Binary Search Trees. Addison-Wesley.