{"record":{"id":"acfc266fb8afc84d","repo":"TheAlgorithms/Go","slug":"huffman-coding-hufftree-calling-method-with-emp","errorCode":null,"errorMessage":"huffman coding: HuffTree : calling method with empty list of symbol-frequency pairs","messagePattern":"huffman coding: HuffTree : calling method with empty list of symbol-frequency pairs","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compression/huffmancoding.go","lineNumber":37,"sourceCode":"type Node struct {\n\tleft   *Node\n\tright  *Node\n\tsymbol rune\n\tweight int\n}\n\n// A SymbolFreq is a pair of a symbol and its associated frequency.\ntype SymbolFreq struct {\n\tSymbol rune\n\tFreq   int\n}\n\n// HuffTree returns the root Node of the Huffman tree by compressing listfreq.\n// The compression produces the most optimal code lengths, provided listfreq is ordered,\n// i.e.: listfreq[i] <= listfreq[j], whenever i < j.\nfunc HuffTree(listfreq []SymbolFreq) (*Node, error) {\n\tif len(listfreq) < 1 {\n\t\treturn nil, fmt.Errorf(\"huffman coding: HuffTree : calling method with empty list of symbol-frequency pairs\")\n\t}\n\tq1 := make([]Node, len(listfreq))\n\tq2 := make([]Node, 0, len(listfreq))\n\tfor i, x := range listfreq { // after the loop, q1 is a slice of leaf nodes representing listfreq\n\t\tq1[i] = Node{left: nil, right: nil, symbol: x.Symbol, weight: x.Freq}\n\t}\n\t//loop invariant: q1, q2 are ordered by increasing weights\n\tfor len(q1)+len(q2) > 1 {\n\t\tvar node1, node2 Node\n\t\tnode1, q1, q2 = least(q1, q2)\n\t\tnode2, q1, q2 = least(q1, q2)\n\t\tnode := Node{left: &node1, right: &node2,\n\t\t\tsymbol: -1, weight: node1.weight + node2.weight}\n\t\tq2 = append(q2, node)\n\t}\n\tif len(q1) == 1 { // returns the remaining node in q1, q2\n\t\treturn &q1[0], nil\n\t}","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/compression/huffmancoding.go#L19-L55","documentation":"Raised by HuffTree when listfreq contains fewer than one SymbolFreq entry; building a Huffman tree requires at least one symbol, so an empty (or nil) slice cannot produce a root node. It is a generic input-size guard at the top of the method.","triggerScenarios":"Thrown at compression/huffmancoding.go:37 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass a non-empty []SymbolFreq slice to HuffTree","Check len(listfreq) > 0 before calling and construct a trivial single-node tree for one symbol"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}