#pragma once
typedef char ElemType;
typedef struct BtNode
{
struct BtNode* leftchild;
struct BtNode* rightchild;
ElemType val;
}BtNode,*BinaryTree;
//添加元素
struct BtNode* BuyNode();
//创建二叉树
struct BtNode* CreateTree1();
struct BtNode* CreateTree2(const char *&str);
//递归遍历
void PreOrder(struct BtNode* p);
void InOrder(struct BtNode* p);
void PastOrder(struct BtNode* p);
//非递归方式遍历二叉树
void NicePreOrder(struct BtNode *s);
void NiceInOrder(struct BtNode *s);
void NicePastOrder(struct BtNode *s);
//先序和中序创建树
struct BtNode* CreateTreePI(char *pstr,char *istr,int len);
//中序和后序创建二叉树
struct BtNode* CreateTreeIL(char *istr,char *lstr,int len);
//层次遍历
void LevelOrder1(struct BtNode *p);//ABGCDHEF
void LevelOrder2(struct BtNode *p);//ABGCDHEF
二叉树的结构体:
typedef char ElemType;
typedef struct BtNode
{
struct BtNode* leftchild;
struct BtNode* rightchild;
ElemType val;
}BtNode,*BinaryTree;
添加元素,初始化:
//初始化,添加元素
struct BtNode* BuyNode()
{
struct BtNode *p=(struct BtNode*)malloc(sizeof(struct BtNode));
if(NULL==p)
{
exit(1);
}
memset(p,0,sizeof(struct BtNode));
return p;
}
//创建二叉树
struct BtNode* CreateTree1()
{
//输入值,创建结点
ElemType val;
cin>>val;
struct BtNode *p=NULL;
if(val!=END)
{
p=BuyNode();
p->val=val;
p->leftchild=CreateTree1();
p->rightchild=CreateTree1();
}
return p;
}
struct BtNode* CreateTree2(const char *&str)
{
struct BtNode* p=NULL;
if(str!=NULL && *str!=END)
{
p=BuyNode();
p->val=*str;
p->leftchild=CreateTree2(++str);
p->rightchild=CreateTree2(++str);
}
return p;
}
void PreOrder(struct BtNode* p)
{
if(p!=NULL)
{
cout<<p->val<<" ";
PreOrder(p->leftchild);
PreOrder(p->rightchild);
}
}
void InOrder(struct BtNode* p)
{
if(p!=NULL)
{
InOrder(p->leftchild);
cout<<p->val<<" ";
InOrder(p->rightchild);
}
}
void PastOrder(struct BtNode* p)
{
if(p!=NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout<<p->val<<" ";
}
}
##非 递归遍历二叉树:
void NicePreOrder(struct BtNode *s)
{
if(s==NULL) return;
stack<struct BtNode*> st;
st.push(s);
while(!st.empty())
{
s=st.top();
st.pop();
cout<<s->val<<" ";
if(s->rightchild!=NULL)
{
st.push(s->rightchild);
}
if(s->leftchild!=NULL)
{
st.push(s->leftchild);
}
}
}
void NiceInOrder(struct BtNode *s)
{
if(s==NULL) return;
stack<struct BtNode*> st;
while(!st.empty() || s!=NULL)
{
while(s!=NULL)
{
st.push(s);
s=s->leftchild;
}
s=st.top();
st.pop();
cout<<s->val<<" " ;
s=s->rightchild;
}
}
void NicePastOrder(struct BtNode *s)
{
if(s==NULL) return;
stack<struct BtNode*> st;
struct BtNode *tag;
while(!st.empty() || s!=NULL)
{
while(s!=NULL)
{
st.push(s);
s=s->leftchild;
}
s=st.top();
st.pop();
if(s->rightchild==NULL || s->rightchild==tag)
{
cout<<s->val<<" ";
tag=s;
s=NULL;
}
else
{
st.push(s);
s=s->rightchild;
}
}
}
//先序和中序创建树
int FindIndex(char *istr,int len,ElemType target)
{
int i=0;
for(i;i<len;i++)
{
if(istr[i]==target)
{
return i;
}
}
return -1;
}
struct BtNode* CreateTreePI(char *pstr,char *istr,int len)
{
struct BtNode *s=NULL;
if(len>0)
{
//生成结点
s=BuyNode();
//将数据放入结点
s->val=pstr[0];
//寻找中序结点的位置
int pos=FindIndex(istr,len,pstr[0]);
if(pos==-1)
{
exit(1);
}
//构建左孩子
return s->leftchild=CreateTreePI(pstr+1,istr,pos);
//构建右孩子
return s->rightchild=CreateTreePI(pstr+1+pos,istr+pos+1,len-1-pos);
}
}
struct BtNode* CreateTreeIL(char *istr,char *lstr,int len)
{
struct BtNode *s=NULL;
if(len>0)
{
//生成结点
s=BuyNode();
//将数据放入结点
s->val=lstr[len-1];
//寻找中序结点的位置
int pos=FindIndex(istr,len,lstr[len-1]);
if(pos<0)
{
exit(1);
}
//创建左孩子
return s->leftchild=CreateTreeIL(istr,lstr,pos);
//创建右孩子
return s->rightchild=CreateTreeIL(istr+pos+1,lstr+1+pos,len-1-pos);
}
}
void LevelOrder1(struct BtNode *p)//ABGCDHEF
{
if(p==NULL) return;
queue<struct BtNode*> que;
que.push(p);
while(!que.empty())
{
p=que.front();
que.pop();
cout<<p->val<<" ";
if(p->leftchild!=NULL)
{
que.push(p->leftchild);
}
if(p->rightchild!=NULL)
{
que.push(p->rightchild);
}
}
cout<<endl;
}
-AGBCDHFE
void LevelOrder2(struct BtNode *p)//AGBCDHFE
{
if(p==NULL) return;
stack<struct BtNode*>st;
queue<struct BtNode*>que;
st.push(p);
while(!st.empty() || !que.empty())
{
while(!st.empty())
{
p=st.top();
st.pop();
cout<<p->val<<" ";
if(p->rightchild!=NULL)
{
que.push(p->rightchild);
}
if(p->leftchild!=NULL)
{
que.push(p->leftchild);
}
}
while(!que.empty())
{
p=que.front();
que.pop();
cout<<p->val<<" ";
if(p->rightchild!=NULL)
{
st.push(p->rightchild);
}
if(p->leftchild!=NULL)
{
st.push(p->leftchild);
}
}
}
cout<<endl;
}
main函数:
#include
#include "Tree.h"
using namespace std;
int main()
{
BinaryTree root=NULL;
const char *str="ABC##DE##F##G#H##";
//root=CreateTree1();
root=CreateTree2(str);
NicePreOrder(root);
cout<<endl;
NiceInOrder(root);
cout<<endl;
NicePastOrder(root);
cout<<endl;
char *pstr="ABCDEFGH";//先序
char *istr="CBEDFAGH";//中序
char *lstr="CEFDBHGA";//后序
int len=strlen(pstr);
CreateTreeIL(pstr,istr,len);
NicePreOrder(root);
cout<<endl;
LevelOrder1(root);
LevelOrder2(root);
return 0;
}