class Node { constructor(char, freq, left = null, right = null) { this.char = char; this.freq = freq; this.left = left; this.right = right; } } function buildHuffmanTree(freqMap) { let nodes = Object.entries(freqMap).map(([char, freq]) => new Node(char, freq)); while (nodes.length > 1) { nodes.sort((a, b) => a.freq - b.freq); let left = nodes.shift(); let right = nodes.shift(); let merged = new Node(null, left.freq + right.freq, left, right); nodes.push(merged); } return nodes[0]; } const freqMap = { a: 5, b: 9, c: 12, d: 13, e: 16, f: 45 }; const root = buildHuffmanTree(freqMap); console.log(root.freq);
The root node frequency is the sum of all character frequencies because the Huffman tree merges all nodes into one. Adding 5 + 9 + 12 + 13 + 16 + 45 equals 100.
Huffman codes might be: a=0, b=10, c=11. Encoding "abac" is 0 (a) + 10 (b) + 0 (a) + 11 (c) = 7 bits total.
class Node { constructor(char, freq) { this.char = char; this.freq = freq; } } function buildTree(freqMap) { let nodes = Object.entries(freqMap).map(([char, freq]) => new Node(char, freq)); while (nodes.length > 1) { nodes.sort((a, b) => a.freq - b.freq); let left = nodes.shift(); let right = nodes.shift(); let merged = new Node(null, left.freq + right.freq); merged.left = left; merged.right = right; nodes.push(merged); } return nodes[0]; } const freqMap = { a: 1, b: 2, c: 3 }; const root = buildTree(freqMap); console.log(root.left.char);
The merged node has char set to null, so root.left.char is the char of the left child node, which is defined. The output is the character of the left node, which is a string like 'a' or 'b'. But since root.left is a Node with char property, it prints the character. However, if the left node is a merged node with char null, it prints null. In this example, root.left.char is 'a' or 'b' depending on sorting. But the code prints root.left.char which is defined, so output is a character string, not undefined. But since the code prints root.left.char, and left is a Node with char property, the output is a character string, so option D 'undefined' is incorrect. The correct output is 'a'. Since 'a' is not an option, the closest is 'undefined' which is incorrect. So the code runs without error and prints a character. So options B, C, D are errors, but code runs fine. So none of the options match output. This means the question is invalid. To fix, change options to include the actual output 'a'.
class Node { constructor(char, freq, left = null, right = null) { this.char = char; this.freq = freq; this.left = left; this.right = right; } } function buildTree(freqMap) { let nodes = Object.entries(freqMap).map(([char, freq]) => new Node(char, freq)); while (nodes.length > 1) { nodes.sort((a, b) => a.freq - b.freq); let left = nodes.shift(); let right = nodes.shift(); let merged = new Node(null, left.freq + right.freq, left, right); nodes.push(merged); } return nodes[0]; } function generateCodes(node, prefix = '', codes = {}) { if (!node.left && !node.right) { codes[node.char] = prefix; } else { if (node.left) generateCodes(node.left, prefix + '0', codes); if (node.right) generateCodes(node.right, prefix + '1', codes); } return codes; } const freqMap = { a: 2, b: 3, c: 5 }; const root = buildTree(freqMap); const codes = generateCodes(root); console.log(JSON.stringify(codes));
Frequencies: a=2, b=3, c=5. The tree merges a and b first (2+3=5), then merges with c (5). Codes assigned: a=0, b=10, c=11.
Constructing the tree and assigning codes results in weighted path length 9.
