-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_SortedSet.cs
More file actions
104 lines (87 loc) · 5.91 KB
/
Copy path06_SortedSet.cs
File metadata and controls
104 lines (87 loc) · 5.91 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
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
namespace DataStructures
{
/// <summary>
/// Demonstrates the SortedSet<T> collection in C#.
/// A SortedSet maintains elements in sorted order and guarantees uniqueness.
/// It supports mathematical set operations and provides efficient range queries.
///
/// Internally implemented as a red-black tree.
/// Common use cases: leaderboards, priority sets, range queries,
/// unique sorted collections, set algebra.
/// </summary>
public class SortedSetExamples
{
// ─── Entry Point ───────────────────────────────────────────────
public static void Main()
{
Console.WriteLine("═══════════════════════════════════════════════");
Console.WriteLine(" C# SortedSet<T> Examples ");
Console.WriteLine("═══════════════════════════════════════════════\n");
// ── 1. Basic Operations ─────────────────────────────────────
Console.WriteLine("── Basic Add / Remove ─────────────────────────");
var scores = new SortedSet<int> { 85, 92, 78, 95, 88 };
Console.WriteLine($" Initial scores (auto-sorted): {string.Join(", ", scores)}");
scores.Add(70); // Add a new element
scores.Add(92); // Duplicate — ignored
Console.WriteLine($" After Add(70), Add(92): {string.Join(", ", scores)}");
scores.Remove(78);
Console.WriteLine($" After Remove(78): {string.Join(", ", scores)}");
Console.WriteLine($" Min: {scores.Min} | Max: {scores.Max}");
Console.WriteLine();
// ── 2. Mathematical Set Operations ──────────────────────────
Console.WriteLine("── Set Operations ─────────────────────────────");
var setA = new SortedSet<int> { 1, 2, 3, 4, 5 };
var setB = new SortedSet<int> { 3, 4, 5, 6, 7 };
Console.WriteLine($" Set A: {string.Join(", ", setA)}");
Console.WriteLine($" Set B: {string.Join(", ", setB)}");
// Union — all elements from both sets
var union = new SortedSet<int>(setA);
union.UnionWith(setB);
Console.WriteLine($" Union (A ∪ B) : {string.Join(", ", union)}");
// Intersection — elements common to both sets
var intersect = new SortedSet<int>(setA);
intersect.IntersectWith(setB);
Console.WriteLine($" Intersect (A ∩ B): {string.Join(", ", intersect)}");
// Symmetric Except — elements in A or B but not both
var symExcept = new SortedSet<int>(setA);
symExcept.SymmetricExceptWith(setB);
Console.WriteLine($" SymExcept (A △ B): {string.Join(", ", symExcept)}");
// Difference — elements in A that are not in B
var diff = new SortedSet<int>(setA);
diff.ExceptWith(setB);
Console.WriteLine($" Difference (A−B) : {string.Join(", ", diff)}");
Console.WriteLine();
// ── 3. Unique Elements from Random Data ─────────────────────
Console.WriteLine("── Extracting Unique Sorted Elements ──────────");
var random = new Random();
var numbers = new List<int>();
for (int i = 0; i < 15; i++)
{
numbers.Add(random.Next(1, 20));
}
Console.WriteLine($" Random list ({numbers.Count} items): {string.Join(", ", numbers)}");
var uniqueSorted = new SortedSet<int>(numbers);
Console.WriteLine($" Unique sorted ({uniqueSorted.Count} items): {string.Join(", ", uniqueSorted)}");
Console.WriteLine($" Duplicates removed: {numbers.Count - uniqueSorted.Count}");
Console.WriteLine();
// ── 4. Range Queries ────────────────────────────────────────
Console.WriteLine("── Range Queries ──────────────────────────────");
var data = new SortedSet<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
Console.WriteLine($" Full set: {string.Join(", ", data)}");
// GetViewBetween — returns a subset within a range
var range = data.GetViewBetween(15, 40);
Console.WriteLine($" Range [15..40]: {string.Join(", ", range)}");
// Subset checks
var small = new SortedSet<int> { 10, 20, 30 };
Console.WriteLine($" {{10,20,30}} IsSubsetOf data: {small.IsSubsetOf(data)}");
Console.WriteLine($" {{10,20,30}} Overlaps {{1,2,3}}: {small.Overlaps(new SortedSet<int> { 1, 2, 3 })}");
Console.WriteLine("\n═══════════════════════════════════════════════");
Console.WriteLine(" Complexity: Add O(log n) | Remove O(log n) | Contains O(log n)");
Console.WriteLine(" Iteration O(n) — always in sorted order");
Console.WriteLine(" Set operations: O(n + m)");
Console.WriteLine("═══════════════════════════════════════════════");
}
}
}