层序遍历二叉树.txt

来自「该程序的功能是实现二叉树结点的类型定义和对二叉树的基本操作。该程序包括二叉树结构」· 文本 代码 · 共 98 行

TXT
98
字号
#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 + =
减小字号Ctrl + -
显示快捷键?