Ex i.j denotes exercise j at end of chapter i.
Ex i.j.k denotes exercise k at end of chapter i, section j.
If p is a tree whose nodes each holds a value of type T.
T root_value(tree p) // returns value stored at p's root.
tree & left(tree p) // p's left subtree
tree & right(tree p) // p's right subtree
bool empty(tree p) // true of an empty tree, no nodes
bool leaf(tree p) // true if p is not empty and both left(p) and right(p) are empty.
Note: this system allows left(p) and right(p) and root_value(p) to be valid on all trees including empty ones. The children of an empty tree are empty trees. The root_value of an empty tree is the default value of type T.
template< class T > typedef node* tree ; template< class T > struct node { tree* l; tree* r; T rv; node(T& x = T(), tree& lt = NULL, tree& rt = NULL)): rv(x), l(lt), r(rt){} }; template< class T> T root_value(tree t) { return (t == NULL) ? T() : t->rv; } template< class T> tree left(tree t) { return (t == NULL) ? NULL : t->l; } template< class T> tree right(tree t) { return (t == NULL) ? NULL : t->r; } template< class T> bool empty(tree t) { return t == NULL; } template< class T > bool leaf(tree t) { return t != NULL && t->l == NULL && t->r == NULL; } template< class T > tree singleton(T& x) { return new node(x); } template< class T > tree buildtree(T& x, tree& lt, tree& rt) { return new node(x, lt, rt); }