Same fringe: Difference between revisions

→‎version 1.1: comgined two subroutines into one, changed/added comments, used cardinal numbers instead of ordinal numbers for internal tree identifiers.
m (→‎version 1.1: removed OVERFLOW from the PRE html tagm, indented and changed section header comments.)
(→‎version 1.1: comgined two subroutines into one, changed/added comments, used cardinal numbers instead of ordinal numbers for internal tree identifiers.)
Line 1,699:
<br><br>This version has:
:::* elided a subroutine
:::* elided superfluous &nbsp; '''do ── end''' &nbsp; groups
:::* elided some stemmed array tails
:::* elided some REXX variables &nbsp; (lvl, debug, ···)
:::* simplified some stem names
:::* displays the tree &nbsp; (as an ASCII display)
:::* changed ''tree'' names so as to not conflict with ''leaf'' names
:::* uses non-case sensitive tree names
:::* used boolean based variables as ''logicals''
:::* expanded message texts
:::* streamlinedcombined thesubroutines &nbsp; ''make_tree'attleft''' &nbsp; subroutineand &nbsp; '''attright''' &nbsp; into one
:::* streamlined the &nbsp; '''make_tree''' &nbsp; subroutine
<lang rexx>/*REXX pgmprogram examines the leaves of two binary trees. (shown Treebelow). used is as above.*/
_=left('',28); say _ ' A A '
say _ ' / \ ◄────1st tree / \ '
Line 1,721 ⟶ 1,722:
say; #=0 /*#: # of leaves. */
parse var # done. 1 node. /*set all these variables to zero*/
call make_tree '1 /*define tree number 1 (1st' tree)*/
call make_tree '2 /* " " " 2 (2nd' " )*/
z1=root.1st1; L1=node.1st1.z1; done.1st1.z1=1 /*L1 is a leaf on 1st tree #1. */
z2=z1; L2=node.2nd2.z2; done.2nd2.z2=1 /*L2 " " " " 2nd " #2. */
 
do # %2 2 /*loop for the number of leaves. */
if L1==L2 then do
if L1==0 then call sayX 'The trees are equal.'
say ' The ' L1 " leaf is identical in both trees."
do until \done.1st1.z1
z1=go_next(z1,'1st'1); L1=node.1st1.z1
end
done.1st1.z1=1
do until \done.2nd2.z2
z2=go_next(z2,'2nd'2); L2=node.2nd2.z2
end
done.2nd2.z2=1
end
else select
when L1==0 then call sayX L2 'exceeds leaves in 1st tree'
when L2==0 then call sayX L1 'exceeds leaves in 2nd tree'
otherwise call sayX 'A difference is: ' L1 '¬=' L2
end /*select*/
end /*# % 2*/
exit
/*──────────────────────────────────GO_NEXT subroutine──────────────────*/
Line 1,763 ⟶ 1,764:
return next /*the next node (or 0, if done).*/
/*──────────────────────────────────MAKE_NODE subroutine────────────────*/
make_node: parse arg name,t; # = #+1 /*make a new node/branch on tree.*/
q = node.t.0 + 1; ; node.t.q = name; ; node.t.q._dad = 0
node.t.q._Lson = 0 ; node.t.q._Rson = 0 ; node.t.0 = q
return q /*number of the node just created*/
/*──────────────────────────────────MAKE_TREE subroutine────────────────*/
make_tree: procedure expose node. root. #; arg tree /*build a tree.*/
if tree=='1ST'1 then hhh='H'
else hhh='δ' /*the odd duck in the whole tree.*/
if tree=='1ST' then hhh='H'
a=make_node('A',tree); root.tree=a
b=make_node('B',tree); call sonLson 'L',b,a,tree
c=make_node('C',tree); call sonRson 'R',c,a,tree
d=make_node('D',tree); call sonLson 'L',d,b,tree
e=make_node('E',tree); call sonRson 'R',e,b,tree
f=make_node('F',tree); call sonLson 'L',f,c,tree
g=make_node('G',tree); call sonLson 'L',g,d,tree
/*quacks like a /*quackduck?*/ h=make_node(hhh,tree); call sonLson 'L',h,f,tree
i=make_node('I',tree); call sonRson 'R',i,f,tree
return
/*──────────────────────────────────SAYX subroutine─────────────────────*/
sayX: say; say arg(1); say; exit /*tell msg &and exit.*/
/*──────────────────────────────────SON subroutine──────────────────────*/
/*──────────────────────────────────SONL subroutine─────────────────────*/
sonLson: procedure expose node.; parse arg ?,son,dad,t; /*build left son.LR = */'_'?"SON"
node.t.son._dad=dad; q=node.t.dad._LsonLR /*define which son [↑] */
if q\==0 then do; node.t.q._dad=son; node.t.son._LsonLR=q; end
node.t.dad._LsonLR=son
if ?=='R' then if node.t.dad._LsonLR>0 then node.t.le._brother=node.t.dad._RsonLR
return
/*──────────────────────────────────SONR subroutine─────────────────────*/
sonR: procedure expose node.; parse arg son,dad,t /*build right son.*/
node.t.son._dad=dad; q=node.t.dad._Rson
if q\==0 then do; node.t.q._dad=son; node.t.son._Rson=q; end
node.t.dad._Rson=son
if node.t.dad._Lson>0 then node.t.le._brother=node.t.dad._Rson
return</lang>
'''output'''