-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA2_3.html
More file actions
96 lines (89 loc) · 6.31 KB
/
Copy pathDSA2_3.html
File metadata and controls
96 lines (89 loc) · 6.31 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
94
95
96
<article>
<h1>DSA II: Page 3 - Trees: Introduction</h1>
<section>
<h2>The Hierarchical Structure</h2>
<p>Unlike linear data structures (arrays, linked lists) where data is stored sequentially, a <strong>Tree</strong> is a <strong>hierarchical</strong> data structure. It consists of nodes connected by edges, starting from a single "root" node and branching downwards. Trees are ubiquitous in computer science because they perfectly model hierarchical relationships: file systems, HTML document object models (DOM), organization charts, and database indexing structures are all modeled as trees.</p>
<pre><code class="language-python"># A simple hierarchical structure representation
tree = {
'root': 'CEO',
'children': ['VP Engineering', 'VP Sales']
}</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://images.unsplash.com/photo-1474718723952-48d2a016108f?q=80&w=800&auto=format&fit=crop" alt="Abstract network and hierarchical nodes">
</div>
</div>
</section>
<section>
<h2>1. Tree Terminology</h2>
<p>To talk about trees, you must know the vocabulary:</p>
<ul>
<li><strong>Root:</strong> The topmost node of the tree.</li>
<li><strong>Parent/Child:</strong> A node is a parent to its children.</li>
<li><strong>Leaf:</strong> A node with no children (the bottom of a branch).</li>
<li><strong>Depth:</strong> The number of edges from the root to a node.</li>
<li><strong>Height:</strong> The number of edges from a node to the deepest leaf.</li>
</ul>
<pre><code class="language-python">class Node:
def __init__(self, value):
self.value = value
self.children = [] # List to hold references to child nodes</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://images.unsplash.com/photo-1416339306562-f3d12fefd36f?q=80&w=800&auto=format&fit=crop" alt="Connected nodes and data visualization">
</div>
</div>
</section>
<section>
<h2>2. Why Trees?</h2>
<p>Trees are much more efficient than linear lists for searching, inserting, and deleting data, especially when the data is structured hierarchically. By keeping the tree "balanced," we can perform searches in <strong>O(log n)</strong> time—exponentially faster than searching a linear list of 1,000,000 items.</p>
<pre><code class="language-python"># O(log n) vs O(n)
# In a balanced tree of 1,000,000 nodes,
# you only need ~20 steps to find any item!</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://images.unsplash.com/photo-1523476843875-43c2cb89aa85?q=80&w=800&auto=format&fit=crop" alt="Logic and efficiency in computing">
</div>
</div>
</section>
<section>
<h2>Visual Learning: Video Tutorials</h2>
<p>Master tree fundamentals 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. Intro to Tree Data Structures</strong><br>
<a href="https://www.youtube.com/watch?v=BBpAmxU_NQo" target="_blank">Watch on YouTube →</a>
<p><small>Hierarchical relationships and core terminology.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>2. Tree Traversal Basics</strong><br>
<a href="https://www.youtube.com/watch?v=8hly31xKli0" target="_blank">Watch on YouTube →</a>
<p><small>How to visit every node in a tree.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>3. Real-World Tree Examples</strong><br>
<a href="https://www.youtube.com/watch?v=zg9ih6SVACc" target="_blank">Watch on YouTube →</a>
<p><small>Where trees are used in the real world.</small></p>
</div>
</div>
</section>
<section>
<h2>Real-World Relationship: The Organizational Chart</h2>
<p>Think of a <strong>Tree</strong> like an <strong>Organizational Chart of a Company</strong>. The CEO is the <strong>Root</strong> node at the top. The CEO has direct reports (<strong>Children</strong>). Each of those reports may have their own direct reports, creating a hierarchy. The interns and entry-level employees at the bottom are the <strong>Leaves</strong>—they have no direct reports. This structure makes it easy to understand who reports to whom (parent/child relationship) and how high up in the company someone is (depth).</p>
</section>
<section>
<h2>References & Additional Learning</h2>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Tree_(data_structure)" target="_blank">Wikipedia: Tree Overview</a></li>
<li><a href="https://www.geeksforgeeks.org/introduction-to-tree-data-structure/" target="_blank">GeeksforGeeks: Tree Intro</a></li>
<li><a href="https://www.w3schools.com/dsa/dsa_trees.php" target="_blank">W3Schools: DSA Trees</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_2.html" style="text-decoration: none; color: #6c757d;">← Previous: Hash Tables</a>
<a href="#" data-file="DSA2_4.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Binary Search Trees →</a>
</div>
</footer>
</article>