Lambda Calculus Trees.

home1 home2
and the


Trees are not built into the λ-interpreter (they could be) but they can be implemented using lists:

fork = lambda e. lambda L. lambda R. { tree ops }
leaf = lambda e. e::nil::nil::nil,
emptyTree   = nil,
isEmptyTree = lambda T. null T,

elt   = lambda T. hd T,
left  = lambda T. hd tl T,
right = lambda T. hd tl tl T

To perform infix traversal of a tree, producing a list of values:

infix = lambda T.
    { : Tree e -> List e,  O(|T|)-time }
  let rec inf = lambda inT. lambda op.
    if isEmptyTree inT then op
      inf (left inT) (elt inT::inf (right inT) op)
  in inf T nil

Note that function inf above, which does all the work of traversal, has an accumulating parameter, op.

A tree can also be traversed in breadth-first order. A queue, Q, of trees is used, subtrees being taken off the front of the queue and their children, if any, being added to the other end.

BFT = lambda T.  { : Tree e -> List e }
  let rec
    Q = T :: (traverse Q 1),

    traverse = lambda Q. lambda n.
    { n is remaining length of Q }
      if n = 0 then nil else
      let rec T1 = hd Q,
              Lf = left  T1,
              Rt = right T1,
            rest = traverse tl Q
      in if isEmptyTree Lf then
           if isEmptyTree Rt then rest (n-1)
           else Rt :: rest n
           Lf :: if isEmptyTree Rt then rest n
                 else Rt :: rest (n+1)

  in if isEmptyTree T then nil
     else map elt Q

Note that the queue data-structure, Q, and the function, traverse, that builds Q are mutually recursive, making this a circular program (Bird 1984, Allison 1989, 1993).

A binary search tree can be built from a given list of values:

BST = lambda L.
{ binary search tree of L; both may be infinite }
  if null L then emptyTree
  else let hdL = hd L, tlL = tl L
       in fork hdL
               (BST (filter (gt hdL) tlL))
               (BST (filter (lt hdL) tlL))

An element can be added to an existing binary search tree:

BSTadd = lambda T. lambda e.
     { : Tree e -> e -> Tree e }
  if isEmptyTree T then leaf e
  let eT = elt T in
  if e = eT then T
  else if e < eT then
    fork eT (BSTadd (left T) e) (right T)
  else { e > eT }
    fork eT (left T) (BSTadd (right T) e)

and another way to build a binary search tree from a list of values, L, is
foldl BSTadd emptyTree L

L. Allison, Circular programs and self referential structures. Software Practice and Experience 19(2) pp99-109, Feb 1989.
L. Allison, Applications of Recursively Defined Data Structures, Australian Computer Journal 25(1) pp14-20 1993.
Coding Ockham's Razor, L. Allison, Springer

A Practical Introduction to Denotational Semantics, L. Allison, CUP

free op. sys.
free office suite
~ free photoshop
web browser

λ ...
:: list cons
nil the [ ] list
null  predicate
hd head (1st)
tl tail (rest)

© L. Allison   (or as otherwise indicated),
Faculty of Information Technology (Clayton), Monash University, Australia 3800 (6/'05 was School of Computer Science and Software Engineering, Fac. Info. Tech., Monash University,
was Department of Computer Science, Fac. Comp. & Info. Tech., '89 was Department of Computer Science, Fac. Sci., '68-'71 was Department of Information Science, Fac. Sci.)
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Thursday, 20-Jun-2024 15:59:53 AEST.