Tree
About Tree Data Structure
A Tree is a non-linear data structure used to represent hierarchical relationships between elements. It consists of nodes connectedT by edges, with one node designated as the root. Unlike arrays, stacks, or linked lists, which store data sequentially, trees are structured to allow for efficient searching, insertion, and deletion operations.
Trees are fundamental in various computing applications, including databases, networking, artificial intelligence, and file systems. They provide fast lookups, efficient sorting mechanisms, and optimized memory usage for hierarchical data storage.
What is a Tree?
A Tree is a hierarchical data structure that consists of nodes connected by edges. It has the following key characteristics:
Root Node: The topmost node in the hierarchy.
Parent-Child Relationship: Each node can have multiple child nodes, but only one parent (except the root, which has no parent).
Leaf Nodes: Nodes that do not have any children.
Height of a Tree: The longest path from the root to any leaf.
Depth of a Node: The distance from the root to that node.
Basic Terminologies
Node
A single element in the tree.
Edge
The connection between two nodes.
Root
The topmost node of the tree.
Parent
A node that has one or more child nodes.
Child
A node that has a parent.
Sibling
Nodes that share the same parent.
Leaf
A node that has no children.
Degree
The number of child nodes a node has.
Height
The longest path from a node to a leaf node.
Depth
The distance from the root node to a specific node.
Breadth
The breadth of a tree refers to the maximum number of nodes present at any level in the tree.
Example
Examples of Trees
Trees are widely used in real-world applications due to their hierarchical nature. Here are some practical examples:
1. File System Hierarchy
The file system in an operating system is organized as a tree:
Each directory is a node, and files or subdirectories are children.
2. Organization Charts
A company’s hierarchy is represented as a tree:
This helps in structuring teams and workflow.
Tree Height, Depth & Breadth
1. Height of a Tree
The height of a tree is defined as the length of the longest path from the root node to a leaf node. It represents the maximum number of edges from the root to any leaf.
Root node height = The height of the entire tree.
Leaf node height = Always
0
, as there are no children below it.
How to Calculate the Height of a Tree?
Start at the root node.
Recursively find the height of all child subtrees.
The height of the tree is
1 + max(height of all child subtrees)
.If the tree has no nodes, the height is -1 (for edge-based height) or 0 (for node-based height).
Example:
G
is a leaf node → Height =0
D
has one childG
→ Height =1
B
has one childD
→ Height =2
E
andF
are leaves → Height =0
C
has two childrenE
andF
→ Height =1
A
(root) has two childrenB
(Height2
) andC
(Height1
) → Height =3
Thus, Height of Tree = 3.
2. Depth of a Node
The depth of a node is the number of edges from the root to that node.
Root node depth = Always
0
.Child node depth =
1 + depth of its parent
.
How to Calculate the Depth of a Node?
Start at the root node (depth = 0).
Move down the tree, increasing depth by 1 at each level.
The depth of a node is the number of edges from the root to that node.
Example:
Depth(A) = 0 (root node)
Depth(B) = 1, Depth(C) = 1 (children of A)
Depth(D) = 2, Depth(E) = 2, Depth(F) = 2 (children of B and C)
Depth(G) = 3 (child of D)
Edge-Based vs. Node-Based Height
Edge-Based Height: The number of edges in the longest path to a leaf (common in CS).
Node-Based Height: The number of nodes in the longest path (sometimes used in theoretical problems).
For a tree with a single node,
Edge-Based Height =
-1
Node-Based Height =
0
3. Breadth of a Tree
The breadth of a tree is the maximum number of nodes at any level.
For example:
Level 0:
A
→ 1 nodeLevel 1:
B, C
→ 2 nodesLevel 2:
D, E, F
→ 3 nodes (maximum)Level 3:
G
→ 1 node
Breadth of this tree = 3 (since level 2 has the most nodes).
Balanced and Unbalanced Tree
1. What is a Balanced Tree?
A Balanced Tree is a tree in which the height difference between the left and right subtrees of any node is at most 1. This ensures that the tree remains efficient for operations like searching, inserting, and deleting.
Characteristics of a Balanced Tree:
The difference in height (Balance Factor) between the left and right subtrees of every node is at most 1.
Searching, inserting, and deleting operations run in O(log N) time.
Prevents the tree from becoming too deep and inefficient.
Examples of Balanced Trees:
AVL Tree: Self-balancing binary search tree where balance factor is maintained between
-1
and1
.Red-Black Tree: A balanced binary search tree used in many libraries (e.g., TreeMap in Java).
B-Trees & B+ Trees: Used in databases for indexing.
Heap (Min Heap / Max Heap): Ensures a balanced structure to maintain efficient extraction of min/max.
The height difference between left and right subtrees for every node is ≤1.
Height = log(N) → Searching is efficient.
Example of a Balanced Tree
Balance Factor Calculation
All nodes have balance factors between -1
and 1
, so this tree is balanced.
Node
Left Subtree Height
Right Subtree Height
Balance Factor (Left - Right)
10
3
3
0
5
2
2
0
15
1
2
-1
2
1
1
0
7
0
1
-1
12
0
1
-1
18
0
0
0
2. What is an Unbalanced Tree?
An Unbalanced Tree is a tree in which the height difference between the left and right subtrees is greater than 1, leading to inefficiencies in operations.
Characteristics of an Unbalanced Tree:
The height of one subtree is much greater than the other.
Searching, inserting, and deleting operations can take O(N) time instead of O(log N).
The tree resembles a linked list in extreme cases (degenerate tree).
The height of the tree is N instead of log(N).
Searching for
4
requires O(N) operations instead of O(log N).
Examples of Unbalanced Trees:
Skewed Binary Tree: All nodes are aligned to one side.
Degenerate Tree: Every node has only one child, forming a linked list.
Example of an Unbalanced Tree (Right-Skewed):
Example of an Unbalanced Tree (Without Being Skewed)
Balance Factor Calculation
Node 15 has a balance factor of
-2
, making the tree unbalanced.Although the left subtree of 10 is balanced, the right subtree is too deep.
Searching for 20 would take longer than expected.
Node
Left Subtree Height
Right Subtree Height
Balance Factor (Left - Right)
10
2
3
-1 (Balanced)
5
2
1
1 (Balanced)
15
0
2
-2 (Unbalanced) X
2
1
0
1 (Balanced)
7
0
0
0 (Balanced)
18
0
1
-1 (Balanced)
1
0
0
0 (Balanced)
20
0
0
0 (Balanced)
3. Why Should a Tree Be Balanced?
Ensures Fast Operations: A balanced tree guarantees that operations like searching, inserting, and deleting run in O(log N) time.
Prevents Performance Degradation: An unbalanced tree degenerates into a linked list, leading to inefficient operations.
Used in Many Real-World Applications: Databases (B-Trees), Memory Management (Heap), and File Systems (Trie) rely on balanced trees.
Types of Trees in Data Structures
1. General Tree
A tree where each node can have any number of children.
Used in hierarchical structures like file systems, organization charts, etc.
Height: Maximum depth of the tree.
Breadth: Maximum number of nodes at any level.
2. Binary Tree
Each node has at most two children: left and right.
Types of binary trees include:
Full Binary Tree
Complete Binary Tree
Perfect Binary Tree
Balanced Binary Tree
Degenerate Tree
Height: At most O(n) for an unbalanced tree, O(log n) for balanced trees.
Depth: Varies depending on the structure.
2.1. Full Binary Tree
A binary tree where every node has either 0 or 2 children (no nodes with only one child).
Height: h=O(logn)
Breadth: Maximum at the last level.
2.2. Complete Binary Tree
A binary tree where all levels except the last are completely filled, and nodes in the last level are as left as possible.
Example: Heap data structures follow this rule.
Height: O(logn)
Breadth: Maximum at last level 2^(h−1)
2.3. Perfect Binary Tree
A binary tree where all levels are fully filled.
Number of nodes at depth d is 2^d.
Height: h=log(n+1)−1 (log with base 2)
Breadth: 2^(h−1) (maximum nodes at last level).
2.4. Balanced Binary Tree
A binary tree where the height of left and right subtrees differs by at most 1.
Example: AVL Tree, Red-Black Tree.
Height: O(logn).
Breadth: Maximum at last level.
2.5. Degenerate (Skewed Tree)
A linked list-like structure, where each node has only one child.
Height: O(n)(worst case).
Breadth: Always 1.
3. Binary Search Tree (BST)
A Binary Search Tree (BST) is a binary tree in which:
Each node has at most two children (left and right).
The left subtree contains only nodes with values smaller than the parent node.
The right subtree contains only nodes with values greater than the parent node.
The left and right subtrees must also be BSTs.
Used for efficient searching.
Height: O(logn) (balanced), O(n) (worst case).
Example of a BST
BST Property Holds:
Left subtree of
10
→{5, 2, 7}
(All values < 10)Right subtree of
10
→{15, 12, 20}
(All values > 10)This rule applies to all nodes recursively.
A Non-Binary Search Tree is any tree that does not follow the BST property. It can be:
A Binary Tree that does not maintain BST ordering.
A tree that is not even binary (i.e., nodes have more than two children).
Example of a Non-Binary Search Tree (Binary but not BST)
Not a BST because:
The left subtree of
5
contains12
, but12 > 5
The right subtree of
15
contains7
, but7 < 15
This is not a BST, even though it is a binary tree.
Example of a Non-Binary Search Tree (Non-Binary)
A General Tree where each node can have more than two children:
Not a BST because:
More than two children per node
No strict left < parent < right rule
This is an n-ary tree and does not qualify as a BST.
4. AVL Tree (Self-Balancing BST)
A BST with a balance factor (height difference of left and right subtree is at most 1).
Height: O(logn).
Used for fast lookups.
5. Red-Black Tree
A self-balancing BST with the following rules:
Nodes are either Red or Black.
Root is always black.
Red nodes cannot be consecutive.
All paths from root to leaves have the same number of black nodes.
Height: O(logn)
6. B-Trees & B+ Trees
B-Tree: A self-balancing multi-way tree (used in databases).
B+ Tree: Variation of B-tree where all values are stored in leaf nodes (used in file systems).
Height: O(logn)
7. Trie (Prefix Tree)
A Trie (pronounced "try") is a tree-based data structure used to store strings efficiently for fast retrieval. It is also called a prefix tree because all common prefixes are shared among words.
Features of a Trie:
Each node represents a character in a string.
The root node represents an empty string.
Each path from the root to a leaf forms a word.
Efficient for prefix-based searching, autocomplete, and dictionary implementations.
Structure of a Trie
A Trie consists of:
Nodes – Each node contains:
A character
A list of child nodes (pointers to next characters)
A flag to mark the end of a word
Edges – Represent connections between characters.
Example of a Trie storing "car", "cat", "bat", and "bar":
Words Stored: "car"
, "cat"
, "bat"
, "bar"
Common Prefixes are Shared: "ca"
is shared between "car"
and "cat"
.
8. Segment Tree
A tree used for range queries and updates (sum, min, max).
Height: O(logn).
Example: Finding the sum of elements in an array from index 2 to 5.
9. Fenwick Tree (Binary Indexed Tree - BIT)
A more space-efficient version of a segment tree.
Used for fast prefix sum calculations.
Height: O(logn)
10. Heap (Min Heap, Max Heap)
A Heap is a specialized tree-based data structure that satisfies the Heap Property. It is commonly used for priority queues, scheduling algorithms, and efficient sorting techniques (Heap Sort).
1. What is a Heap?
A Heap is a complete binary tree (every level is fully filled except possibly the last level, which is filled from left to right) that satisfies one of the following properties:
Min Heap Property → The parent node is always smaller than its children.
Max Heap Property → The parent node is always greater than its children.
2. Min Heap
A Min Heap is a binary tree where:
The root node is the smallest element in the tree.
Every parent node has a value smaller than or equal to its children.
The smallest element is always at the root.
Example of a Min Heap
Min Heap Property Holds:
2 < 5
,2 < 7
(Root is smallest)5 < 10
,5 < 15
,7 < 17
,7 < 25
Operations in Min Heap
Operation
Time Complexity
Insertion
O(log N)
Deletion (extract min)
O(log N)
Heapify (convert array to heap)
O(N)
Find Min
O(1)
(always the root)
3. Max Heap
A Max Heap is a binary tree where:
The root node is the largest element in the tree.
Every parent node has a value greater than or equal to its children.
The largest element is always at the root.
Example of a Max Heap
Max Heap Property Holds:
25 > 17
,25 > 10
(Root is largest)17 > 7
,17 > 5
,10 > 2
,10 > 15
Operations in Max Heap
Operation
Time Complexity
Insertion
O(log N)
Deletion (extract max)
O(log N)
Heapify (convert array to heap)
O(N)
Find Max
O(1)
(always the root)
11. Suffix Tree
A compressed Trie used for string matching.
Height: O(m).
Used in bioinformatics, search engines.
Comparisons of Tree Types
General Tree
Any number of children
O(n)
O(n)
Hierarchical data
Binary Tree
At most 2 children
O(n)
O(n)
Basic tree structure
BST
Sorted Binary Tree
O(log n)
O(log n)
Searching
AVL Tree
Self-balancing BST
O(log n)
O(log n)
Fast lookups
Red-Black Tree
Self-balancing BST
O(log n)
O(log n)
Efficient insertions
Trie
Prefix-based
O(m)
O(n)
Autocomplete, dictionaries
Segment Tree
Interval-based
O(log n)
O(log n)
Range queries
Heap
Complete Binary Tree
O(log n)
O(2^h)
Priority Queues
Last updated
Was this helpful?