树
Huffman树的权值
哈夫曼树
每次选择两个最小的结点,合成一个结点。
选择 n - 1 次,直到只剩下一个结点,即为Huffman树的跟结点。
//优先队列,默认是小根堆,这样写是大根堆
priority_queue<int, vector<int>, greater<int>> heap;
int get_huffmantree_weight(){
int res = 0;
while (heap.size() > 1) {
int x = heap.top(); heap.pop(); // 选第一个,弹出
int y = heap.top(); heap.pop(); // 选第二个,弹出
heap.push(x + y); // 作为新的结点加入
res += x + y;
}
return res;
}