C ++ template tree Creation

Source: Internet
Author: User

Header file:

#ifndef TREE_H#define TREE_Hnamespace wyp{template<class T>class SearchTree;template<class T>class TreeNode{public:TreeNode() : data(NULL){}TreeNode(T data, TreeNode<T> *left, TreeNode<T> *right) : data(data), left(left), right(right){} friend class SearchTree<T>;private:T data;TreeNode<T> *left;TreeNode<T> *right;};template<class T>class SearchTree{public:SearchTree() : root(NULL){}virtual ~SearchTree();void insert(T item);bool inTree(T item) const;void inorderShow() const;private:void insert(T item, TreeNode<T> *& subTreeRoot);bool inTree(T item, TreeNode<T> *subTreeRoot) const;void deleteSubTree(TreeNode<T> *&subTreeRoot); void inorderShow(TreeNode<T> *subTreeRoot) const;TreeNode<T> *root;};}#endif

Implementation:

#include "tree.h"#include <iostream>using namespace std;namespace wyp{template<class T>void SearchTree<T>::insert(T item, TreeNode<T> *& subTreeRoot){if(subTreeRoot == NULL){subTreeRoot = new TreeNode<T>(item, NULL, NULL);}else if(item < subTreeRoot->data){insert(item, subTreeRoot->left);}else{insert(item, subTreeRoot->right);}}template<class T>void SearchTree<T>::insert(T item){insert(item, root);}template<class T>bool SearchTree<T>::inTree(T item, TreeNode<T> *subTreeRoot) const{if(subTreeRoot == NULL){return false;}else if(subTreeRoot->data == item){return true;}else if(subTreeRoot->data > item){inTree(item, subTreeRoot->right);}else{inTree(item, subTreeRoot->left);}}template<class T>bool SearchTree<T>::inTree(T item) const{inTree(item, root);}template<class T>void SearchTree<T>::deleteSubTree(TreeNode<T> *&subTreeRoot){if(subTreeRoot != NULL){deleteSubTree(subTreeRoot->left);deleteSubTree(subTreeRoot->right);delete subTreeRoot;subTreeRoot = NULL;}}template<class T>void SearchTree<T>::inorderShow(TreeNode<T> *subTreeRoot) const{if(subTreeRoot != NULL){inorderShow(subTreeRoot->left);cout << subTreeRoot->data << "\t";inorderShow(subTreeRoot->right);}}template<class T>void SearchTree<T>::inorderShow() const{inorderShow(root);}template<class T>SearchTree<T>::~SearchTree(){deleteSubTree(root);}}

Exploitation:

#include <iostream>#include "tree.h"#include "tree.cpp"using namespace std;using wyp::SearchTree;int main(){SearchTree<int> t;cout << "Enter the number \n";int next;cin >> next;while(next >= 0){t.insert(next);cin >> next;}cout << "Show tree\n";t.inorderShow();cout << endl;return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.