Same fringe: Difference between revisions

→‎{{header|Scheme}}: reword & example
(→‎version 1.1: +Scheme version)
(→‎{{header|Scheme}}: reword & example)
Line 1,768:
=={{header|Scheme}}==
 
Descend provides a list, or stack, of the firstleftmost ''unvisited'' nodes at each level of the tree. Two such lists are used as lifo stack cursors to keep track of the remaining tree nodes. The main routineloop compares the top item of each list (ie the leftmost remaining node) and breaks returningwith ''false'' if different, or calls Ascend to update the lists,. withUpdating helpmay fromrequire calling Descend again if more unvisited left-nodes are found. If the end of eachboth listlists is reached simultaneously, and therefore eachthe tree,end isof reachedboth trees, ''true'' is returned.
 
<lang Scheme>; binary tree helpers from "Structure and Interpretation of Computer Programs" 2.3.3
Line 1,801:
(cond
((and (null? l1) (null? l2)) #t)
((=eq? (entry (car l1)) (entry (car l2)))
(next (ascend l1) (ascend l2)))
(else #f))))</lang>
 
{{out}}
<pre>> (same-fringe? (list 1 '() (list 2 '() (list 3 '() '()))) (list 3 (list 2 (list 1 '() '()) '()) '()))
#t</pre>
 
=={{header|Tcl}}==
Anonymous user