#include <iostream>
#include <stdlib.h>
#include <stack>
#include <string.h>
#include <sstream>
#include <queue>
using namespace std;
//提前声明类
template<class T>
class BinaryTreeNode;
//提前声明类
template<class T>
class BinaryTree;
//自定义异常
class OutOfBound{
public:
OutOfBound(){
cout<<"程序出现越界错误!"<<endl;
}
};
//节点类
template<class T>
class BinaryTreeNode{
public:
friend class BinaryTree<T>;
BinaryTreeNode(){LeftChild=NULL;RightChild=NULL;}
BinaryTreeNode(const T& e){data=e;LeftChild=NULL;RightChild=NULL;}
BinaryTreeNode(const T& e, BinaryTreeNode *l, BinaryTreeNode *r){
data = e; LeftChild = l; RightChild = r; // data + links 参数
}
void SetLeft(BinaryTreeNode<T> *L){LeftChild=L;}
void SetRight(BinaryTreeNode<T> *R){RightChild=R;}
private:
T data;
BinaryTreeNode<T> *LeftChild, *RightChild;
};
//二叉树类
template<class T>
class BinaryTree{
public:
BinaryTree(){root=NULL;treeSize=0;}
BinaryTree(const string& str);
BinaryTreeNode<T>* CreatePreIn(const char *pre,const char *in,int pl,int pr,int il,int ir);
BinaryTree(string &pre,string &in){
root=CreatePreIn(pre.c_str(),in.c_str(),0,pre.length(),0,in.length());
treeSize=pre.length();
}
//~BinaryTree(){Erase();}
bool IsEmpty(){return (root ?false:true);}
void preOrder(BinaryTreeNode<T>* t);//前序遍历
void inOrder(BinaryTreeNode<T>* t);//中序遍历
void postOrder(BinaryTreeNode<T>* t);//后序遍历
void levelOrder();//层次遍历
void visit(BinaryTreeNode<T>* t);//访问节点
BinaryTreeNode<T>* getRoot(){return root;}
int getSize(){
return treeSize;
}
int height(BinaryTreeNode<T>* t);
private:
int treeSize;
BinaryTreeNode<T> *root;
};
//通过字符串构造二叉树
template<class T>
BinaryTree<T>::BinaryTree(const string& str){
int length=str.length();
treeSize=length;
queue<BinaryTreeNode<T>*> qu;
root=new BinaryTreeNode<T>();
BinaryTreeNode<T> *pointer=root;
qu.push(pointer);
for(int i=1;i<=str.length();i++){
pointer=qu.front();
qu.pop();
pointer->data=str[i-1];
if(length>=2*i) pointer->LeftChild=new BinaryTreeNode<T>();
if(length>=2*i+1) pointer->RightChild=new BinaryTreeNode<T>();
qu.push(pointer->LeftChild);
qu.push(pointer->RightChild);
//cout<<pointer->data<<endl;
}
cout<<"构造完毕"<<endl;
}
template<class T>//访问函数
void BinaryTree<T>::visit(BinaryTreeNode<T> *t){
cout<<t->data<<" ";
}
template<class T>//前序遍历
void BinaryTree<T>::preOrder(BinaryTreeNode<T> *t){
if(t!=NULL){
BinaryTree::visit(t);
preOrder(t->LeftChild);
preOrder(t->RightChild);
}
}
template<class T>//中序遍历
void BinaryTree<T>::inOrder(BinaryTreeNode<T> *t){
if(t!=NULL){
inOrder(t->LeftChild);
BinaryTree::visit(t);
inOrder(t->RightChild);
}
}
template<class T>//后序遍历
void BinaryTree<T>::postOrder(BinaryTreeNode<T> *t){
if(t!=NULL){
postOrder(t->LeftChild);
postOrder(t->RightChild);
BinaryTree::visit(t);
}
}
template<class T>//层次遍历
void BinaryTree<T>::levelOrder(){
queue<BinaryTreeNode<T>*> qu;
BinaryTreeNode<T>* pointer;
qu.push(root);
while(qu.size()!=0){
pointer=qu.front();
qu.pop();
BinaryTree::visit(pointer);
if(pointer->LeftChild!=NULL){
qu.push(pointer->LeftChild);
}
if(pointer->RightChild!=NULL){
qu.push(pointer->RightChild);
}
}
}
template<class T>//获取高度
int BinaryTree<T>::height(BinaryTreeNode<T>* t){
if(t==NULL){
return 0;
}
int hl=height(t->LeftChild);
int hr=height(t->RightChild);
if(hl>hr){
return ++hl;
}else{
return ++hr;
}
}
template<class T>//通过前序和中序构造二叉树
BinaryTreeNode<T>* BinaryTree<T>::CreatePreIn(const char *pre,const char *in,int pl,int pr,int il,int ir){
if(pl>pr) return NULL;
if(il>ir) return NULL;
T val=pre[pl];
if(val==NULL) return NULL;
BinaryTreeNode<T> *rt=new BinaryTreeNode<T>(pre[pl]);
int i=il;
while((in[i]!=val)&&(i<=ir)) i++;
//cout<<pl<<" "<<pr<<" "<<il<<" "<<ir<<" "<<"val:"<<val<<" i:"<<i<<endl;
rt->SetLeft(CreatePreIn(pre,in,pl+1,pl+i-il,il,i-1));
rt->SetRight(CreatePreIn(pre,in,pl+i+1-il,pr,i+1,ir));
return rt;
}
int main(){
string str;
cout<<"请输入你想要创建的二叉树的字符串:"<<endl;
cin>>str;
BinaryTree<char> bin(str);
cout<<"前序遍历:";
bin.preOrder(bin.getRoot());
cout<<endl;
cout<<"中序遍历:";
bin.inOrder(bin.getRoot());
cout<<endl;
cout<<"后序遍历:";
bin.postOrder(bin.getRoot());
cout<<endl;
cout<<"节点数:";
cout<<bin.getSize()<<endl;
cout<<"高度:";
cout<<bin.height(bin.getRoot())<<endl;
cout<<"请输入你想要创建的第二个二叉树的前序遍历和中序遍历:"<<endl;
string pre,in;
cin>>pre;cin>>in;
BinaryTree<char> bin2(pre,in);
cout<<"后序遍历:";
bin2.postOrder(bin2.getRoot());
cout<<endl;
cout<<"层次遍历:";
bin2.levelOrder();
cout<<endl;
system("pause");
}
树的遍历方式总结(山东大学数据结构2016级实验6)
最新推荐文章于 2023-04-25 19:16:25 发布