一句话:给出 Garsia–Wachs 算法的纯函数式实现,在保持最优叶树构造的同时达到 O(n log n) 时间。
论文讨论 Garsia–Wachs 算法:给定输入序列,构造一棵叶子顺序恰好等于该序列且总代价最小的二叉叶树。该问题与 Huffman 编码类似,但额外要求叶子的排列固定,不能任意置换。作者给出该算法的纯函数式实现,并证明其运行时间为 O(n log n),即对长度为 n 的输入只需线性对数步,与 Huffman 编码的时间界相同。该工作展示了如何以纯函数式风格有效地实现这一最优树构造算法。
The Garsia–Wachs algorithm is an algorithm for building a binary leaf tree whose cost is as small as possible. The problem and the algorithm are described in more detail below, but the task is essentially the same as that of building a Huffman coding tree with the added constraint that the fringe of the tree has to be exactly the given list of inputs (in Huffman coding, the fringe of the tree can be any permutation of the input). As we will show below, the Garsia–Wachs algorithm can be implemented with a linearithmic running time—a running time of O ( n log n ) steps for an input of length n , the same time bound as for Huffman coding.