template class BinaryTree{ private: struct node{ node * left; node * right; type data; }; node * root; void insertHelper(node * &, type, bool(*less)(type)); void inorderHelper(node * &, void (*visit)(type)); void preorderHelper(node * &, void (*visit)(type)); void postorderHelper(node * &, void (*visit)(type)); public: BinaryTree(); void insert(type); void preorder(void (*visit)(type)); bool isempty(){ if (root == NULL) return true; else return false; } }; //default constructor template BinaryTree::BinaryTree(){ root = NULL; } //insert function /*template void BinaryTree::insert(type){ insertHelper(root, newData, less); }*/ //functions for traversals template void BinaryTree::preorder(void (*visit)(type currentData)){ preorderHelper(root, visit); } //all these helper functions basically allow for recursion to do all the work //for traversals and inserts template void BinaryTree::insertHelper(node * &newRoot, type newData, bool(*less)(type)){ //if the tree is empty, or you have found the right place to put the new node if (newRoot == NULL){ //add the information and set the pointers to null newRoot = new node; newRoot -> data = newData; newRoot -> right = NULL; newRoot -> left = NULL; return; } //otherwise, go to the appropriate branch of the tree and look for //its place there if(!less(newData, newRoot -> data)) insertHelper(newRoot->right, newData, less); else insertHelper(newRoot->left, newData, less); return; } template void BinaryTree::inorderHelper(node * &newRoot, void (*visit)(type)){ //if the tree's not empty or you're not at the end of the tree if (newRoot != NULL){ //process what's to the left inorderHelper(newRoot -> left, visit); //visit the current node visit(newRoot->data); //process what's to the right inorderHelper(newRoot -> right, visit); } } template void BinaryTree::postorderHelper(node * &newRoot, void (*visit)(type)){ //if the tree's not empty or you're not at the end of the tree if (newRoot != NULL){ //process what's to the left postorderHelper(newRoot -> left, visit); //process what's to the right postorderHelper(newRoot -> right, visit); //visit the current node visit(newRoot->data); } } template void BinaryTree::preorderHelper(node * &newRoot, void (*visit)(type)){ //if the tree's not empty or you're not at the end of the tree if (newRoot != NULL){ //visit the current node visit(newRoot->data); //process what's to the left preorderHelper(newRoot -> left, visit); //process what's to the right preorderHelper(newRoot -> right, visit); } }