Algebraic data types: Difference between revisions
Content added Content deleted
(C# changed output to look more like a tree) |
m (→{{header|REXX}}: added/changed whitespace and comments.) |
||
Line 1,803: | Line 1,803: | ||
The nodes used for this example are taken from the Wikipedia example at: |
The nodes used for this example are taken from the Wikipedia example at: |
||
[[https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#/media/File:Red-black_tree_example.svg red black tree, an example]] |
[[https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#/media/File:Red-black_tree_example.svg red black tree, an example]] |
||
<lang rexx>/*REXX pgm builds a red/black tree (with verification & validation), |
<lang rexx>/*REXX pgm builds a red/black tree (with verification & validation), balanced as needed.*/ |
||
parse arg nodes '/' insert /*obtain optional arguments from the CL*/ |
parse arg nodes '/' insert /*obtain optional arguments from the CL*/ |
||
if nodes='' then nodes = 13.8.17 8.1.11 17.15.25 1.6 25.22.27 |
if nodes='' then nodes = 13.8.17 8.1.11 17.15.25 1.6 25.22.27 /*default nodes. */ |
||
if insert='' then insert= 22.44 44.66 |
if insert='' then insert= 22.44 44.66 /* " inserts.*/ |
||
⚫ | |||
top=. |
|||
call Dnodes nodes /*define nodes, balance them as added. */ |
call Dnodes nodes /*define nodes, balance them as added. */ |
||
call Dnodes insert /*insert |
call Dnodes insert /*insert " " " " needed.*/ |
||
call Lnodes /*list the nodes (with |
call Lnodes /*list the nodes (with indentations). */ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
err: say; say '***error***: ' arg(1); say; exit 13 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Dnodes: arg $d; do j=1 for words($d); t= word($d, j) /*color: encoded into L. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
L.n= space(L.n a b) /*append to the level list*/ |
|||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Lnodes: |
Lnodes: do L=1 for maxL; w= length(maxL); rb= word('(red) (black)', 1+L//2) |
||
say "level:" right(L, w) left('', L+L) " ───► " rb ' ' L.L |
|||
end /*lev*/ |
|||
return |
return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Vnodes: arg $v; |
Vnodes: arg $v; do v=1 for words($v); y= word($v, v) |
||
if \datatype(y, 'W') then call err "node isn't a whole number: " y |
|||
y= y / 1 /*normalize Y int.: no LZ, dot*/ |
|||
if top==. then do; LO=y; top=y; HI=y; L.=; @.=; maxL=1; end |
|||
LO= min(LO, y); HI= max(HI, y) |
|||
if @.y\==. & @.y\=='' then call err "node is already defined: " y |
|||
end /*v*/ |
|||
return</lang> |
return</lang> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |