早教吧作业答案频道 -->其他-->
以二叉链表为存储结构,分别写出前序、中序、后序遍历二叉树的递归与非递归算法。(数据结构)
题目详情
以二叉链表为存储结构,分别写出前序、中序、后序遍历二叉树的递归与非递归算法。(数据结构)
▼优质解答
答案和解析
#include
#include
typedef char ElemType;
struct BTreeNode
{
ElemType data;
BTreeNode*left;
BTreeNode*right;
};
void InitBTree(BTreeNode*& BT)
{
BT=NULL;
}
void CreateBTree(BTreeNode*& BT,char*a)
{
const int MaxSize=1000;
BTreeNode*s[MaxSize];
int top=-1;
BT=NULL;
BTreeNode*p;
int k;
int i=0;
while (a[i])
{
switch(a[i])
{
case ' ':
break;
case'(':
if(top==MaxSize-1)
{
cout< exit(1);
}
top++; s[top]=p;k=1;
break;
case ')':
if(top==-1)
{
cout< }
top--;
break;
case ',':
k=2;
break;
default:
p=new BTreeNode;
p->data=a[i];p->left=p->right=NULL;
if(BT==NULL) BT=p;
else
{
if(k==1) s[top]->left=p;
else s[top]->right=p;
}
}
i++;
}
}
bool EmptyBTree (BTreeNode*BT)
{
return BT==NULL;
}
void PreOrder(BTreeNode* BT)
{
if(BT!=NULL)
{
cout<data< PreOrder(BT->left);
PreOrder(BT->right);
}
}
void InOrder(BTreeNode* BT)
{
if(BT!=NULL)
{
InOrder(BT->left);
cout<data< InOrder(BT->right);
}
}
void PostOrder(BTreeNode* BT)
{
if(BT!=NULL)
{
PostOrder(BT->left);
PostOrder(BT->right);
cout<data< }
}
void LevelOrder(BTreeNode* BT)
{
const int MaxSize=30;
BTreeNode* q[MaxSize];
int front=0,rear=0;
BTreeNode* p;
if(BT!=NULL)
{
rear=(rear+1)%MaxSize;
q[rear]=BT;
}
while (front!=rear)
{
front=(front+1)%MaxSize;
p=q[front];
cout<data< if(p->left!=NULL)
{
rear=(rear+1)%MaxSize;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%MaxSize;
q[rear]=p->right;
}
}
}
void PrintBTree(BTreeNode*BT)
{
if(BT!=NULL)
{
cout<data;
if(BT->left!=NULL || BT->right!=NULL )
{
cout< PrintBTree(BT->left);
if(BT->right!=NULL)
cout< PrintBTree(BT->right);
cout< }
}
}
void ClearBTree(BTreeNode*&BT)
{
if(BT!=NULL)
{
ClearBTree(BT->left);
ClearBTree(BT->right);
delete BT;
BT=NULL;
}
}
void main()
{
BTreeNode* bt;
InitBTree(bt);
char b[50];
cout< cin.getline(b,sizeof(b));
CreateBTree(bt,b);
PrintBTree(bt); cout< cout< cout< cout< cout< }
#include
typedef char ElemType;
struct BTreeNode
{
ElemType data;
BTreeNode*left;
BTreeNode*right;
};
void InitBTree(BTreeNode*& BT)
{
BT=NULL;
}
void CreateBTree(BTreeNode*& BT,char*a)
{
const int MaxSize=1000;
BTreeNode*s[MaxSize];
int top=-1;
BT=NULL;
BTreeNode*p;
int k;
int i=0;
while (a[i])
{
switch(a[i])
{
case ' ':
break;
case'(':
if(top==MaxSize-1)
{
cout< exit(1);
}
top++; s[top]=p;k=1;
break;
case ')':
if(top==-1)
{
cout< }
top--;
break;
case ',':
k=2;
break;
default:
p=new BTreeNode;
p->data=a[i];p->left=p->right=NULL;
if(BT==NULL) BT=p;
else
{
if(k==1) s[top]->left=p;
else s[top]->right=p;
}
}
i++;
}
}
bool EmptyBTree (BTreeNode*BT)
{
return BT==NULL;
}
void PreOrder(BTreeNode* BT)
{
if(BT!=NULL)
{
cout<
PreOrder(BT->right);
}
}
void InOrder(BTreeNode* BT)
{
if(BT!=NULL)
{
InOrder(BT->left);
cout<
}
}
void PostOrder(BTreeNode* BT)
{
if(BT!=NULL)
{
PostOrder(BT->left);
PostOrder(BT->right);
cout<
}
void LevelOrder(BTreeNode* BT)
{
const int MaxSize=30;
BTreeNode* q[MaxSize];
int front=0,rear=0;
BTreeNode* p;
if(BT!=NULL)
{
rear=(rear+1)%MaxSize;
q[rear]=BT;
}
while (front!=rear)
{
front=(front+1)%MaxSize;
p=q[front];
cout<
{
rear=(rear+1)%MaxSize;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%MaxSize;
q[rear]=p->right;
}
}
}
void PrintBTree(BTreeNode*BT)
{
if(BT!=NULL)
{
cout<
if(BT->left!=NULL || BT->right!=NULL )
{
cout< PrintBTree(BT->left);
if(BT->right!=NULL)
cout< PrintBTree(BT->right);
cout< }
}
}
void ClearBTree(BTreeNode*&BT)
{
if(BT!=NULL)
{
ClearBTree(BT->left);
ClearBTree(BT->right);
delete BT;
BT=NULL;
}
}
void main()
{
BTreeNode* bt;
InitBTree(bt);
char b[50];
cout< cin.getline(b,sizeof(b));
CreateBTree(bt,b);
PrintBTree(bt); cout<
看了 以二叉链表为存储结构,分别写...的网友还看了以下:
高二化学1.有机化学方程式是不是写结构式,结构简式,分子式都可以2.分别将乙炔和苯点燃,可看到的相 2020-05-14 …
(1)分子式为C4H8的烯烃有3种同分异构体,其结构简式分别为;、和.(2)某同学为某烃命名为“1 2020-05-14 …
有机物熔沸点(一)分子式相同的烃为什么支链越多,沸点越低?为什么分子对称性越高,熔点越高(二)同分 2020-05-16 …
乙烷中,其一氯代物有2种,则它的结构简式为:丙烷的二氯代物有种,其结构简式分别为: 2020-05-24 …
丙二酸的结构简式为:,已知一个丙二酸分子脱去两分子水后的产物为丙二酸酐.该分子具链状结构.(1)丙 2020-07-01 …
急急急,几道高一简单的化学题哦1.某烃的相对分子质量为106,其分子式为(),若分子中有一个苯环, 2020-07-09 …
(1)硫酸根离子SO42-连四硫酸根离子S4O62-的结构式可分硫代硫酸根和连二硫酸根离子S2O6 2020-07-17 …
系表结构造句以及problem和question的区别和联系—soundfeellookseems 2020-07-22 …
4.在一个圆上标出一些数:第一次先把圆周二等分,在两个分点分别标上2和4.第二次把两段半弧分别二等分 2020-11-19 …
关于as...as...的一个句子theywereasfitinmanyaspectaswerehe 2020-12-02 …