-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA2_5.html
More file actions
93 lines (86 loc) · 6.33 KB
/
Copy pathDSA2_5.html
File metadata and controls
93 lines (86 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<article>
<h1>DSA II: Page 5 - Balanced Trees</h1>
<section>
<h2>The Danger of Unbalanced Trees</h2>
<p>A Binary Search Tree (BST) is incredibly efficient <em>if it stays balanced</em>. But if you insert data that is already sorted (e.g., 1, 2, 3, 4, 5), your BST turns into a straight line—effectively becoming a Linked List! In this "unbalanced" state, searching takes <strong>O(n)</strong> time instead of <strong>O(log n)</strong>. <strong>Balanced Trees</strong> are self-correcting data structures that detect this imbalance and automatically restructure themselves to keep the height logarithmic, guaranteeing peak performance.</p>
<pre><code class="language-python"># Unbalanced Tree (Degenerate)
# 1 -> 2 -> 3 -> 4 -> 5 (Height = 5)
# Balanced Tree
# 3
# / \
# 2 4 (Height = 2)</code></pre>
<div style="text-align: center; margin: 20px 0;">
<div style="display: inline-block; padding: 20px; border: 2px solid #ddd; background: #f9f9f9; border-radius: 8px;">
<img src="https://picsum.photos/seed/378370831/800/500" alt="Balance and stability representation">
</div>
</div>
</section>
<section>
<h2>1. AVL Trees: Strict Balance</h2>
<p>An <strong>AVL Tree</strong> is a self-balancing BST where the height difference between left and right subtrees (the "balance factor") can never be more than 1. If it exceeds this, the tree performs a <strong>Rotation</strong> (Left, Right, or Double Rotations) to restore balance. This strictness makes AVL trees perfect for scenarios where you need fast searching and don't modify the tree quite as often.</p>
<pre><code class="language-python"># AVL Rotation (Pseudo)
if balance_factor > 1:
if target < node.left.value:
return right_rotate(node)
else:
return left_right_rotate(node)</code></pre>
<div style="text-align: center; margin: 20px 0;">
<div style="display: inline-block; padding: 20px; border: 2px solid #ddd; background: #f9f9f9; border-radius: 8px;">
<img src="https://picsum.photos/seed/78370832/800/500" alt="Precise algorithmic logic and rotation visualization">
</div>
</div>
</section>
<section>
<h2>2. Red-Black Trees: Balanced by Rule</h2>
<p><strong>Red-Black Trees</strong> are slightly more relaxed than AVL trees. They assign a color (Red or Black) to each node and follow a set of rules to ensure the path from the root to any leaf is no more than twice as long as the path to any other leaf. They are more complex to implement but often perform better in scenarios involving many insertions and deletions.</p>
<pre><code class="language-python"># Red-Black Tree Rules
# 1. Every node is red or black.
# 2. The root is black.
# 3. Red nodes cannot have red children.</code></pre>
<div style="text-align: center; margin: 20px 0;">
<div style="display: inline-block; padding: 20px; border: 2px solid #ddd; background: #f9f9f9; border-radius: 8px;">
<img src="https://picsum.photos/seed/78370833/800/500" alt="Complex logic and structured data visualization">
</div>
</div>
</section>
<section>
<h2>Visual Learning: Video Tutorials</h2>
<p>Master balanced trees with these three videos:</p>
<div style="display: flex; gap: 20px; flex-wrap: wrap; margin-top: 20px;">
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>1. Why Balanced Trees Matter</strong><br>
<a href="https://www.youtube.com/watch?v=BBpAmxU_NQo" target="_blank">Watch on YouTube →</a>
<p><small>The problem of unbalanced trees and O(n) degradation.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>2. AVL Tree Rotations</strong><br>
<a href="https://www.youtube.com/watch?v=8hly31xKli0" target="_blank">Watch on YouTube →</a>
<p><small>Visualizing how trees rebalance themselves.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>3. Red-Black Tree Rules</strong><br>
<a href="https://www.youtube.com/watch?v=zg9ih6SVACc" target="_blank">Watch on YouTube →</a>
<p><small>Understand the color-coding rules for balance.</small></p>
</div>
</div>
</section>
<section>
<h2>Real-World Relationship: The Balanced Scale</h2>
<p>Think of a <strong>BST</strong> like a <strong>Scale for weighing objects</strong>. If you put all the weight on one side (unbalanced tree), the scale tips over and is useless. You need to keep the weight distributed evenly on both sides to keep the scale level (Balanced Tree). <strong>AVL trees</strong> are like a precision scale that triggers an automatic mechanism to shift counter-weights (Rotations) the moment it detects even a slight tilt. <strong>Red-Black trees</strong> are like a more sophisticated system that uses color-coded weights to ensure the scale remains stable even if you are constantly adding or removing objects.</p>
</section>
<section>
<h2>References & Additional Learning</h2>
<ul>
<li><a href="https://en.wikipedia.org/wiki/AVL_tree" target="_blank">Wikipedia: AVL Tree</a></li>
<li><a href="https://en.wikipedia.org/wiki/Red%E2%80%93black_tree" target="_blank">Wikipedia: Red-Black Tree</a></li>
<li><a href="https://www.geeksforgeeks.org/introduction-to-avl-tree/" target="_blank">GeeksforGeeks: AVL Intro</a></li>
<li><a href="https://visualgo.net/en/bst" target="_blank">Tool: Tree Visualization</a></li>
</ul>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="DSA2_4.html" style="text-decoration: none; color: #6c757d;">← Previous: Binary Search Trees</a>
<a href="#" data-file="DSA2_6.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Heaps →</a>
</div>
</footer>
</article>