huffman-ts
Version:
Huffman ts is an implementation of Huffman Algorithm in Typescript. It provides full compatibility with Huffman algorithm reference.
27 lines (22 loc) • 653 B
text/typescript
/**
* Huffman tree node type:
* 1. [not Leaf node] have left, right child, do not have value
* 2. [Leaf node] do not have left and right, have value
*/
import { HuffmanEncoded } from "./type"
export class TreeNode { // Hu left and right node will be Leaf
left: TreeNode | null = null
right:TreeNode | null = null
value: string | null = null
constructor() {
}
isLeaf() {
return this.left === null && this.right === null
}
encode(): HuffmanEncoded {
if (this.value) {
return this.value
}
return [(<TreeNode>this.left).encode(), (<TreeNode>this.right).encode()]
}
}