⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 层序遍历二叉树.txt

📁 该程序的功能是实现二叉树结点的类型定义和对二叉树的基本操作。该程序包括二叉树结构类型以及每一种操作的具体的函数定义和主函数。
💻 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 + -