二叉树的C++实现

来源:互联网 发布:浮云网淘宝小号 编辑:程序博客网 时间:2024/05/16 01:07

关于二叉树的实现直接贴代码吧:

// BinaryTree_Class.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "stdafx.h"#include <iostream>#include<string.h>#include<stack> using namespace std;template<class T>struct _BinNode{T data;struct _BinNode *lchild,*rchild;};template<class T>class BinTree{public:typedef struct _BinNode<T> BinNode;//声明根结点成员BinNode *root;public:BinTree();~BinTree();void createTree( char *s);void printInorder(BinNode *pRoot) const;void _printInorder(BinNode *pRoot) const;void printPreOrder(BinNode *pRoot) const;void _printPreOrder(BinNode *pRoot) const;void printPostOrder(BinNode *pRoot) const;void _printPostOrder(BinNode *pRoot) const;int size(BinNode *T) const;int Height(BinNode *T) const;BinNode * _allocate();//产生一个新的结点};template<class T>typename BinTree<T>::BinNode * BinTree<T>::_allocate(){BinNode *newNode = new struct _BinNode<T>;newNode->lchild = NULL;newNode->rchild = NULL;newNode->data = NULL;return newNode;}template<class T>BinTree<T>::BinTree(){root = _allocate();}template<class T>BinTree<T>::~BinTree(){delete root;root = NULL; //记住在用delete释放root的内存后,root成为了悬空指针,需要让它指为空,否则很危险}template<class T> void BinTree<T>::createTree( char *s)  //形如A(B,C(D,E)){stack<T> s1;stack<typename BinTree<T>::BinNode *> s2;  //用于存放结点root->data = s[0];s2.push(root);bool isRight = false;  //int i=1;while(i<strlen(s)){if(s[i] == '('){isRight = false;s1.push(s[i]);}else if(s[i] == ')'){s1.pop();s2.pop();}else if(s[i] == ',')isRight = true;else{//结点typename BinTree<T>::BinNode *newNode = _allocate();newNode->data = s[i];typename BinTree<T>::BinNode *temp = s2.top();if(isRight)temp->rchild = newNode;elsetemp->lchild = newNode;if(s[i+1] == '(')s2.push(newNode);}i++;}}//中序递归遍历template<class T>void BinTree<T>::printInorder(BinNode *pRoot) const{if(pRoot!=NULL){printInorder(pRoot->lchild);cout<<pRoot->data<<',';printInorder(pRoot->rchild);}}//中序非递归遍历template<class T>void BinTree<T>::_printInorder(BinNode *pRoot) const{stack<BinNode *> s;BinNode *p = pRoot;while(!s.empty() ||p!=NULL){while(p!=NULL){s.push(p);p = p->lchild;}if(!s.empty()){BinNode *temp = s.top();s.pop();cout<<temp->data<<',';p = temp->rchild;}}}template<class T>void BinTree<T>::printPreOrder(typename BinTree<T>::BinNode *pRoot) const{if(pRoot!=NULL){cout<<pRoot->data<<',';printPreOrder(pRoot->lchild);printPreOrder(pRoot->rchild);}}template<class T>void BinTree<T>::_printPreOrder(BinNode *pRoot) const{stack<BinNode *> s;BinNode *p = pRoot;while(!s.empty() || p!=NULL){while(p!=NULL){cout<<p->data<<',';s.push(p);p = p->lchild;}if(!s.empty()){BinNode *temp = s.top();s.pop();p = temp->rchild;}}}template<class T>void BinTree<T>::printPostOrder(typename BinTree<T>::BinNode *pRoot) const{if(pRoot!=NULL){printPostOrder(pRoot->lchild);printPostOrder(pRoot->rchild);cout<<pRoot->data<<',';}}int _tmain(int argc, _TCHAR* argv[]){ char *s = "A(B,C(D,E))";BinTree<char> *bTree = new BinTree<char>;bTree->createTree(s);cout<<"中序遍历:";bTree->printInorder(bTree->root);bTree->_printInorder(bTree->root);cout<<"前序遍历:";bTree->printPreOrder(bTree->root);bTree->_printPreOrder(bTree->root);cout<<"后序遍历:";bTree->printPostOrder(bTree->root);return 0;}