📚 Chapter 1

🌳 Tree Basics: Introduction to Hierarchical Structures

Learn the definition, terminology, and representations of tree data structures

🌲 Tree Structure Visualization (Click a node!)

Root Node
Internal Node
Leaf Node
📊 Current Tree Properties
Nodes
10
Edges
9
Height
3
Leaf Nodes
5
Internal Nodes
4
Degree
2

📝 Tree Terminology Dictionary (Click to visualize)

Root
The topmost node of the tree, has no parent
Parent
A node that has child nodes
Child
A lower node connected from a parent
Sibling
Nodes that share the same parent
Leaf
A terminal node with no children
Internal Nodes Internal
A node that has children (excluding leaves)
Ancestor
All nodes on the path to the root
Descendant
All nodes in the subtree of a given node
Depth
Number of edges from root to the node
Height Height
Number of edges from the node to the deepest leaf

📖 Concept Guide

🌳 What is a Tree?

A Tree is a hierarchical data structure consisting of nodes and edges.

💡 Key Properties:
• A single root node exists
• Every node except root has exactly one parent
No cycles — acyclic connected graph

Recursive Definition:

  • An empty tree is a tree
  • A root node r with 0 or more subtrees T₁, T₂, ... Tₖ is a tree

📐 Mathematical Properties of Trees

PropertyFormula
EdgesE = N - 1 (N: Nodes)
Max nodes in binary tree2^h - 1 (h: Height)
Max nodes at level k in binary tree2^(k-1)
Min nodes for height hh (skewed tree)
📊 Edges = Nodes - 1
The most fundamental formula that always holds in trees!
10 nodes → 9 edges

🌲 Types of Trees

TypeFeatures
Binary TreeEach node has at most 2 children
Binary Search TreeLeft < Parent < Right
Complete Binary TreeAll levels filled except possibly the last
Full Binary TreeAll levels are completely filled
Balanced TreeHeight difference between left and right ≤ 1
AVL / Red-BlackSelf-balancing binary search tree
B-TreeMulti-way search tree, used in databases

🌍 Real-World Applications of Trees

  • File System — Hierarchical structure of folders and files
  • HTML DOM — Element structure of web pages
  • Database Index — B-Tree, B+Tree
  • Decision Tree — Machine learning, AI
  • Organization Chart — Company structure
  • Family Tree — Family relationships
  • Compiler — Abstract Syntax Tree (AST)
  • Routing — Network path finding

🎯 Selected Node Info

📍 Click a node
Click a node in the tree to display its detailed information.
🔦 Highlight by Term
💾 Tree Representations
// Complete binary tree array representation // Index: 0 1 2 3 4 5 6 tree[] = [A, B, C, D, E, F, G] // Parent: (i-1)/2 // Left child: 2*i + 1 // Right child: 2*i + 2
struct Node { int data; Node* left; Node* right; }; // Each node links to children via pointers // Flexible but has memory overhead
// Parent array representation // parent[i] = parent index of node i parent[] = [-1, 0, 0, 1, 1, 2, 2] // Root parent is -1 // Used in Union-Find
📐 Key Formulas
Edges
E = N - 1
Binary tree parent index
(i - 1) / 2
Left/Right child
2i + 1 / 2i + 2
🌍 Real-World Examples
📁 File System — Folder hierarchy
🏢 Org Chart — Company structure
🌐 HTML DOM — Web element structure