A tree
can be empty (nilTree
) or an element e
that forks between two trees
TYPES
type tree = nilTree | fork e * (tree e) * (tree e)
OPERATIONS
empty : tree e -> boolean
leaf : tree e -> boolean
fork : e * tree e * tree e -> tree e
left : tree e -> tree e
right: tree e -> tree e
contents : tree e -> e
Recursively defined; height equals 1 plus the height of the subtrees below.
height(nilTree) = 0
| height(fork(e, TL, TR)) = 1 + max(height(TL), height(TR))
Also recursively defined; weight equals 1 plus the weights of every subtree as it forks.
weight(nilTree) = 0
| weight(fork(e,TL, TR)) = 1 + weight(TL) + weight(TR)
Root, then left subtree then right subtree
preorder(nilTree) = nil
| preorder(fork(e,TL,TR)) = print(e), preorder(TL), preorder(TR)
Left subtree, root, then right subtree
inorder(nilTree) = nil
| inorder(fork(e,TL,TR)) = inorder(TL), print(e), inorder(TR)
Left subtree, right subtree, then root
postorder(nilTree) = nil
| postorder(fork(e,TL,TR)) = postorder(TL), postorder(TR), print(e)
Root is visited, then children, then grandchildren
breadthfirst(nilTree) = nil
| ; left as exercise