#include<iostream>
using namespace std;
typedef char Elemtype;
struct BiNode{
Elemtype date;
BiNode *lchild,*rchild;
};
typedef BiNode *Bitree;
//树的初始化
void Inittree(Bitree &bt)
{
bt=NULL;
}
//树的先序创建
void Create(Bitree &bt)
{
//abc##d##ef##g##
Elemtype d;
cin>>d;
if(d=='#') bt=NULL;
else
{
bt=new BiNode;
bt->date=d;
Create(bt->lchild);
Create(bt->rchild);
}
}
//递归
//树的先序遍历
void preOrder(Bitree &bt)
{
if(bt)
{
cout<<bt->date<<" ";
preOrder(bt->lchild);
preOrder(bt->rchild);
}
}
//树的中序遍历
void inOrder(Bitree &bt)
{
if(bt)
{
inOrder(bt->lchild);
cout<<bt->date<<" ";
inOrder(bt->rchild);
}
}
//树的后序遍历
void postOrder(Bitree &bt)
{
if(bt)
{
postOrder(bt->lchild);
postOrder(bt->rchild);
cout<<bt->date<<" ";
}
}
//非递归
//非递归先序遍历
void preIterOrder(Bitree
【数据结构】二叉树的定义,递归遍历,非递归遍历,层次遍历,深度等
最新推荐文章于 2023-02-15 12:59:49 发布