Skip to content

Commit 06fdff2

Browse files
committed
common programming operations
- defining custom comparison logic - heap manipulation
1 parent a5f9c06 commit 06fdff2

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

content/Programming/Common Programming Operations.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tags:
88
- cpp
99
- python
1010
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
1212
References:
1313
draft:
1414
description:
@@ -39,3 +39,85 @@ sort(sorted.begin(), sorted.end());
3939
4040
- **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)
4141
- 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

Comments
 (0)