Same fringe: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Combine into a single file for ease of testing) |
(→version 1.1: changed some variable names, aligned statements, elided unreferenced variables, added/changed comments and whitespace, used a template for the output section.) |
||
Line 2,194: | Line 2,194: | ||
===version 1.1=== |
===version 1.1=== |
||
This REXX example is a |
This REXX example is a re─written program that mimics the first version (above). |
||
This version has: |
|||
:::* elided a subroutine |
|||
:::* |
:::* elided a subroutine |
||
:::* elided |
:::* elided superfluous '''do ── end''' groups |
||
:::* elided some |
:::* elided some stemmed array tails |
||
:::* elided some REXX variables (LVL, DEBUG, ···) |
|||
:::* simplified some stem names |
|||
:::* |
:::* simplified some stem names |
||
:::* displays the tree (as an ASCII display) |
|||
:::* changed '' |
:::* changed ''TREE'' names so as to not conflict with ''LEAF'' names |
||
:::* uses |
:::* uses non─case sensitive tree names |
||
:::* used boolean based variables as ''logicals'' |
:::* used boolean based variables as ''logicals'' |
||
:::* expanded message texts |
:::* expanded message texts |
||
:::* combined subroutines '''attleft''' and '''attright''' into one |
|||
:::* |
:::* combined subroutines '''ATTLEFT''' and '''ATTRIGHT''' into one |
||
:::* streamlined the '''MAKE_TREE''' subroutine |
|||
<lang rexx>/*REXX |
<lang rexx>/*REXX pgm examines the leaves of 2 binary trees (as shown below), and finds inequities.*/ |
||
⚫ | |||
_=left('', 28); say _ " A A " |
|||
say _ " / \ ◄════1st tree / \ " |
|||
say _ " / |
say _ " / \ / \ " |
||
say _ " |
say _ " / \ / \ " |
||
say _ " |
say _ " B C B C " |
||
say _ " / \ / 2nd tree════► / \ / " |
|||
say _ " |
say _ " D E F D E F " |
||
say _ " |
say _ " / / \ / / \ " |
||
⚫ | |||
say |
say |
||
#=0; done.=0; |
#=0; done.=0; @.=0 /*initialize: #, leaves, DONE., nodes*/ |
||
call make_tree 1 /*define tree number 1 (1st tree)*/ |
call make_tree 1 /*define tree number 1 (the 1st tree).*/ |
||
call make_tree 2 /* " " " 2 (2nd " )*/ |
call make_tree 2 /* " " " 2 (the 2nd " ).*/ |
||
z1=root.1; L1= |
z1=root.1; L1=@.1.z1; done.1.z1=1 /*L1: is a leaf on tree number 1. */ |
||
z2=z1; L2= |
z2=z1; L2=@.2.z2; done.2.z2=1 /*L2: " " " " " " 2. */ |
||
do # % 2 /*loop for the number of leaves. |
do # % 2 /*loop for the number of (tree) leaves.*/ |
||
if L1==L2 then do |
if L1==L2 then do |
||
if L1==0 then call sayX 'The trees are equal.' |
if L1==0 then call sayX 'The trees are equal.' |
||
say ' The ' L1 " leaf is identical in both trees." |
say ' The ' L1 " leaf is identical in both trees." |
||
do until \done.1.z1; z1=go_next(z1, 1); L1=@.1.z1; end |
|||
z1=go_next(z1,1); L1=node.1.z1 |
|||
⚫ | |||
done.1.z1=1 |
done.1.z1=1 |
||
do until \done.2.z2; z2=go_next(z2, 2); L2=@.2.z2; end |
|||
z2=go_next(z2,2); L2=node.2.z2 |
|||
end |
|||
done.2.z2=1 |
done.2.z2=1 |
||
end |
end |
||
else select |
else select |
||
when L1==0 then call sayX L2 'exceeds leaves in 1st tree' |
when L1==0 then call sayX L2 'exceeds leaves in 1st tree' |
||
when L2==0 then call sayX L1 'exceeds leaves in 2nd tree' |
when L2==0 then call sayX L1 'exceeds leaves in 2nd tree' |
||
otherwise call sayX 'A difference is: ' L1 '¬=' L2 |
otherwise call sayX 'A difference is: ' L1 '¬=' L2 |
||
end /*select*/ |
end /*select*/ |
||
end /*# % 2*/ |
end /*# % 2*/ |
||
exit |
exit |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────GO_NEXT subroutine──────────────────*/ |
|||
go_next: procedure expose |
go_next: procedure expose @.; arg q,t; next=. /*find next node in the tree. */ |
||
if @.t.q._Lson\==0 &, /*there a left branch in tree?*/ |
|||
next=. |
|||
@.t.q._Lson.vis==0 then do /*has this node been visited? */ |
|||
next= @.t.q._Lson /*point to the ──► next node.*/ |
|||
@.t.q._Lson.vis= 1 /*the leftside is completed. */ |
|||
end |
|||
if next==. &, |
|||
@.t.q._Rson\==0 &, /*there a right tree branch ? */ |
|||
if next==. &, |
|||
@.t.q._Rson.vis==0 then do /*has this node been visited? */ |
|||
next= @.t.q._Rson /*──► next node. */ |
|||
@.t.q._Rson.vis= 1 /*the rightside is completed. */ |
|||
end |
|||
if next==. then next= @.t.q._dad /*process the father node. */ |
|||
return next /*next node (or 0, if done).*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
return next /*the next node (or 0, if done).*/ |
|||
⚫ | |||
/*──────────────────────────────────MAKE_NODE subroutine────────────────*/ |
|||
@.t.q= name; @.t.q._Lson= 0; @.t.0= q |
|||
⚫ | |||
@.t.q._dad=0; @.t.q._Rson= 0; return q /*number of node just created.*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
node.t.q._Lson = 0 ; node.t.q._Rson = 0 ; node.t.0 = q |
|||
⚫ | |||
return q /*number of the node just created*/ |
|||
if tree==1 then hhh='H' /* [↓] must be a wood duck*/ |
|||
/*──────────────────────────────────MAKE_TREE subroutine────────────────*/ |
|||
else hhh='δ' /*the odd duck in the tree. */ |
|||
⚫ | |||
a=make_node('A', tree); root.tree=a |
|||
b=make_node('B', tree); call son 'L', b, a, tree |
|||
c=make_node('C', tree); call son 'R', c, a, tree |
|||
d=make_node('D', tree); call son 'L', d, b, tree |
|||
e=make_node('E', tree); call son 'R', e, b, tree |
|||
f=make_node('F', tree); call son 'L', f, c, tree |
|||
g=make_node('G', tree); call son 'L', g, d, tree |
|||
/*quacks like a duck?*/ h=make_node(hhh, tree); call son 'L', h, f, tree |
|||
i=make_node('I', tree); call son 'R', i, f, tree |
|||
return |
|||
/*quacks like a duck?*/ h=make_node(hhh, tree); call son 'L', h,f,tree |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
i=make_node('I', tree); call son 'R', i,f,tree |
|||
⚫ | |||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────SAYX subroutine─────────────────────*/ |
|||
⚫ | |||
⚫ | |||
@.t.son._dad= dad; q= @.t.dad.LR /* [↓] define which son. */ |
|||
/*──────────────────────────────────SON subroutine──────────────────────*/ |
|||
⚫ | |||
⚫ | |||
@.t.dad.LR= son; return</lang> |
|||
⚫ | |||
if q\==0 then do; node.t.q._dad=son; node.t.son.LR=q; end |
|||
node.t.dad.LR=son |
|||
if ?=='R' & node.t.dad.LR>0 then node.t.le._brother=node.t.dad.LR |
|||
return</lang> |
|||
⚫ | |||
<pre> |
<pre> |
||
A A |
A A |
||
/ \ |
/ \ ◄════1st tree / \ |
||
/ \ / \ |
/ \ / \ |
||
/ \ / \ |
/ \ / \ |
||
B C B C |
B C B C |
||
/ \ / 2nd |
/ \ / 2nd tree════► / \ / |
||
D E F D E F |
D E F D E F |
||
/ / \ / / \ |
/ / \ / / \ |
||
G H I G δ I |
G H I G δ I |
||
The A leaf is identical in both trees. |
The A leaf is identical in both trees. |