下面代碼僅供本人複習資料結構所用,實用性N低,各位飄過吧~~哈哈:>
//// C++ 模版技術實現簡單二叉樹. //#include <cstdlib>#include <cstring>#include <iostream>// 二叉樹類模版前置聲明template <typename T> class BinaryTree; //// 二叉樹節點類模版. //template <typename T>class Node{friend class BinaryTree<T>;private:T _data;Node<T> *_pLeftChild, *_pRightChild;public:Node(const T &data): _data(data), _pLeftChild(NULL), _pRightChild(NULL){ NULL; }};//// 二叉樹類模版 //template <typename T> class BinaryTree{private:Node<T> *_pRoot;private:void _create(Node<T> **ppRoot, T **ppArray, int *piSize, const T &delim);void _clear(Node<T> *pRoot);void _preOrder(Node<T> *pRoot) const;void _inOrder(Node<T> *pRoot) const;void _postOrder(Node<T> *pRoot) const; size_t _height(Node<T> *pRoot) const; size_t _count(Node<T> *pRoot) const; public:// 遍曆類型. enum TraversalType {PRE_ORDER,IN_ORDER,POST_ORDER};public:BinaryTree(T *array, int size, const T &delim);~BinaryTree(void);void traversal(const TraversalType type) const;size_t getHeight(void) const;size_t getNumberOfNode(void) const;};//// 建立二叉樹.// 這裡基於前序走訪構造二叉樹,輸入的是二叉樹的先序序列, // 但必須在其中加入虛節點以表示null 指標的位置. // 例如: // (A)// / \// (B) (C)// / \ / \// (D) % (E) (F)// / \ / \ / \// % % % % % %//// 其中字母代表節點,% 代表虛節點,故先序序列為:ABD%%%CE%%F%%. //// 參數 ppRoot 是指向根指標的指標,故修改 *ppRoot 就修改了實參(根指標)本身.// 參數 ppArray 是指向儲存先序序列數組地址的指標,我們也需要必要的修改.// 參數 piSize 是指向表示數組大小的整型變數的指標,我們也需要修改它. // 參數 delim 是分隔字元,用來指代虛節點,我們需要跳過虛節點。 // template <typename T>void BinaryTree<T>::_create(Node<T> **ppRoot, T **ppArray, int *piSize,const T &delim){if (0 < *piSize && delim != **ppArray) {*ppRoot = new Node<T>(**ppArray);_create(&((*ppRoot)->_pLeftChild), &(++*ppArray), &(--*piSize), delim);_create(&((*ppRoot)->_pRightChild), &(++*ppArray), &(--*piSize), delim);}}//// 刪除所有節點. //template <typename T> void BinaryTree<T>::_clear(Node<T> *pRoot){if (NULL != pRoot){_clear(pRoot->_pLeftChild);_clear(pRoot->_pRightChild);delete pRoot;}}//// 前序走訪. //template <typename T> void BinaryTree<T>::_preOrder(Node<T> *pRoot) const{if (NULL != pRoot){std::cout << pRoot->_data << " ";_preOrder(pRoot->_pLeftChild);_preOrder(pRoot->_pRightChild);}}//// 中序遍曆. //template <typename T> void BinaryTree<T>::_inOrder(Node<T> *pRoot) const{if (NULL != pRoot){_inOrder(pRoot->_pLeftChild);std::cout << pRoot->_data << " ";_inOrder(pRoot->_pRightChild);}}//// 後序遍曆. //template <typename T> void BinaryTree<T>::_postOrder(Node<T> *pRoot) const{if (NULL != pRoot){_postOrder(pRoot->_pLeftChild);_postOrder(pRoot->_pRightChild);std::cout << pRoot->_data << " ";}}//// 通過後序遍曆方式計算二叉樹高度. //template <typename T> size_t BinaryTree<T>::_height(Node<T> *pRoot) const{static size_t height = 0, leftHeight = 0, rightHeight = 0;if (NULL != pRoot){leftHeight = _height(pRoot->_pLeftChild);rightHeight = _height(pRoot->_pRightChild);height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;}else {height = 0;}return height;}//// 通過前序走訪方式計算二叉樹節點數. //template <typename T> size_t BinaryTree<T>::_count(Node<T> *pRoot) const{static size_t counter = 0;if (NULL != pRoot) {counter = _count(pRoot->_pLeftChild) + _count(pRoot->_pRightChild) + 1;}else {counter = 0;}return counter;}template <typename T> inline BinaryTree<T>::BinaryTree(T *array, int size, const T &delim){ _create(&_pRoot, &array, &size, delim);}template <typename T> inline BinaryTree<T>::~BinaryTree(void){_clear(_pRoot);}//// 根據控制標識選擇遍曆方式. // template <typename T> void BinaryTree<T>::traversal(const TraversalType type) const{switch (type){case PRE_ORDER :_preOrder(_pRoot);break;case IN_ORDER :_inOrder(_pRoot);break;case POST_ORDER :_postOrder(_pRoot);break;}}template <typename T> size_t BinaryTree<T>::getHeight(void) const{return _height(_pRoot);}template <typename T> size_t BinaryTree<T>::getNumberOfNode(void) const{return _count(_pRoot);}//// 測試二叉樹. //int main(void){const size_t MAX_SIZE = 100;char szPreOrder[MAX_SIZE] = {'\0'};std::cout << "請輸入二叉樹節點資料前序序列:" << std::endl; while (!std::cin.getline(szPreOrder, MAX_SIZE)){std::cout << "輸入錯誤. " << std::endl;std::cout << "重新輸入: ";std::cin.clear();std::cin.sync();}BinaryTree<char> binTree(szPreOrder, strlen(szPreOrder), ' ');//// 遍曆二叉樹. //std::cout << "前序走訪:";binTree.traversal(BinaryTree<char>::PRE_ORDER);std::cout << std::endl << "中序遍曆:";binTree.traversal(BinaryTree<char>::IN_ORDER);std::cout << std::endl << "中序遍曆:";binTree.traversal(BinaryTree<char>::POST_ORDER);std::cout << std::endl;std::cout << "樹高:" << binTree.getHeight() << std::endl; std::cout << "節點:" << binTree.getNumberOfNode() << std::endl; return EXIT_SUCCESS;}