/*************************************************************//*三、二叉樹的遍曆/*1.輸入一個完全二叉樹的層次遍曆字串,建立這個二叉樹,/*輸出這個二叉樹的前序走訪字串、中序遍曆字串、/*後序遍曆字串、結點數目、二叉樹高度。/*2.輸入二叉樹前序序列和中序序列(各元素各不相同),/*建立這個二叉樹,輸出該二叉樹的後序序列。/*************************************************************///鏈表描述二叉樹的建立與遍曆#include <iostream>#include <string>#include <queue>#include <stack>using namespace std;using std::queue;using std::stack;#define MAX 100//前中序輸入的最大數目static string outputString="";/**********************************//*以下內容是一個二叉樹資料結構定義*//**********************************/template<class T>class BinaryTreeNode{public:BinaryTreeNode(){LeftChild=RightChild=0;}BinaryTreeNode(const T&e){data=e;LeftChild=RightChild=0;}BinaryTreeNode(const T&e,BinaryTreeNode *l,BinaryTreeNode *r){data=e;LeftChild=l;RightChild=r;}T data;BinaryTreeNode<T>*LeftChild;//左子樹BinaryTreeNode<T>*RightChild;//右子樹};/**********************************//*以下內容是一個二叉樹臨時元素定義*//**********************************/#ifndef STACKELEMENT_H#define STACKELEMENT_H//StackElement結構的定義是為瞭解決非遞迴後序遍曆問題enum Tags{Left,Right};template<class T>struct StackElement{BinaryTreeNode<T>* pointer;Tags tag;};#endiftemplate<class T>class BinaryTree{public:BinaryTree(){root=0;}~BinaryTree(){};bool IsEmpty()const{return ((root)?false:true)};//如果root存在則為false,也就是不為空白。bool Root(T&x)const;void MakeTree(const T&element,BinaryTree<T>&left,BinaryTree<T>&right);void MakeTree(BinaryTreeNode<T>*r){root=r;}void BreakTree(T&element,BinaryTree<T>&left,BinaryTree<T>&right);void PreOrder(void(*Visit)(BinaryTreeNode<T>*u)){PreOrder(Visit,root);}void InOrder(void(*Visit)(BinaryTreeNode<T>*u)){InOrder(Visit,root);}void PostOrder(void(*Visit)(BinaryTreeNode<T>*u)){PostOrder(Visit,root);}void LevelOrder(void(*Visit)(BinaryTreeNode<T>*u)){LevelOrder(Visit,root);};//二叉樹類的擴充void PreOutput();void InOutput();void PostOutput();void LevelOutput();void Delete();//刪除二叉樹並釋放其節點void AddNode(const T&u);//刪除二叉樹並釋放其節點int Height(BinaryTreeNode<T>*t)const;//返回樹的高度int Size();//返回樹中的節點數BinaryTreeNode<T>*root;void PreOrder(void(*Visit)(BinaryTreeNode<T>*u),BinaryTreeNode<T>*t);void InOrder(void(*Visit)(BinaryTreeNode<T>*u),BinaryTreeNode<T>*t);void PostOrder(void(*Visit)(BinaryTreeNode<T>*u),BinaryTreeNode<T>*t);void LevelOrder(void(*Visit)(BinaryTreeNode<T>*u),BinaryTreeNode<T>*t);};//取根節點的data域//如果沒有根節點則返回falsetemplate<class T>bool BinaryTree<T>::Root(T&x)const{if(root){x=root->data;return true;}else{return false;}}//將left,right,element合并成一顆新樹//left、right和this必須是不同的樹template<class T>void BinaryTree<T>::MakeTree(const T&element,BinaryTree<T>&left,BinaryTree<T>&right){root = new BinaryTreeNode<T>(element,left.root,right.root);left.root=right.root=0;//禁止通過其他途徑訪問left和right}void BadInput(){cout<<"Bad Input!"<<endl;}//將this拆分成left、right和element//left、right和this必須是不同的樹template<class T>void BinaryTree<T>::BreakTree(T&element,BinaryTree<T>&left,BinaryTree<T>&right){if(root)throw BadInput();//空樹//分解樹element=root->data;left.root=root->LeftChild;right.root=root->RightChild;delete root;root = 0;}//靜態成員函數Output輸出樹template<class T>static void Output(BinaryTreeNode<T>*t){outputString+=t->data;outputString+=",";}template<class T>void BinaryTree<T>::PreOutput(){outputString="";PreOrder(Output,root);outputString.erase(outputString.end()-1);cout<<outputString<<endl;}template<class T>void BinaryTree<T>::InOutput(){outputString="";InOrder(Output,root);outputString.erase(outputString.end()-1);cout<<outputString<<endl;}template<class T>void BinaryTree<T>::PostOutput(){outputString="";PostOrder(Output,root);outputString.erase(outputString.end()-1);cout<<outputString<<endl;}template<class T>void BinaryTree<T>::LevelOutput(){outputString="";LevelOrder(Output,root);outputString.erase(outputString.end()-1);cout<<outputString<<endl;}template<class T>void BinaryTree<T>::PreOrder(void(*Visit)(BinaryTreeNode<T>*u),BinaryTreeNode<T>*t){/*************使用遞迴實現的遍曆***************if(t){Visit(t);PreOrder(Visit,t->LeftChild);PreOrder(Visit,t->RightChild);}/**********************************************//************使用非遞迴實現的遍曆**************/stack<BinaryTreeNode<T>*>myStack;BinaryTreeNode<T>*pointer = root;while(!myStack.empty()||pointer)//棧不空或者pointer不空{if(pointer){Visit(pointer);//訪問當前節點myStack.push(pointer);//當前節點地址入棧pointer=pointer->LeftChild;//訪問左邊分支}else//左子樹訪問完畢{pointer=myStack.top();myStack.pop();pointer=pointer->RightChild;}}}template<class T>void BinaryTree<T>::InOrder(void(*Visit)(BinaryTreeNode<T>*u),BinaryTreeNode<T>*t){/*************使用遞迴實現的遍曆***************if(t){InOrder(Visit,t->LeftChild);Visit(t);InOrder(Visit,t->RightChild);}/**********************************************//************使用非遞迴實現的遍曆**************/stack<BinaryTreeNode<T>*>myStack;BinaryTreeNode<T>*pointer = root;while(!myStack.empty()||pointer)//棧不空或者pointer不空{if(pointer){myStack.push(pointer);//當前節點地址入棧pointer=pointer->LeftChild;//訪問左邊分支}else//左子樹訪問完畢{pointer=myStack.top();Visit(pointer);//訪問當前節點myStack.pop();pointer=pointer->RightChild;}}}template<class T>void BinaryTree<T>::PostOrder(void(*Visit)(BinaryTreeNode<T>*u),BinaryTreeNode<T>*t){/*************使用遞迴實現的遍曆***************/if(t){PostOrder(Visit,t->LeftChild);PostOrder(Visit,t->RightChild);Visit(t);}/**********************************************//************使用非遞迴實現的遍曆**************StackElement<T>element;stack<StackElement<T>>myStack;BinaryTreeNode<T>*pointer;if(root==NULL)return;pointer=root;while(true){while(pointer!=NULL){element.pointer = pointer;element.tag=Left;myStack.push(element);pointer=pointer->LeftChild;}element=myStack.top();myStack.pop();pointer=element.pointer;//從右子樹回來while(element.tag==Right){Visit(pointer->data);if (myStack.empty()){return}else{element=myStack.top();myStack.pop();pointer=element.pointer;}//從左子樹回來element.tag = Right;myStack.push(element);//轉向訪問右子樹pointer=pointer->RightChild;}}/**********************************************/}template<class T>void BinaryTree<T>::LevelOrder(void(*Visit)(BinaryTreeNode<T>*u),BinaryTreeNode<T>*t){queue<BinaryTreeNode<T>*>myQueue;while(t){Visit(t);if(t->LeftChild)myQueue.push(t->LeftChild);if(t->RightChild)myQueue.push(t->RightChild);if(!myQueue.empty()){t=myQueue.front();myQueue.pop();}else{break;}}}template<class T>void BinaryTree<T>::AddNode(const T&u){if(!root){root = new BinaryTreeNode<T>;root->data = u;root->LeftChild=0;root->RightChild=0;return ;}BinaryTreeNode<T> *newNode = new BinaryTreeNode<T>;newNode->data = u;queue<BinaryTreeNode<T>*>myQueue;BinaryTreeNode<T>*t;t=root;while(t){if(t->LeftChild){myQueue.push(t->LeftChild);}if(t->RightChild){myQueue.push(t->RightChild);}if(!t->LeftChild){t->LeftChild=newNode;return;}else{}if(!t->RightChild){t->RightChild=newNode;return;}else{}if(!myQueue.empty()){t=myQueue.front();myQueue.pop();//隊列中刪除一個節點並且將其賦值給t}}}template<class T>static void Free(BinaryTreeNode<T>*t){delete t;}template<class T>void BinaryTree<T>::Delete(){PostOrder(Free,root);root = 0;}template<class T>int BinaryTree<T>::Height(BinaryTreeNode<T>*t)const{if(!t)return 0;int hl = Height(t->LeftChild);int hr = Height(t->RightChild);return hl>hr?++hl:++hr;}template<class T>int BinaryTree<T>::Size(){count = 0 ;PreOrder(countTree,root);return count;}int count = 0;BinaryTree<char> a,x,y,z;template<class T>void countTree(BinaryTreeNode<T>*t){count++;}BinaryTreeNode<char>* creat(char *pre,char *in,int len){ int k; if(len<=0) return NULL; BinaryTreeNode<char> *head=new BinaryTreeNode<char>; head->data=*pre; char *p; for(p=in;*p!=NULL;p++) //應該是*p!= NULL, 而不是p!=NULL if(*p==*pre) break;//判斷失敗if (*p == NULL){printf("NO ANSWER.\n");return NULL;}k=p-in;head->LeftChild=creat(pre+1,in,k);head->RightChild=creat(pre+k+1,p+1,len-k-1);return head;}void main(){cout<<"Input1"<<endl;string myInput=" ";cin>>myInput;int which = 0;while(myInput[which]){char temp = myInput[which];y.AddNode(temp);which++;}cout<<"Output1"<<endl;y.PreOutput();y.InOutput();y.PostOutput();cout<<"Input2"<<endl;string input1;cin>>input1;string input2;cin>>input2;char*preOrder=(char*)input1.c_str();char*inOrder=(char*)input2.c_str();BinaryTreeNode<char>*myRoot = creat(preOrder,inOrder,input2.length());BinaryTree<char>newTree;newTree.MakeTree(myRoot);cout<<"Output2"<<endl;newTree.PostOutput();newTree.LevelOutput();cout<<"End"<<endl;}