Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -41,7 +41,7 @@ struct Fringe(T) {
|
|||
alias const(BinaryTreeNode!T)* BT;
|
||||
private Stack!BT stack;
|
||||
|
||||
pure nothrow invariant() {
|
||||
pure nothrow invariant {
|
||||
assert(stack.empty || isLeaf(stack.head));
|
||||
}
|
||||
|
||||
|
|
@ -49,9 +49,9 @@ struct Fringe(T) {
|
|||
if (t != null) {
|
||||
stack.push(t);
|
||||
if (!isLeaf(t)) {
|
||||
// Here invariant() doesn't hold.
|
||||
// invariant() isn't called for private methods.
|
||||
nextLeaf();
|
||||
// Here the invariant doesn't hold.
|
||||
// invariant isn't called for private methods.
|
||||
nextLeaf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
44
Task/Same-Fringe/D/same-fringe-3.d
Normal file
44
Task/Same-Fringe/D/same-fringe-3.d
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import std.stdio, std.concurrency, std.range, std.algorithm;
|
||||
|
||||
struct Node(T) {
|
||||
T data;
|
||||
Node* L, R;
|
||||
}
|
||||
|
||||
Generator!T fringe(T)(Node!T* t1) {
|
||||
return new typeof(return)({
|
||||
if (t1 != null) {
|
||||
if (t1.L == null && t1.R == null) // Is a leaf.
|
||||
yield(t1.data);
|
||||
else
|
||||
foreach (data; t1.L.fringe.chain(t1.R.fringe))
|
||||
yield(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bool sameFringe(T)(Node!T* t1, Node!T* t2) {
|
||||
return t1.fringe.equal(t2.fringe);
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias N = Node!int;
|
||||
|
||||
auto t1 = new N(10, new N(20, new N(30, new N(40), new N(50))));
|
||||
auto t2 = new N(1, new N(2, new N(3, new N(40), new N(50))));
|
||||
sameFringe(t1, t2).writeln;
|
||||
|
||||
auto t3 = new N(1, new N(2, new N(3, new N(40), new N(51))));
|
||||
sameFringe(t1, t3).writeln;
|
||||
|
||||
auto t4 = new N(1, new N(2, new N(3, new N(40))));
|
||||
sameFringe(t1, t4).writeln;
|
||||
|
||||
N* t5;
|
||||
sameFringe(t1, t5).writeln;
|
||||
sameFringe(t5, t5).writeln;
|
||||
|
||||
auto t6 = new N(2);
|
||||
auto t7 = new N(1, new N(2));
|
||||
sameFringe(t6, t7).writeln;
|
||||
}
|
||||
|
|
@ -1,87 +1,81 @@
|
|||
/*REXX pgm examines leaves of two binary trees. Tree used is as above.*/
|
||||
_=left('',28); say _ ' A A '
|
||||
say _ ' / \ ◄────1st tree / \ '
|
||||
say _ ' / \ / \ '
|
||||
say _ ' / \ / \ '
|
||||
say _ ' B C B C '
|
||||
say _ ' / \ / 2nd tree────► / \ / '
|
||||
say _ ' D E F D E F '
|
||||
say _ ' / / \ / / \ '
|
||||
say _ 'G H I G δ I '
|
||||
say; #=0 /*#: # of leaves. */
|
||||
parse var # done. 1 node. /*set all these variables to zero*/
|
||||
call make_tree '1st'
|
||||
call make_tree '2nd'
|
||||
z1=root.1st; L1=node.1st.z1; done.1st.z1=1 /*L1 is a leaf on 1st tree*/
|
||||
z2=z1; L2=node.2nd.z2; done.2nd.z2=1 /*L2 " " " " 2nd " */
|
||||
/*REXX program examines the leaves of two binary trees (as shown below).*/
|
||||
_=left('',28); say _ " A A "
|
||||
say _ " / \ ◄════1st tree / \ "
|
||||
say _ " / \ / \ "
|
||||
say _ " / \ / \ "
|
||||
say _ " B C B C "
|
||||
say _ " / \ / 2nd tree════► / \ / "
|
||||
say _ " D E F D E F "
|
||||
say _ " / / \ / / \ "
|
||||
say _ "G H I G δ I "
|
||||
say
|
||||
#=0; done.=0; node.=0 /*initialize # leaves,DONE.,NODE.*/
|
||||
call make_tree 1 /*define tree number 1 (1st tree)*/
|
||||
call make_tree 2 /* " " " 2 (2nd " )*/
|
||||
z1=root.1; L1=node.1.z1; done.1.z1=1 /*L1 is a leaf on tree number 1. */
|
||||
z2=z1; L2=node.2.z2; done.2.z2=1 /*L2 " " " " " " 2. */
|
||||
|
||||
do #%2 /*loop for the number of leaves. */
|
||||
do # % 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.1st.z1
|
||||
z1=go_next(z1,'1st'); L1=node.1st.z1
|
||||
end
|
||||
done.1st.z1=1
|
||||
do until \done.2nd.z2
|
||||
z2=go_next(z2,'2nd'); L2=node.2nd.z2
|
||||
end
|
||||
done.2nd.z2=1
|
||||
if L1==0 then call sayX 'The trees are equal.'
|
||||
say ' The ' L1 " leaf is identical in both trees."
|
||||
do until \done.1.z1
|
||||
z1=go_next(z1,1); L1=node.1.z1
|
||||
end
|
||||
done.1.z1=1
|
||||
do until \done.2.z2
|
||||
z2=go_next(z2,2); L2=node.2.z2
|
||||
end
|
||||
done.2.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
|
||||
otherwise call sayX 'A difference is: ' L1 '¬=' L2
|
||||
end /*select*/
|
||||
end /*#%2*/
|
||||
end /*# % 2*/
|
||||
exit
|
||||
/*──────────────────────────────────GO_NEXT subroutine──────────────────*/
|
||||
go_next: procedure expose node.; arg q,t /*find next node.*/
|
||||
next=0
|
||||
if node.t.q._Lson\==0 then /*is there a left branch in tree?*/
|
||||
if node.t.q._Lson.done==0 then do /*has this node been visited yet?*/
|
||||
next=node.t.q._Lson /*──► next node. */
|
||||
node.t.q._Lson.done=1 /*mark Lson done.*/
|
||||
end
|
||||
if next==0 then
|
||||
if node.t.q._Rson\==0 then /*is there a right tree branch ? */
|
||||
if node.t.q._Rson.done==0 then do /*has this node been visited yet?*/
|
||||
next=node.t.q._Rson /*──► next node*/
|
||||
node.t.q._Rson.done=1 /*mark Rson don*/
|
||||
end
|
||||
if next==0 then next=node.t.q._dad /*process the father node. */
|
||||
go_next: procedure expose node.; arg q,t /*find next node.*/
|
||||
next=.
|
||||
if node.t.q._Lson\==0 &, /*is there a left branch in tree?*/
|
||||
node.t.q._Lson.vis==0 then do /*has this node been visited yet?*/
|
||||
next=node.t.q._Lson /*──► next node. */
|
||||
node.t.q._Lson.vis=1 /*leftside done. */
|
||||
end
|
||||
if next==. &,
|
||||
node.t.q._Rson\==0 &, /*is there a right tree branch ? */
|
||||
node.t.q._Rson.vis==0 then do /*has this node been VISited yet?*/
|
||||
next=node.t.q._Rson /*──► next node. */
|
||||
node.t.q._Rson.vis=1 /*rightside done.*/
|
||||
end
|
||||
if next==. then next=node.t.q._dad /*process the father node. */
|
||||
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
|
||||
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.*/
|
||||
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 sonL b,a,tree
|
||||
c=make_node('C',tree); call sonR c,a,tree
|
||||
d=make_node('D',tree); call sonL d,b,tree
|
||||
e=make_node('E',tree); call sonR e,b,tree
|
||||
f=make_node('F',tree); call sonL f,c,tree
|
||||
g=make_node('G',tree); call sonL g,d,tree
|
||||
/*quack?*/ h=make_node(hhh,tree); call sonL h,f,tree
|
||||
i=make_node('I',tree); call sonR i,f,tree
|
||||
make_tree: procedure expose node. root. #; parse arg tree /*build trees*/
|
||||
if tree==1 then hhh='H' /* [↓] must be a wood duck*/
|
||||
else hhh='δ' /*the odd duck in the whole 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
|
||||
/*──────────────────────────────────SAYX subroutine─────────────────────*/
|
||||
sayX: say; say arg(1); say; exit /*tell msg & exit.*/
|
||||
/*──────────────────────────────────SONL subroutine─────────────────────*/
|
||||
sonL: procedure expose node.; parse arg son,dad,t /*build left son. */
|
||||
node.t.son._dad=dad; q=node.t.dad._Lson
|
||||
if q\==0 then do; node.t.q._dad=son; node.t.son._Lson=q; end
|
||||
node.t.dad._Lson=son
|
||||
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
|
||||
sayX: say; say arg(1); say; exit /*tell msg and exit.*/
|
||||
/*──────────────────────────────────SON subroutine──────────────────────*/
|
||||
son: procedure expose node.; parse arg ?,son,dad,t; LR = '_'?"SON"
|
||||
node.t.son._dad=dad; q=node.t.dad.LR /*define which son [↑] */
|
||||
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
|
||||
|
|
|
|||
35
Task/Same-Fringe/Scheme/same-fringe.ss
Normal file
35
Task/Same-Fringe/Scheme/same-fringe.ss
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
; binary tree helpers from "Structure and Interpretation of Computer Programs" 2.3.3
|
||||
(define (entry tree) (car tree))
|
||||
(define (left-branch tree) (cadr tree))
|
||||
(define (right-branch tree) (caddr tree))
|
||||
(define (make-tree entry left right)
|
||||
(list entry left right))
|
||||
|
||||
; returns a list of leftmost nodes from each level of the tree
|
||||
(define (descend tree ls)
|
||||
(if (null? (left-branch tree))
|
||||
(cons tree ls)
|
||||
(descend (left-branch tree) (cons tree ls))))
|
||||
|
||||
; updates the list to contain leftmost nodes from each remaining level
|
||||
(define (ascend ls)
|
||||
(cond
|
||||
((and (null? (cdr ls)) (null? (right-branch (car ls)))) '())
|
||||
((null? (right-branch (car ls))) (cdr ls))
|
||||
(else
|
||||
(let ((ls (cons (right-branch (car ls))
|
||||
(cdr ls))))
|
||||
(if (null? (left-branch (car ls)))
|
||||
ls
|
||||
(descend (left-branch (car ls)) ls))))))
|
||||
|
||||
; loops thru each list until the end (true) or nodes are unequal (false)
|
||||
(define (same-fringe? t1 t2)
|
||||
(let next ((l1 (descend t1 '()))
|
||||
(l2 (descend t2 '())))
|
||||
(cond
|
||||
((and (null? l1) (null? l2)) #t)
|
||||
((or (null? l1)
|
||||
(null? l2)
|
||||
(not (eq? (entry (car l1)) (entry (car l2))))) #f)
|
||||
(else (next (ascend l1) (ascend l2))))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue