?? 樹的高度、葉子節點數.c
字號:
#include<stdlib.h>
#include<stdio.h>
static int NUM=0;
static int DEEP=0;
static int TEMP=0;
#define m0 100 //定義節點最大個數
#define Len sizeof (BinNode) //定義存儲單元空間
typedef struct BinNode //定義節點類型
{
char data;
struct BinNode *fch,*nsb;
} BinNode,*Bintree;
struct chtp
{
int len;
char ch[m0];
};
struct BinNode *bt,*m;
struct chtp s;
int top=0,i=0;
void crt_pre(Bintree *t) //遞歸建立樹
{
char c;
c=s.ch[i];
i=i+1;
if (c=='.')
*t=NULL;
else
{
*t=(BinNode *)malloc(Len);
(*t)->data=c;
crt_pre(&(*t)->fch);
crt_pre(&(*t)->nsb);
};
}
void preorder(BinNode *t,int temp)
{
int top=temp;
if(t!=NULL)
{
TEMP=temp+1;
if(t->fch==NULL)
{
NUM++;
if(TEMP>DEEP)DEEP=TEMP;
}
printf("%c",t->data);
preorder(t->fch,top+1);
preorder(t->nsb,top);
}
}
void main()
{
char /*c[]={'a','b','d','.','.','.','c','e','.','.','f','.','.','!'};*/
c[]={'a','b','.','c','d','.','.','e','.','.','.','!'};
int i=0;
for(i=0,s.len=0;c[i]!='!';i++,s.len++)
s.ch[i]=c[i];
crt_pre(&bt);
preorder(bt,0);
printf("\n");
printf("高度為:%d\n",DEEP);
printf("葉子數為:%d",NUM);
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -