exercises.txt 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 1.1 This simple program implements persistent functional binary search
  2. trees, so that if tree2-insert(x,tree1), then tree1 is till available
  3. for lookups even while tree2 can be used.
  4. type key = string
  5. datatype tree = LEAF | TREE of tree * key * tree
  6. val empty = LEAF
  7. fun insert (key, LEAF) = TREE(LEAF, key, LEAF)
  8. | insert (key, TREE(l,k,r)) =
  9. if key < k
  10. then TREE(insert(key,l),k,r)
  11. else if key > k
  12. then TREE(l,k,insert(key,r))
  13. else TREE(l,key,r)
  14. a. implement a `member` function that returns `true` if the item is
  15. found, else `false`
  16. b. Extend the program to include not just membership, but the mapping
  17. of keys to bindings:
  18. datatype 'a tree = ...
  19. insert: 'a tree * key * 'a -> 'a tree
  20. lookup: 'a tree * key -> 'a
  21. c. These trees are not balanced; demonstrate the behavior on the
  22. following two sequences of insertions:
  23. (a) t s p i p f b s t
  24. (b) a b c d e f g h i
  25. d*. Research balanced search trees in Sedgewick [1997] and recommend a
  26. balanced-tree data structure for functional symbol tables. Hint: To
  27. preserve a functional style, the algorithm should be one that
  28. rebalances on insertion but not on lookup, so a data structure such as
  29. splay trees is not appropriate.