📄 层序遍历二叉树.txt
字号:
#include"stdio.h"
#include"stdlib.h"
typedef char ElemType;
typedef struct CSNode
{
ElemType data;
struct CSNode *firstchild,*nextsibling;
}CSNode,*CSTree;
/*访问当前节点*/
void Visit(CSNode *p)
{
if(p)
{
printf("%2c\n",p->data);
}
}
/*按前序次序建立二叉树*/
void BinTreeCreate(CSTree *BT)
{
ElemType ch;
printf("Please input elemnt:\n");
scanf("%c",&ch);
fflush(stdin);
if(ch=='$')
{
*BT=NULL;
}
else
{
*BT=(CSNode *)malloc(sizeof(CSNode));
if(!(*BT))
{
printf("OVERFLOW!\n");
exit(1);
}
(*BT)->data=ch;
BinTreeCreate(&(*BT)->firstchild);
BinTreeCreate(&(*BT)->nextsibling);
}
}
/*按层次遍历二叉树*/
void LayOrder(CSTree *BT)
{
int depth;
int counter;
CSNode *fc;
CSNode *ns;
if(*BT)
{
fc=*BT;
depth=0;
counter=0;
while(fc)
{
ns=fc;
while(ns)
{
counter++;
Visit(ns);
ns=ns->nextsibling;
}
depth++;
fc=fc->firstchild;
}
}
else
{
printf("BitTree is Empty!\n");
}
}
void main()
{
CSTree BT;
BinTreeCreate(&BT);
if(BT!=NULL)
{
printf("NULL");
}
LayOrder(&BT);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -