0% found this document useful (0 votes)
6 views

Balanced Trees in Data Structures

The document provides an overview of balanced trees in data structures, detailing their definitions, importance, characteristics, and types such as AVL trees, Red-Black trees, and B-Trees. It covers operations like insertion and deletion, balancing techniques, and performance analysis, emphasizing the efficiency of these structures in optimizing search and data retrieval. Additionally, it discusses applications in database indexing, memory management, and file systems.

Uploaded by

rhems1995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Balanced Trees in Data Structures

The document provides an overview of balanced trees in data structures, detailing their definitions, importance, characteristics, and types such as AVL trees, Red-Black trees, and B-Trees. It covers operations like insertion and deletion, balancing techniques, and performance analysis, emphasizing the efficiency of these structures in optimizing search and data retrieval. Additionally, it discusses applications in database indexing, memory management, and file systems.

Uploaded by

rhems1995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Balanced Trees in Data

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

Order  30,20,1 30,10,2 10,30,2 10,20,3


0 0 0 0
30 30 10 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}

2-2=0 3-1=2 1-3=-2

1-0=1 1-0=1 0 1-2=-1


0-2=-2 0

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

Initially Insert After LR


20 Rotation
30
30 20
10 LR
10 Imbalance 10 30
20
Types of Balanced Trees
AVL Trees:
Initially Insert After RR
30 Rotation
10
10 20

20
20 RR
10 30
Imbalance
30

Initially Insert After RL


20 Rotation
10
10 20
RL
30
30 Imbalance 10 30
20
Types of Balanced Trees
AVL Trees:

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

before deletion of 32 after deletion


AVL Tree Rebalancing after a
Removal
• Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y
be the child of z with the larger height, and let x be the child of y with the larger height
• We perform a trinode restructuring to restore balance at z
• As this restructuring may upset the balance of another node higher in the tree, we must
continue checking for balance until the root of T is reached

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: Balance restoration using rotations.

-Red-Black Trees: Balancing through recoloring and rotations.


Operations on
Balanced Trees
• Balancing Techniques: Rotations (Single and Double) in
AVL Trees

-AVL Rotations: Single and double rotations explained.

-Red-Black Rules: Maintaining balance using recoloring.


Operations on
Balanced Trees
• Recoloring and Rotations in Red-Black Trees

-Uses color changes and rotations to restore balance

-Ensures Red-Black properties post-insertion/deletion


Operations on
Balanced Trees
• Performance Analysis of Operations: Time Complexity

-Time Complexity: O(log n) for search, insertion, and deletion.


Insertion in Red-Black Trees
1.If tree is empty, create newnode as root node with color Black.
2.If tree is not empty, create newnode as leaf node with color Red.
3.If parent of newnode is Black then exit.
4.If parent of newnode is Red, then check the color of parent’s
sibling of newnode.
a.If the color is Black or null, then do suitable rotation and
recolor.
b.If the color is Red, then recolor and also check if parent’s parent
Tree Traversal in
Balanced Trees
• Tree Traversal Methods: Inorder, Preorder, Postorder, Level
Order

-Inorder Traversal: Visits nodes in ascending order for binary search


trees; visits left subtree, root, then right subtree.
-Preorder Traversal: Visits the root first, then left and right
subtrees; useful for creating a copy of the tree.
-Postorder Traversal: Visits left and right subtrees before the root;
commonly used for deleting or freeing nodes.
-Level Order Traversal: Visits nodes level by level from top to
bottom; implemented using a queue, ideal for breadth-first search.
Tree Traversal in
Balanced Trees
• Traversal Characteristics in AVL and Red-Black Trees

AVL Trees:

-Inorder traversal results in sorted output due to AVL’s strict height-

balancing, making it efficient for ordered data retrieval.

-Ensures that subtree heights differ by at most one, providing

faster access times with O(log n) complexity.


Tree Traversal in
Balanced Trees
• Traversal Characteristics in AVL and Red-Black 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)

9-10. At least 2 Operations of Balance Trees

Make AVL Tree using this key 30,40,10,50,20,5,35 (10 pts)

Balance this tree (10 pts)

44

17 62

50 78

48 54 88

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy