#include // Provides assert #include // Provides setw #include // Provides cout #include // Provides NULL, size_t template struct BinaryTreeNode { Item data; BinaryTreeNode* left; BinaryTreeNode* right; }; template BinaryTreeNode* create_node( const Item& entry, BinaryTreeNode* l_ptr = NULL, BinaryTreeNode* r_ptr = NULL ) { BinaryTreeNode* result_ptr; result_ptr = new BinaryTreeNode; result_ptr->data = entry; result_ptr->left = l_ptr; result_ptr->right = r_ptr; return result_ptr; } template BinaryTreeNode* tree_copy(const BinaryTreeNode* root_ptr) { BinaryTreeNode *l_ptr; BinaryTreeNode *r_ptr; if (root_ptr == NULL) return NULL; else { l_ptr = tree_copy(root_ptr->left); r_ptr = tree_copy(root_ptr->right); return create_node(root_ptr->data, l_ptr, r_ptr); } } template void tree_clear(BinaryTreeNode*& root_ptr) { if (root_ptr != NULL) { tree_clear(root_ptr->left); tree_clear(root_ptr->right); delete root_ptr; root_ptr = NULL; } } template bool is_leaf(const BinaryTreeNode& node) { return (node.left == NULL) && (node.right == NULL); } template void inorder(Process f, BinaryTreeNode* node_ptr) { if (node_ptr != NULL) { inorder(f, node_ptr->left); f(node_ptr->data); inorder(f, node_ptr->right); } } template void postorder(Process f, BinaryTreeNode* node_ptr) { if (node_ptr != NULL) { postorder(f, node_ptr->left); postorder(f, node_ptr->right); f(node_ptr->data); } } template void preorder(Process f, BinaryTreeNode* node_ptr) { if (node_ptr != NULL) { f(node_ptr->data); preorder(f, node_ptr->left); preorder(f, node_ptr->right); } } template void print(BinaryTreeNode* node_ptr, SizeType depth) { if (node_ptr != NULL) { print(node_ptr->right, depth+1); cout << setw(4*depth) << ""; // Indent 4*depth spaces cout << node_ptr->data << endl; print(node_ptr->left, depth+1); } } template size_t tree_size(const BinaryTreeNode* node_ptr) { if (node_ptr == NULL) return 0; else return 1 + tree_size(node_ptr->left) + tree_size(node_ptr->right); }