Tree from nesting levels: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(Added XPL0 example.) |
||
Line 2,361: | Line 2,361: | ||
<pre> |
<pre> |
||
Same as iterative version. |
Same as iterative version. |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
A sentinel 0 is used to terminate the input arrays because XPL0 does not have built-in lists. |
|||
Note that [0] displays nothing. |
|||
<syntaxhighlight lang "XPL0">proc ShowTree(List); |
|||
int List, NestLevel, I; |
|||
[NestLevel:= 0; |
|||
for I:= 0 to -1>>1 do |
|||
[while List(I) < NestLevel do |
|||
[ChOut(0, ^]); NestLevel:= NestLevel-1]; |
|||
if List(I) = 0 then [CrLf(0); return]; |
|||
if I # 0 then Text(0, ", "); |
|||
while List(I) > NestLevel do |
|||
[ChOut(0, ^[); NestLevel:= NestLevel+1]; |
|||
IntOut(0, NestLevel); |
|||
]; |
|||
]; |
|||
[ShowTree([0]); |
|||
ShowTree([1, 2, 4, 0]); |
|||
ShowTree([3, 1, 3, 1, 0]); |
|||
ShowTree([1, 2, 3, 1, 0]); |
|||
ShowTree([3, 2, 1, 3, 0]); |
|||
ShowTree([3, 3, 3, 1, 1, 3, 3, 3, 0]); |
|||
ShowTree([1, 2, 4, 2, 2, 1, 0]); |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[1, [2, [[4]]]] |
|||
[[[3]], 1, [[3]], 1] |
|||
[1, [2, [3]], 1] |
|||
[[[3], 2], 1, [[3]]] |
|||
[[[3, 3, 3]], 1, 1, [[3, 3, 3]]] |
|||
[1, [2, [[4]], 2, 2], 1] |
|||
</pre> |
</pre> |