Annotation of 43BSD/contrib/icon/samples/btrees.icn, revision 1.1.1.1

1.1       root        1: #
                      2: #          B I N A R Y   T R E E S
                      3: #
                      4: 
                      5: #  This program accepts string representations of binary trees from
                      6: #  standard input.  It performs a tree walk and lists the leaves of
                      7: #  each tree.
                      8: 
                      9: record node(data,ltree,rtree)
                     10: 
                     11: procedure main()
                     12:    local line, tree
                     13:    while line := read() do {
                     14:       tree := tform(line)
                     15:       write("tree walk")
                     16:       every write(walk(tree))
                     17:       write("leaves")
                     18:       every write(leaves(tree))
                     19:       }
                     20: end
                     21: 
                     22: procedure tform(s)
                     23:    local value,left,right
                     24:    if /s then return
                     25:    s ? if value := tab(upto('(')) then {
                     26:       move(1)
                     27:       left := tab(bal(','))
                     28:       move(1)
                     29:       right := tab(bal(')'))
                     30:       return node(value,tform(left),tform(right))
                     31:       }
                     32:       else return node(s)
                     33: end
                     34: 
                     35: procedure walk(t)
                     36:    suspend walk(\t.ltree | \t.rtree)
                     37:    return t.data
                     38: end
                     39: 
                     40: procedure leaves(t)
                     41:    if not(\t.ltree | \t.rtree) then return t.data
                     42:    suspend leaves(\t.ltree | \t.rtree)
                     43: end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.