C++ examples for Data Structure:Tree
Determine how many levels a binary tree has.
template <typename NODETYPE> class Tree;/* ww w.j a v a2 s .c om*/ template <typename NODETYPE> class TreeNode { friend class Tree<NODETYPE>; public: explicit TreeNode(const NODETYPE &d) : leftPtr(0), data(d), rightPtr(0) {} // return copy of node's data NODETYPE getData() const { return data; } private: TreeNode<NODETYPE> *leftPtr; NODETYPE data; TreeNode<NODETYPE> *rightPtr; }; #include <iostream> #include <algorithm> // Tree class-template definition template <typename NODETYPE> class Tree { public: Tree(); void insertNode(const NODETYPE &); void preOrderTraversal() const; void inOrderTraversal() const; void postOrderTraversal() const; // determine the depth of the given tree static size_t depth(Tree<NODETYPE> tree) { return tree.depthHelper(tree.rootPtr); } private: TreeNode<NODETYPE> *rootPtr; // utility functions void insertNodeHelper(TreeNode<NODETYPE> **, const NODETYPE &); void preOrderHelper(TreeNode<NODETYPE> *) const; void inOrderHelper(TreeNode<NODETYPE> *) const; void postOrderHelper(TreeNode<NODETYPE> *) const; size_t depthHelper(TreeNode<NODETYPE> *) const; }; // constructor template <typename NODETYPE> Tree<NODETYPE>::Tree() { rootPtr = 0; // indicate tree is initially empty } // insert node in tree template <typename NODETYPE> void Tree<NODETYPE>::insertNode(const NODETYPE &value) { insertNodeHelper(&rootPtr, value); } template <typename NODETYPE> void Tree<NODETYPE>::insertNodeHelper(TreeNode<NODETYPE> **ptr, const NODETYPE &value) { if (*ptr == 0) { *ptr = new TreeNode<NODETYPE>(value); } else { // data to insert is less than data in current node if (value < (*ptr)->data) { insertNodeHelper(&((*ptr)->leftPtr), value); } else { // data to insert is greater than data in current node if (value > (*ptr)->data) insertNodeHelper(&((*ptr)->rightPtr), value); else // duplicate value ignored std::cout << value << " dup" << std::endl; } } } // being preorder traversal of Tree template <typename NODETYPE> void Tree<NODETYPE>::preOrderTraversal() const { preOrderHelper(rootPtr); } // utility function to perform preorder traversal of Tree template <typename NODETYPE> void Tree<NODETYPE>::preOrderHelper(TreeNode<NODETYPE> *ptr) const { if (ptr != 0) { std::cout << ptr->data << ' '; // process node preOrderHelper(ptr->leftPtr); preOrderHelper(ptr->rightPtr); } } // begin inorder traversal of Tree template <typename NODETYPE> void Tree<NODETYPE>::inOrderTraversal() const { inOrderHelper(rootPtr); } // utility function to perform inorder traversal of Tree template <typename NODETYPE> void Tree<NODETYPE>::inOrderHelper(TreeNode<NODETYPE> *ptr) const { if (ptr != 0) { inOrderHelper(ptr->leftPtr); std::cout << ptr->data << ' '; inOrderHelper(ptr->rightPtr); } } // begin postorder traversal of Tree template <typename NODETYPE> void Tree<NODETYPE>::postOrderTraversal() const { postOrderHelper(rootPtr); } // utility function to perform postorder traversal of Tree template <typename NODETYPE> void Tree<NODETYPE>::postOrderHelper(TreeNode<NODETYPE> *ptr) const { if (ptr != 0) { postOrderHelper(ptr->leftPtr); postOrderHelper(ptr->rightPtr); std::cout << ptr->data << ' '; // process node } } // utility function to return the depth of the given node template <typename NODETYPE> size_t Tree<NODETYPE>::depthHelper(TreeNode<NODETYPE> *ptr) const { // base case if (ptr == 0) { return 0; } else { size_t ldepth = depthHelper(ptr->leftPtr); size_t rdepth = depthHelper(ptr->rightPtr); if (ldepth <= rdepth) { return rdepth + 1; } else { return ldepth + 1; } } } #include <iostream> int main(int argc, const char* argv[]) { static const int LIMIT = 10; Tree<int> intTree; std::cout << "Enter " << LIMIT << " integers: "; int value; for (int i = 0; i < LIMIT; ++i) { std::cin >> value; intTree.insertNode(value); } std::cout << "Depth of tree: " << Tree<int>::depth(intTree) << std::endl; return 0; }