/* Classs to build and process binary search trees * data stored in nodes have class T * class T just have relation operators defined: ==, !=, <, <=, >, >= * output stream cout must have << overloaded for type T * nodes have class TreeNode */ #ifndef _BSTREE_H #define _BSTREE_H #include #include "Entry.h" #include "TreeNode.h" using namespace std; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Class Header * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ template class BSTree { protected: TreeNode * root; private: void printKernel (TreeNode * base) ; T * lookupKernel (TreeNode * base, T * desiredItem) ; public: BSTree () ; void insert (T newEntry) ; void print () ; T * lookup (T desiredItem) ; T * lookup (T * desiredItem) ; } ; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Implementation Code * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Since TreeNode is a templated class, implementation details normally must be * included in the header file, because the compiler must create code for * instantiated class required in an application. This is not possible, if * the templated class is implamented and compiled in a separate file. * For additional information, see https://cplusplus.com/forum/general/12575/ */ template BSTree :: BSTree () : root (NULL) { } template void BSTree ::insert (T newEntry) { if (root == NULL) root = new TreeNode(newEntry); else { TreeNode * ptr = root; // pointer to node in search for leaf while (ptr != NULL) {// search for leaf if (newEntry < *(ptr->getData())) { // insert on left of given node if (ptr->getLeft() == NULL) { // when at end of tree, insert ptr->setLeft(new TreeNode(newEntry)); return; } else { // move left in tree and continue search ptr = ptr->getLeft(); } } else { // insert on right of given node if (ptr->getRight() == NULL) { // when at end of tree, insert ptr->setRight(new TreeNode(newEntry)); return; } else { // move right in tree and continue search ptr = ptr->getRight(); } } } } } template void BSTree :: print () { cout << "--------------------Directory Listing--------------------\n"; printKernel (root); cout << "\n--------------------End of Listing--------------------\n\n"; } template void BSTree ::printKernel (TreeNode * base) { // to print elements in a tree (using an in-order traversal), // print the left subtree // print the elements in a node // print the right subtree if (base != NULL) { printKernel (base->getLeft()); cout << *(base->getData()); printKernel (base->getRight()); } } template T * BSTree ::lookup (T desiredItem) { return lookupKernel (root, & desiredItem); } template T * BSTree ::lookup (T * desiredItem) { return lookupKernel (root, desiredItem); } template T * BSTree ::lookupKernel (TreeNode * base, T * desiredItem) { if (base == NULL) return NULL; else if (*(base->getData()) == (*desiredItem)) return base->getData(); else if (*(base->getData()) < (*desiredItem)) return lookupKernel (base->getRight(), desiredItem); else return lookupKernel (base->getLeft(), desiredItem); } #endif