RosettaCodeData/Task/Tree-datastructures/11l/tree-datastructures.11l

75 lines
1.8 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

T NNode
String value
[NNode] children
F (value)
.value = value
F add(node)
.children.append(node)
F.const to_str(depth) -> String
V result = ( * depth)(.value)"\n"
L(child) .children
result = child.to_str(depth + 1)
R result
F String()
R .to_str(0)
T INode = (String value, Int level)
F to_indented(node)
[INode] result
F add_node(NNode node, Int level) -> Void
@result.append(INode(node.value, level))
L(child) node.children
@add_node(child, level + 1)
add_node(node, 0)
R result
F to_nested(tree)
[NNode] stack
V nnode = NNode(tree[0].value)
L(i) 1 .< tree.len
V inode = tree[i]
I inode.level > stack.len
stack.append(nnode)
E I inode.level == stack.len
stack.last.children.append(nnode)
E
L inode.level < stack.len
stack.last.children.append(nnode)
nnode = stack.pop()
stack.last.children.append(nnode)
nnode = NNode(inode.value)
L stack.len > 0
stack.last.children.append(nnode)
nnode = stack.pop()
R nnode
print(Displaying tree built using nested structure:)
V nestedTree = NNode(RosettaCode)
V rocks = NNode(rocks)
rocks.add(NNode(code))
rocks.add(NNode(comparison))
rocks.add(NNode(wiki))
V mocks = NNode(mocks)
mocks.add(NNode(trolling))
nestedTree.add(rocks)
nestedTree.add(mocks)
print(nestedTree)
print(Displaying tree converted to indented structure:)
V indentedTree = to_indented(nestedTree)
L(node) indentedTree
print((node.level) (node.value))
print()
print(Displaying tree converted back to nested structure:)
print(to_nested(indentedTree))
print(Are they equal? (I String(nestedTree) == String(to_nested(indentedTree)) {yes} E no))