这是我在python中实现的MinHeap和MaxHeap。这使用比较器来反转MaxHeap中的存储顺序
import heapq
class MinHeap:
def __init__(self):
self.heap = []
def push(self, item):
heapq.heappush(self.heap, item)
def pop(self):
return heapq.heappop(self.heap)
def peek(self):
return self.heap[0]
def __getitem__(self, item):
return self.heap[item]
def __len__(self):
return len(self.heap)
class MaxHeap(MinHeap):
def push(self, item):
heapq.heappush(self.heap, Comparator(item))
def pop(self):
return heapq.heappop(self.heap)
def peek(self):
return self.heap[0]
def __getitem__(self, i):
return self.heap[i].val
class Comparator:
def __init__(self, val):
self.val = val
def __lt__(self, other):
return self.val > other
def __eq__(self, other):
return self.val == other
if __name__ == '__main__':
max_heap = MaxHeap()
max_heap.push(12)
max_heap.push(3)
max_heap.push(17)
print(max_heap.pop())
MinHeap似乎工作正常,但是MaxHeap会抛出以下错误。
<__main__.Comparator object at 0x10a5c1080>
我似乎很难理解我在这里做错了什么。有人可以帮我这个。
答案 0 :(得分:2)
我已将__repr__
和__gt__
方法添加到您的Comparator
类,因此代码现已运行,Comparator
个实例会在val
个实例显示Comparator
时打印。
重要的是让这些比较方法在两个MaxHeap
个实例之间正确进行比较。
您会注意到我已从MinHeap
中删除了大部分方法。它们不是必需的,因为从MaxHeap
继承的方法可以正常工作。您可能希望将此版本恢复为def __getitem__(self, i):
return self.heap[i].val
MaxHeap
取决于您打算如何使用import heapq
class MinHeap:
def __init__(self):
self.heap = []
def push(self, item):
heapq.heappush(self.heap, item)
def pop(self):
return heapq.heappop(self.heap)
def peek(self):
return self.heap[0]
def __getitem__(self, item):
return self.heap[item]
def __len__(self):
return len(self.heap)
class MaxHeap(MinHeap):
def push(self, item):
heapq.heappush(self.heap, Comparator(item))
class Comparator:
def __init__(self, val):
self.val = val
def __lt__(self, other):
return self.val > other.val
def __eq__(self, other):
return self.val == other.val
def __repr__(self):
return repr(self.val)
if __name__ == '__main__':
max_heap = MaxHeap()
max_heap.push(12)
max_heap.push(3)
max_heap.push(17)
while True:
try:
print(max_heap.pop())
except IndexError:
# The heap's empty, bail out
break
。
17
12
3
<强>输出强>
Comparator
给Comparator
全套丰富的比较方法可能是一个好主意。它们不需要使上述代码有效,但它们将使def __lt__(self, other):
return self.val > other.val
def __le__(self, other):
return self.val >= other.val
def __gt__(self, other):
return self.val < other.val
def __ge__(self, other):
return self.val <= other.val
def __eq__(self, other):
return self.val == other.val
def __ne__(self, other):
return self.val != other.val
实例更加灵活。所以如果你想要它们,这里它们是:
const express = require('express');
const mongoose = require('mongoose');
const Post = require('./models/Post');
const keys = require('./config/keys');
const path = require('path');
mongoose.connect(keys.mongoURI);
const app = express();
app.use(express.static(path.join(__dirname, '../react-app/build')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../react-app/build',
'index.html'));
});
app.get('/posts', (req, res) => {
Post.find({}, (err, posts) => {
if(err) {
console.log(err);
res.sendStatus(500);
} else {
res.send(posts)
}
})
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`App listening on port ${PORT}`));