Anonymous user
Huffman coding: Difference between revisions
m
→{{header|Nim}}: Remove 'of RootObj' since we don't need inheritance. Comment formatting.
(Solve task in Nim) |
m (→{{header|Nim}}: Remove 'of RootObj' since we don't need inheritance. Comment formatting.) |
||
Line 3,225:
CodeSymbol = range[0..1]
HuffCode = seq[CodeSymbol]
Node = ref object
f: int
parent: Node
Line 3,235:
proc `<`(a: Node, b: Node): bool =
# For min operator
a.f < b.f
Line 3,242 ⟶ 3,243:
result &= $symbol
# Constructs a sequence of nodes which can be adopted▼
# Optional parent parameter can be set to ensure node will not adopt itself▼
proc freeChildList(tree: seq[Node], parent: Node = nil): seq[Node] =
▲ # Constructs a sequence of nodes which can be adopted
▲ # Optional parent parameter can be set to ensure node will not adopt itself
result = @[]
for node in tree:
Line 3,250 ⟶ 3,251:
result.add(node)
# Only call this proc when sure that parent has a free child slot▼
proc connect(parent: Node, child: Node) =
▲ # Only call this proc when sure that parent has a free child slot
child.parent = parent
parent.f += child.f
|