📄 11þ-
字号:
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#define OVERFLOW 0
#define STACKSIZE 50
#define ADD 20
typedef struct CSNode
{
char data;
struct CSNode *firstchild;
struct CSNode *nextsibling;
}CSNode,* CSTree;
void CreateCSTree(CSTree &T) //先序扩展序列建立树的递归算法
{
char ch;
scanf("%c",&ch);
if (ch=='.')
{
T=NULL;
}
else
{
if(!(T=(CSNode*)malloc(sizeof(CSNode))))
{
exit(OVERFLOW);
}
T->data=ch;
CreateCSTree(T->firstchild);
CreateCSTree(T->nextsibling);
}
}
int Depth(CSTree T)//递归
{ int dep1,dep2;
if(T==NULL) return(0);
else
{ dep1=Depth(T->firstchild);//求第一个孩子子树的深度
dep2=Depth(T->nextsibling);//求下一个兄弟子树的深度
if(dep1+1>dep2) return(dep1+1);//树的深度为"max(左深+1,右深)"
else return(dep2);
}
}
void main()
{ CSTree T=NULL;
printf("请输孩子兄弟法输入树,当子树为空时,用.来代替:\n");
CreateCSTree(T);
if(T){
printf("此树的深度为:");
printf("%d",Depth(T));
printf("\n");
}
else printf("树为空!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -