|
8 | 8 | - cpp |
9 | 9 | - python |
10 | 10 | Creation Date: 2024-12-14, 20:31 |
11 | | -Last Date: 2024-12-14T21:12:34+08:00 |
| 11 | +Last Date: 2024-12-16T22:14:18+08:00 |
12 | 12 | References: |
13 | 13 | draft: |
14 | 14 | description: |
@@ -39,3 +39,85 @@ sort(sorted.begin(), sorted.end()); |
39 | 39 |
|
40 | 40 | - **C++ `std::sort()`** works directly on a `std::string` without needing to convert it to a character array, because a `std::string` in C++ is essentially a sequence of characters (similar to a char array) |
41 | 41 | - Just like in Java, `std::sort()` returns `void` |
| 42 | +
|
| 43 | +## Defining Custom Comparison Logic |
| 44 | +--- |
| 45 | +```java title="Java" |
| 46 | +class Node implements Comparable<Node> { |
| 47 | + public int freq; |
| 48 | + |
| 49 | + @Override |
| 50 | + public int compareTo(Node other) { |
| 51 | + return Integer.compare(other.freq, this.freq); |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +- The comparator logic above implements a **descending order**. To achieve ascending order, swap `other.freq` and `this.freq` |
| 57 | +- For more details, refer to [[Java Comparison]] |
| 58 | + |
| 59 | +```python title="Python" |
| 60 | +class Node: |
| 61 | + def __init__(self, val, freq): |
| 62 | + self.freq = freq |
| 63 | + |
| 64 | + def __lt__(self, other): |
| 65 | + return other.freq < self.freq |
| 66 | +``` |
| 67 | + |
| 68 | +- The comparator logic above implements a **descending order**. To achieve ascending order, swap `other.freq` and `self.freq` |
| 69 | + |
| 70 | +```cpp title="C++" |
| 71 | +class Node { |
| 72 | +public: |
| 73 | + int freq; |
| 74 | + |
| 75 | + Node(int freq) { |
| 76 | + this->freq = freq; |
| 77 | + } |
| 78 | + |
| 79 | + bool operator<(const Node& other) { |
| 80 | + return other.freq > this->freq; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +- The comparator logic above implements a **descending order**. To achieve ascending order, swap `other.freq` and `self->freq` |
| 86 | +## Heap Manipulation |
| 87 | +--- |
| 88 | +```java title="Java" |
| 89 | +// Create a min-heap |
| 90 | +PriorityQueue<Integer> minHeap = new PriorityQueue<>(); |
| 91 | + |
| 92 | +// Add a new element to the heap |
| 93 | +minHeap.add(5); |
| 94 | + |
| 95 | +// Remove and return the smallest element from the heap |
| 96 | +minHeap.poll(); |
| 97 | +``` |
| 98 | + |
| 99 | +```python title="Python" |
| 100 | +# # Create a list to serve as a container for the min-heap |
| 101 | +minHeap = [] |
| 102 | +# Convert the list with elements into a min-heap |
| 103 | +heapq.heapify(minHeap) |
| 104 | + |
| 105 | +# Add a new element to the heap |
| 106 | +heapq.heappush(minHeap, 5) |
| 107 | + |
| 108 | +# Remove and return the smallest element from the heap |
| 109 | +heapq.heappop(minHeap) |
| 110 | +``` |
| 111 | + |
| 112 | +```cpp title="C++" |
| 113 | +// Create a min-heap |
| 114 | +priority_queue<int> minHeap; |
| 115 | + |
| 116 | +// Add Add a new element to the heap |
| 117 | +minHeap.push(5); |
| 118 | + |
| 119 | +// Remove the smallest element from the heap |
| 120 | +minHeap.pop(); |
| 121 | +// Return the smallest element from the heap |
| 122 | +minHeap.top().val; |
| 123 | +``` |
0 commit comments