内容发布更新时间 : 2025/3/1 16:57:59星期一 下面是文章的全部内容请认真阅读。
WORD格式可编辑 return 0; else if(Depth(T->lchild)>Depth(T->rchild)) return Depth(T->lchild)+1; else return Depth(T->rchild)+1; }
void main() { BiTree T; int sum,dep; T=Create(T); Preorder(T); printf(\ zhongxu(T); printf(\ houxu(T); printf(\ sum=Sumleaf(T); printf(\ dep=Depth(T); printf(\ }
2、写出二叉树的层序遍历函数(选做) #include
#define STACK_MAX 100
typedef struct BiTNode{ char data;
struct BiTNode *lchild,*rchild; }BiTNode,*BiTree;
typedef struct{ int front, rear;
专业知识分享
WORD格式可编辑 BiTNode * data1[STACK_MAX]; } Queue;
void initQueue (Queue *q) /*初始化队列*/ {
q->rear=0; q->front=0; }
int EmptyQueue(Queue *q)/*判断队列空*/ {
if(q->rear==q->front)return 1; else return 0; }
BiTNode * DeQueue(Queue *q) /*出队列*/ {
return q->data1[(q->front)++]; }
void InQueue(Queue *q,BiTNode * p) /*入队列*/ {
if (q->rear==STACK_MAX) { printf(\ } else
q->data1[(q->rear)++]=p; }
BiTree Create(BiTree T) { char ch; ch=getchar(); if(ch=='#') T=NULL; else { if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) printf(\ T->data=ch; T->lchild=Create(T->lchild); T->rchild=Create(T->rchild); } return T;
专业知识分享
WORD格式可编辑 }
Cengxu(BiTree T,Queue * q) { BiTree p; //定义一个暂存节点数据的树形指针 if(T!=NULL) //如果树非空
{
InQueue(q,T); //将根节点入队
while(!EmptyQueue(q)) //