Balanced Trees in Data Structures
Balanced Trees in Data Structures
Structures
Understanding Structure, Types, Operations, and Applications
Presenter:
Rhem Sumodlayon
Robert Encio
November 6, 2024
Introduction to Balanced
Trees
• Definition: Balanced trees maintain a nearly uniform height, ensuring efficient operations.
• Importance: Key in optimizing search, insertion, and deletion operations, critical for
performance in data structures.
• Characteristics:
-Height-Balancing: Minimizes tree height variations.
-Efficiency: Ensures operations run in logarithmic time.
• Comparison:
-Unbalanced Trees: Can degrade to linear time complexity.
-Balanced Trees vs. Other Structures: Why balancing matters.
Types of Balanced Trees
Red-Black
AVL Trees: B-Trees: Splay Trees:
Trees:
• Definition: • Characteristic • Structure: • Definition:
Binary search s: A self- Multi-way Self-adjusting
tree with height balancing search tree. tree.
difference binary search • Order & Use • Properties:
constraints. tree with Cases: Ideal for Recently
• Properties: specific rules. databases and accessed
Balance factor • Properties & file systems. elements move
within -1 to 1. Rules: closer to the
Red/black root.
coloring, no two
red nodes
together, path
from root to leaf
with equal black
nodes.
Types of Balanced Trees
AVL Trees:
• Definition: Binary search tree with height difference constraints.
• Properties: Balance factor within -1 to 1.
h
BST
40 root 1
20 50 2
10 30 45 60 3
Types of Balanced Trees
AVL Trees:
Key: Key:
30,40,10,50,20,5,35 50,40,35,30,20,10,5
50 h
30 h
40
10 40 Log 35 n
n
30
5 20 35 50
20
10
5
Types of Balanced Trees
AVL Trees:
Key: 30, 20,10
20 10 30 20
10 20 20 30
20,30,1
0
20,10,3
0 20
10 30
Types of Balanced Trees
AVL Trees:
Balance factor = height of left subtree – height of right subtree
bf = hl – hr = {-1,0,1}
0 0 1-0=1 0 0-1=-1
0
Types of Balanced Trees
AVL Trees:
Initially Insert After LL
10 Rotation
30
30 20
20 LL
20 Imbalance 10 30
10
20
20 RR
10 30
Imbalance
30
LL Rotation
A
B Ar B
Br
A
C C
Cr Br Ar
Cl Cl Cr
Types of Balanced Trees
Red-Black Trees:
• Characteristics: A self-balancing binary search tree with specific rules.
• Properties & Rules: Red/black coloring, no two red nodes together, path from root to leaf
with equal black nodes.
Types of Balanced Trees
Red-Black Trees:
• Characteristics: A self-balancing binary search tree with specific rules.
• Properties & Rules: Red/black coloring, no two red nodes together, path from root to leaf
with equal black nodes.
Types of Balanced Trees
B-Trees:
• Structure: Multi-way search tree.
• Order & Use Cases: Ideal for databases and file systems.
Operations on Balanced Trees
• Insertion and Deletion:
-AVL Trees: Balance restoration using rotations.
-Red-Black Trees: Balancing through recoloring and
rotations.
• Balancing Techniques:
-AVL Rotations: Single and double rotations explained.
-Red-Black Rules: Maintaining balance using recoloring.
• Performance Analysis:
-Time Complexity: O(log n) for search, insertion, and
deletion.
AVL Tree
Insertion
• Insertion is as in a binary search tree
• Always done by expanding an external node.
• Example: 44 44
17 78 17 78 c=z
a=y
32 50 88 32 50 88
48 62 48 62
b=x
54
w
before insertion
after insertion
AVL Insertion
• Insertion is as in a binary search tree
• Always done by expanding an external node.
• Example:
44 44
c=z
17 78 17 78
a=y
32 50 88 32 50 88
48 62 48 62
b=x
54
w
before insertion
after insertion
AVL Tree Insertion Example,
continued 44
5
2
z 64
17 78 7
3
2 y 1
1
32 1 50 4 88
1
2 x
48
1
3 62
5
54 T3
unbalanced... T2
T0
4
T1 44 4
2 3 x
17
2 y 62
z6
1 2 2
32
1
1 50 3 5
78 7
1 1
...balanced 48 54 88
T2
T0 T1 T3
AVL Tree Removal
• Removal begins as in a binary search tree, which means the node removed
will become an empty external node. Its parent, w, may cause an imbalance.
• Example:
44 44
17 62 17 62
32 50 78 50 78
48 54 88 48 54 88
62
a=z 44
44 78
w 17 62 b=y
17 50 88
50 78 c=x
48 54
48 54 88
Operations on
Balanced Trees
• Insertion and Deletion Procedures in AVL and Red-Black
Trees
AVL Trees:
Red-Black Trees:
-Slightly less strictly balanced than AVL trees, but maintains O(log
n) access times.
-Recoloring and rotations may influence traversal characteristics,
yet still provide a predictable, balanced structure.
-The red and black properties aid in efficient rebalancing, ensuring
even traversal times.
Applications of
Balanced Trees
• Use Cases in Database Indexing and Searching Algorithms
Database Indexing:
-Binary Search Trees (BST): Inordered traversals in balanced
trees (like AVL or B-trees) create sorted indexes for quick data
retrieval.
-B- Trees and B+ Trees: Commonly used for database indexing
due to their high branching factor, allowing efficient multi-level
indexing with minimal disk reads.
-Red-Black Trees: Often applied in memory indexing structures
(e.g., Linux's virtual memory areas) due to their balance and
Applications of
Balanced Trees
• Use Cases in Database Indexing and Searching Algorithms
Searching Algorithms:
-Binary Search: Efficiently uses balanced BSTs, where searching is
O(log n) due to the tree's height-balance, providing predictable
access times.
-Trie Structures: Used for prefix-based searches, such as
autocomplete and spell-check, as each node represents a letter in a
word, supporting quick lookup.
-Hash Indexes with Trees: Combining hashing with tree
structures, such as in a hash tree or trie, allows both exact and
Applications of
Balanced Trees
• Applications in Memory Management and File Systems
Memory Management:
-Red-Black Trees: Used in operating systems (like Linux) to
manage memory allocation for virtual memory areas (VMAs). Red-
Black Trees maintain balance, allowing efficient insertion, deletion,
and search of memory segments.
-Buddy Memory Allocation System: Often represented using
binary trees, where memory is divided and managed in powers of
two. This approach enables fast allocation and deallocation by
merging or splitting blocks.
Applications of
Balanced Trees
• Applications in Memory Management and File Systems
Memory Management:
-Heap Memory Management: Binary trees (e.g., binary heaps)
help allocate memory in priority queues and garbage collection,
where elements are sorted based on memory usage priority.
Applications of
Balanced Trees
• Applications in Memory Management and File Systems
File Systems:
-B-Trees and B+ Trees: Commonly used in file systems (e.g.,
NTFS, HFS+) for directory indexing and file retrieval, as they offer
efficient search and insertion with reduced disk I/O due to their multi-
level structure.
-Inodes in Unix-based Systems: Implement tree-like structures
to manage file metadata, allowing hierarchical navigation and
efficient storage of directory and file information.
Applications of
Balanced Trees
• Applications in Memory Management and File Systems
File Systems:
-Log-Structured File Systems (LFS): Use balanced trees to
manage file versions and changes, storing updates sequentially and
allowing for faster reads and writes in SSDs.
1-8. 4 types of balance trees (2 points each)
44
17 62
50 78
48 54 88