File:  [CSRG BSD Unix] / 43BSD / contrib / icon / samples / btrees.icn
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 16:12:55 2018 UTC (8 years, 1 month ago) by root
Branches: MAIN, BSD
CVS tags: HEAD, BSD43
BSD 4.3

#
#          B I N A R Y   T R E E S
#

#  This program accepts string representations of binary trees from
#  standard input.  It performs a tree walk and lists the leaves of
#  each tree.

record node(data,ltree,rtree)

procedure main()
   local line, tree
   while line := read() do {
      tree := tform(line)
      write("tree walk")
      every write(walk(tree))
      write("leaves")
      every write(leaves(tree))
      }
end

procedure tform(s)
   local value,left,right
   if /s then return
   s ? if value := tab(upto('(')) then {
      move(1)
      left := tab(bal(','))
      move(1)
      right := tab(bal(')'))
      return node(value,tform(left),tform(right))
      }
      else return node(s)
end

procedure walk(t)
   suspend walk(\t.ltree | \t.rtree)
   return t.data
end

procedure leaves(t)
   if not(\t.ltree | \t.rtree) then return t.data
   suspend leaves(\t.ltree | \t.rtree)
end

unix.superglobalmegacorp.com

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