/* Declarations for a typical tree-node class * Common operations * For any binary tree: * * For a binary search tree, an operator must be available to determine * when one node comes before another. * as well as an equal method. This node therefore uses the Entry class * for data storage. */ #ifndef _TREENODE_H #define _TREENODE_H #include using namespace std; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Class Header * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ template class TreeNode { protected: T data; // the information to be stored in the node TreeNode * left; // the pointer to the node's left subtree TreeNode * right; // the pointer to the node's right subtree public: // Constructors TreeNode (T startingData) ; TreeNode (T * initData, TreeNode * leftNode, TreeNode * rightNode) ; // extractors T * getData (); TreeNode * getLeft () ; TreeNode * getRight () ; // modifiers void setData (T newData) ; void setLeft (TreeNode * newLeft) ; void setRight (TreeNode * newRight) ; } ; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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/ */ // Constructors template TreeNode::TreeNode (T startingData) : data (startingData), left (NULL), right (NULL) { } template TreeNode::TreeNode (T * initData, TreeNode * leftNode, TreeNode * rightNode) : data (*initData), left (leftNode), right (rightNode) { } // extractors template T * TreeNode::getData () { return & data; } template TreeNode * TreeNode::getLeft () { return left; } template TreeNode * TreeNode::getRight () { return right; } // modifiers template void TreeNode::setData (T newData) { data = newData; } template void TreeNode::setLeft (TreeNode * newLeft) { left = newLeft; } template void TreeNode::setRight (TreeNode * newRight) { right = newRight; } // end TreeNode implementation #endif