539 priority_queue<huff_tree *, vector<huff_tree *>,
Compare> pq;
540 for(
int i = 0; i < n; i++) {
544 while((n - 1) % (k - 1)) {
545 pq.push(
new huff_tree(0, 1, k));
548 while(pq.size() > 1) {
549 huff_tree *ht =
new huff_tree(0, 0, k);
550 for(
int i = 0; i < k && !pq.empty(); i++) {
551 huff_tree *least = pq.top();
552 ht->height = max(ht->height, least->height + 1);
553 ht->children[i] = least;
554 ht->val += least->val;
559 huff_tree *
root = pq.top();
560 queue<pair<huff_tree *, u_int64_t>> q;
561 q.push(make_pair(
root, 0));
562 u_int64_t min_level = 0;
565 auto [ht, level] = q.front();
568 for(
int i = 0; i < k; i++) {
569 if(ht->children[i] !=
nullptr) {
571 q.push(make_pair(ht->children[i], level + 1));
575 sum += ht->
val * level;
576 min_level = max(min_level, level);
580 << min_level << endl;