標籤:遍曆 二叉樹
#include<iostream>
using namespace std;
//二叉樹節點的建立 template<class Type> class BinTreeNode { //friend class BinTree<Type>; public: BinTreeNode():data(Type()),leftChild(NULL),rightChild(NULL) {} BinTreeNode(Type d,BinTreeNode<Type> *l=NULL,BinTreeNode<Type> *r=NULL): data(d),leftChild(l),rightChild(r) {} ~BinTreeNode() {} private: Type data; BinTreeNode<Type> *leftChild; BinTreeNode<Type> *rightChild; };//二叉樹的建立template <class Type>class BinTree{public: BinTree():root(NULL) {} BinTree(Type ref):StopFlag(ref),root(NULL) {} BinTree(const BinTree<Type> &bt) { root = Copy(bt.root); StopFlag = bt.StopFlag; } ~BinTree() { Destroy(root); }private: BinTreeNode<Type> *root; Type StopFlag;};//二叉樹功能的實現template<class Type>class BinTree{public://根據中序、後序建立二叉樹 void CreateBinTreeByMidPost(BinTreeNode<Type> *&t, char *LVR, char *LRV, int n) { if(n == 0) t = NULL; else { int i=0; while(LRV[n-1] != LVR[i]) {i++;} t = new BinTreeNode<Type>(LVR[i]); CreateBinTreeByMidPost(t->rightChild,LVR+i+1,LRV+i,n-1-i); CreateBinTreeByMidPost(t->leftChild,LVR,LRV,i); }}//根據前序、中序建立二叉樹void CreateBinTreeByPreMid(BinTreeNode<Type> *&t, char *VLR, char *LVR, int n){ if(n == 0) t = NULL; else { int i=0; while(VLR[0] != LVR[i]) { i++; } t = new BinTreeNode<Type>(LVR[i]); CreateBinTreeByPreMid(t->leftChild,VLR+1,LVR,i); CreateBinTreeByPreMid(t->rightChild,VLR+i+1,LVR+i+1,n-i-1); }}//求深度 int Height(BinTreeNode<Type> *t)const { if(!t) return 0; int len1=Height(t->leftChild); int len2=Height(t->rightChild); return len1>len2 ? (len1+1) :(len2+1); }//二叉樹元素的個數int Size(BinTreeNode<Type> *t)const{ if(t == NULL) return 0; else { return Size(t->leftChild)+Size(t->rightChild)+1; }}//判等bool Equal(BinTreeNode<Type> *t1,BinTreeNode<Type> *t2){ if(t1==NULL && t2==NULL) return true; else if(t1!=NULL && t2!=NULL && t1->data==t2->data && Equal(t1->leftChild,t2->leftChild) && Equal(t1->rightChild,t2->rightChild)) return true; else return false;}//拷貝BinTreeNode<Type>* Copy(BinTreeNode<Type> *t){ if(t == NULL) return NULL; else { BinTreeNode<Type> *s = new BinTreeNode<Type>(t->data); s->leftChild = Copy(t->leftChild); s->rightChild = Copy(t->rightChild); return s; }}//求父節點BinTreeNode<Type>* Parent(BinTreeNode<Type> *t,BinTreeNode<Type> *p){ if(t==NULL || p==NULL) return NULL; if(t->leftChild==p || t->rightChild==p) return t; else { // BinTreeNode<Type> *q = Parent(t->leftChild,p); if(q != NULL) return q; return Parent(t->rightChild,p); }}//尋找二叉樹的元素BinTreeNode<Type>* Find(BinTreeNode<Type>*t,Type key){ if(t==NULL) return NULL; if(t->data == key) return t; else { BinTreeNode<Type> *p = Find(t->leftChild,key); if(p != NULL) return p; return Find(t->rightChild,key); }}//右節點BinTreeNode<Type>* RightChild(BinTreeNode<Type> *t,BinTreeNode<Type> *p){ if(t==NULL || p==NULL) return NULL; else return p->rightChild;}//左節點BinTreeNode<Type>* LeftChild(BinTreeNode<Type> *t,BinTreeNode<Type> *p){ if(t==NULL || p==NULL) return NULL; else return p->leftChild;}bool IsEmpty(BinTreeNode<Type> *t){ return t==NULL?true:false;}BinTreeNode<Type>* Root(BinTreeNode<Type> *t){ //return t; if(t == NULL) return NULL; else return t;}//層次遍曆二叉樹 -- 隊列建立void LevelOrder(BinTreeNode<Type> *t)const{ if(t == NULL) return; else { queue<BinTreeNode<Type>* > Q; BinTreeNode<Type> *p = t; Q.push(p); while(!Q.empty()) { cout<<p->data; if(p->leftChild != NULL) Q.push(p->leftChild); if(p->rightChild != NULL) Q.push(p->rightChild); Q.pop(); if(!Q.empty()) {p = Q.front(); } } }}//先序遍曆void PreOrder(BinTreeNode<Type> *t)const{ if(t != NULL) { cout<<t->data; PreOrder(t->leftChild); PreOrder(t->rightChild); }}//中序遍曆void InOrder(BinTreeNode<Type> *t)const{ if(t != NULL) { InOrder(t->leftChild); cout<<t->data; InOrder(t->rightChild); }}//後序遍曆void PostOrder(BinTreeNode<Type> *t)const{ if(t != NULL) { PostOrder(t->leftChild); PostOrder(t->rightChild); cout<<t->data; }}//非遞迴前序走訪二叉樹void NPreOrder(BinTreeNode<Type> *t)const //非遞迴 stack{ if(t == NULL) return; stack<BinTreeNode<Type>*> S; BinTreeNode<Type> *p = t; while(!S.empty() || p) { if(p != NULL) { S.push(p); //入棧 cout<<p->data; //訪問根節點 p = p->leftChild; //訪問左節點 } else { p = S.top(); S.pop(); p = p->rightChild; } }}//非遞迴 中序遍曆二叉樹void NInOrder(BinTreeNode<Type> *t)const{ if(t == NULL) return; stack<BinTreeNode<Type>*> S; BinTreeNode<Type> *p = t; while(!S.empty() || p) { if(p != NULL) { S.push(p); p = p->leftChild; } else { p = S.top(); cout<<p->data; S.pop(); p = p->rightChild; } }}//非遞迴後序遍曆二叉樹void NPostOrder(BinTreeNode<Type> *t)const{ stack<BinTreeNode<Type> *> S; BinTreeNode<Type> *p=t; //當前節點 BinTreeNode<Type> *pre=NULL; //指向前一個被訪問的節點 while(p || !S.empty()) { while(p != NULL) //一直走到左子樹為空白時 { S.push(p); p=p->leftChild; } //當前節點的右子樹為空白或者已被訪問,則訪問當前節點 p=S.top(); if(p->rightChild==NULL || p->rightChild == pre) { // p=S.top(); cout<<p->data; pre = p; S.pop(); p=NULL; } else //否則訪問右子樹 p=p->rightChild; } }private: //先序建立二叉樹 BinTreeNode<Type>* CreateBinTree1(char *&str) { if(*str == StopFlag) return NULL; else { BinTreeNode<Type> *t = new BinTreeNode<Type>(*str); t->leftChild = CreateBinTree1(++str); t->rightChild = CreateBinTree1(++str); return t; }}void CreateBinTree(BinTreeNode<Type> *&t, const char *&str){ if(*str == StopFlag) t = NULL; else { t = new BinTreeNode<Type>(*str); CreateBinTree(t->leftChild,++str); CreateBinTree(t->rightChild,++str); }}void CreateBinTree(BinTreeNode<Type> *&t){ Type item; cin>>item; if(item == StopFlag) t = NULL; else { t = new BinTreeNode<Type>(item); CreateBinTree(t->leftChild); CreateBinTree(t->rightChild); }}//ABC##DE##F##G#H##//ABCDEFGH –前序 //CBEDFAGH –中序 //CEFDBHGA – 後序//主函數功能的實現 void main() { char *str = "ABC##DE##F##G#H##"; char *VLR = "ABCDEFGH"; char *LVR = "CBEDFAGH"; char *LRV = "CEFDBHGA"; BinTree<char> mytree('#'); mytree.CreateBinTree(); //mytree.CreateBinTree(str); mytree.CreateBinTree(str); cout<<mytree.Height()<<endl; mytree.PreOrder(); cout<<endl; mytree.InOrder(); cout<<endl; mytree.PostOrder(); cout<<endl; mytree.CreateBinTreeByPreMid(VLR,LVR,8); mytree.CreateBinTreeByMidPost(LVR,LRV,8); }
二叉樹的C++實現