Tree — Foundations
Terminology, traversals, DFS vs BFS, types of trees
1. Introduction to Trees
A Tree is a hierarchical non-linear data structure consisting of nodes connected by edges.
Unlike Arrays, Linked Lists, Stacks, and Queues:
- Arrays are linear.
- Linked Lists are linear.
- Stacks are linear.
- Queues are linear.
- Trees are hierarchical.
Example:
A
/ \
B C
/ \ \
D E F
Tree starts from a root node and grows downward.
2. Why Trees?
Many real-world systems are hierarchical.
Examples:
- File Systems
- Organization Structure
- Family Tree
- HTML DOM
- Database Indexing
- DNS System
Linear structures become inefficient when relationships become hierarchical.
3. Real World Examples
File System
Root
│
├── Documents
│ ├── Notes
│ └── Reports
│
└── Pictures
├── Photo1
└── Photo2
Organization Structure
CEO
│
├── Manager A
│ ├── Employee 1
│ └── Employee 2
│
└── Manager B
4. Tree Terminology
Understanding terminology is extremely important.
A
/ \
B C
/ \ / \
D E F G
5. Structure of a Tree
Components:
Nodes
Elements of tree.
A B C D E F G
Edges
Connections between nodes.
A-B
A-C
B-D
B-E
C-F
C-G
Root
Topmost node.
A
Every tree has exactly one root.
6. Tree Representation
Example:
10
/ \
20 30
Representation:
10
├──20
└──30
or
Parent -> Children
10 -> [20,30]
7. Node Structure in Java
Generic Tree Node:
class TreeNode {
int data;
TreeNode left;
TreeNode right;
TreeNode(int data){
this.data = data;
}
}
Example:
TreeNode root = new TreeNode(10);
root.left = new TreeNode(20);
root.right = new TreeNode(30);
8. Basic Properties of Trees
A tree:
- Has one root
- Has no cycles
- Is connected
- Has N nodes and N-1 edges
Formula:
If tree has N nodes:
Edges = N - 1
Example:
Nodes = 7
Edges = 6
9. Degree of Node
Degree = Number of children of a node.
Example:
A
/|\
B C D
Degree of A:
3
Degree of B:
0
10. Degree of Tree
Maximum degree among all nodes.
Example:
A has 3 children
B has 2 children
Tree Degree:
3
11. Depth, Level and Height
Consider:
A
/ \
B C
/
D
Level
Root Level:
Level 0
A → Level 0
B,C → Level 1
D → Level 2
Depth
Number of edges from root to node.
Depth of D:
2
Height of Node
Longest path from node to leaf.
Height of B:
1
Height of Tree
Longest path from root to any leaf.
Height:
2
12. Leaf Nodes
Nodes having no children.
Example:
A
/ \
B C
/ \
D E
Leaf Nodes:
B
D
E
13. Internal Nodes
Nodes having at least one child.
Example:
A
C
14. Ancestor and Descendant
Example:
A
/
B
/
C
Ancestors of C:
B
A
Descendants of A:
B
C
15. Sibling Nodes
Nodes sharing same parent.
Example:
A
/ \
B C
B and C are siblings.
16. Subtree
Any node along with its descendants forms a subtree.
Example:
A
/ \
B C
/ \
D E
Subtree rooted at C:
C
/ \
D E
17. Edge Count Formula
For every valid tree:
Edges = Nodes - 1
Example:
Nodes = 10
Edges = 9
18. Types of Trees (Introduction Only)
We will study each separately later.
General Tree
Node can have any number of children.
A
/ | \
B C D
Binary Tree
Each node has at most 2 children.
A
/ \
B C
Binary Search Tree (BST)
Special Binary Tree where:
Left < Root < Right
AVL Tree
Self-balancing Binary Search Tree.
Red Black Tree
Balanced BST using color rules.
Heap
Complete Binary Tree used for priority operations.
Trie
Tree used for strings and dictionaries.
B Tree
Multi-way search tree used in databases.
B+ Tree
Used in DBMS indexing.
Segment Tree
Used for range queries.
Fenwick Tree
Used for prefix sum operations.
19. Tree Traversal Overview
Traversal means visiting every node.
Two major categories:
- Depth First Traversal (DFS)
- Breadth First Traversal (BFS)
20. Depth First Traversal (DFS)
DFS goes deep before moving sideways.
Types:
Preorder
Root
Left
Right
Inorder
Left
Root
Right
Postorder
Left
Right
Root
Example:
A
/ \
B C
Preorder:
A B C
Inorder:
B A C
Postorder:
B C A
21. Breadth First Traversal (BFS)
Also called:
Level Order Traversal
Visit nodes level by level.
Example:
A
/ \
B C
/ \
D E
Output:
A
B C
D E
Uses:
Queue
22. Recursion and Trees
Trees and recursion are closely related.
Example:
void traverse(TreeNode node){
if(node == null)
return;
traverse(node.left);
traverse(node.right);
}
Most tree algorithms use recursion.
23. Array vs Linked List vs Tree
| Feature | Array | Linked List | Tree |
|---|---|---|---|
| Structure | Linear | Linear | Hierarchical |
| Search | O(n) | O(n) | Better (varies) |
| Memory | Contiguous | Dynamic | Dynamic |
| Traversal | Sequential | Sequential | Hierarchical |
24. Applications of Trees
Trees are everywhere.
Examples:
File Systems
Windows Explorer
Linux Directories
Databases
B Trees
B+ Trees
Search Engines
Trie
Operating Systems
Process Trees
Compilers
Parse Trees
Syntax Trees
Networking
DNS Hierarchy
25. Time Complexity Concepts
Complexities depend on tree type.
Common terms:
Height = h
Many operations:
O(h)
Balanced Tree:
O(log n)
Unbalanced Tree:
O(n)
We will study this later in BST and AVL Trees.
26. Common Interview Questions
Theory:
- What is a tree?
- Difference between tree and graph?
- What is height?
- What is depth?
- What is a leaf node?
- What is a subtree?
- Difference between DFS and BFS?
Practical:
- Count nodes
- Count leaves
- Find height
- Level order traversal
- DFS traversal
27. Revision Summary
Tree:
- Hierarchical Data Structure
- Consists of Nodes and Edges
- Has One Root
- No Cycles
Important Terms:
- Root
- Parent
- Child
- Sibling
- Ancestor
- Descendant
- Height
- Depth
- Level
- Leaf Node
- Internal Node
- Subtree
Traversal Types:
DFS:
- Preorder
- Inorder
- Postorder
BFS:
- Level Order
Types of Trees:
- General Tree
- Binary Tree
- BST
- AVL Tree
- Red Black Tree
- Heap
- Trie
- B Tree
- B+ Tree
- Segment Tree
- Fenwick Tree
Learning Path:
Tree Basics → Binary Tree → Binary Tree Traversals → Binary Search Tree → AVL Tree → Heap → Trie → Segment Tree → Advanced Trees