home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 19 / AACD19.BIN / AACD / Programming / YAEC / testsrc / Trees.e < prev    next >
Encoding:
Text File  |  2001-02-23  |  887 b   |  46 lines

  1. ->/* A different style of programming in E:
  2. ->   working with and building large dynamic datastructures
  3. ->   without using the keyword PTR
  4. ->
  5. ->        / \
  6. ->       /   \
  7. ->     /       \
  8. ->   / \       / \
  9. -> /\   /\   /\   /\
  10. ->1  2 3  4 5  6 7  8
  11. ->
  12. ->*/
  13.  
  14. PROC main()
  15.   DEF tree, a
  16.   tree := node(
  17.           node(
  18.             node(leaf(1), leaf(2)),
  19.             node(leaf(3), leaf(4))
  20.           ),
  21.           node(
  22.             node(leaf(5), leaf(6)),
  23.             node(leaf(7), leaf(8))
  24.           )
  25.         )
  26.   WriteF('sum = \d\n',sum(tree))
  27.   FOR a := 1 TO 10
  28.     tree := node(leaf(100), tree)
  29.     WriteF('sum = \d\n', sum(tree))
  30.   ENDFOR
  31. ENDPROC
  32.  
  33. PROC node(l, r)
  34. ENDPROC NEW ["node", l, r]
  35. PROC leaf(n)
  36. ENDPROC NEW ["leaf", n]
  37.  
  38. PROC sum(t)
  39.   DEF left, right, n
  40.   IF t <=> ["node", left, right]
  41.     RETURN sum(left) + sum(right)
  42.   ELSEIF t <=> ["leaf", n]
  43.     RETURN n
  44.   ENDIF
  45. ENDPROC
  46.