题目
假设二叉树中每个结点值为单个字符,采用二叉链存储结构存储。设计一个算法,求二叉树b中第k层上的结点个数。
代码
#include <iostream>
using namespace std;
#define MaxSize 100
typedef char ElemType;
typedef struct tnode{
ElemType data;
struct tnode *lchild,*rchild;
}BTNode;
typedef struct{
BTNode* data[MaxSize];
int top;
}StackBT;
void InitStack(StackBT &st){
st.top=-1;
}
void DestroyStack(StackBT st){
}
int Push(StackBT &st,BTNode* x){
if(st.top==MaxSize-1)
return 0;
else{
st.top++;
st.data[st.top]=x;
return 1;
}
}
int Pop(StackBT &st,BTNode* x){
if(st.top==-1)
return 0;
else{
x=st.data[st.top];
st.top--;
return 1;
}
}
int GetTop(StackBT st,BTNode* &x){
if(st.top==-1)
return 0;
else{
x=st.data[st.top];
return 1;
}
}
int StackEmpty(StackBT st){
if(st.top==-1) return 1;
else return 0;
}
void DestroyBTree(BTNode *&bt){
if(bt!=NULL){
DestroyBTree(bt->lchild);
DestroyBTree(bt->rchild);
delete bt;
}
}
int BTHeight(BTNode *bt){
int lchilddep,rchilddep;
if(bt==NULL) return 0;
else{
lchilddep=BTHeight(bt->lchild);
rchilddep=BTHeight(bt->rchild);
return (lchilddep>rchilddep)?(lchilddep+1):(rchilddep+1);
}
}
int NodeCount(BTNode *bt){
int num1,num2;
if(bt==NULL)
return 0;
else{
num1=NodeCount(bt->lchild);
num2=NodeCount(bt->rchild);
return (num1+num2+1);
}
}
int LeafCount(BTNode *bt){
int num1,num2;
if(bt==NULL)
return 0;
else if (bt->lchild==NULL&&bt->rchild==NULL)
return 1;
else{
num1=LeafCount(bt->lchild);
num2=LeafCount(bt->rchild);
return (num1+num2);
}
}
void DispBTree(BTNode *bt){
if (bt!=NULL){
cout<<bt->data;
if (bt->lchild!=NULL || bt->rchild!=NULL){
cout<<"(";
DispBTree(bt->lchild);
if (bt->rchild!=NULL)
cout<<",";
DispBTree(bt->rchild);
cout<<")";
}
}
}
void CreateBTree(BTNode * &bt,char *str)
{ BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
bt=NULL;
ch=str[j];
while (ch!='\0')
{ switch(ch)
{
case '(':top++;St[top]=p;k=1; break;
case ')':top--;break;
case ',':k=2; break;
default:p=new BTNode();
p->data=ch;p->lchild=p->rchild=NULL;
if (bt==NULL)
bt=p;
else
{ switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
void LevelCount(BTNode *bt,int n){
BTNode *p=NULL,*temp[MaxSize];
int layer=1,count=0,i=0,m=0,f=0;
p=bt;
temp[0]=NULL;
temp[++i]=p;
if(n==1 && p!=NULL){
cout<<1<<endl;
return ;
}
else if(p==NULL){
cout<<0<<endl;
return ;
}
while(1){
if(n==layer){
cout<<count<<endl;
break;
}
f=0;
count=0;
layer++;
while(m+f!=i){
if(temp[1+m+f]->lchild!=NULL){
count++;
}
if(temp[1+m+f]->rchild!=NULL){
count++;
}
f++;
}
for(int k=0;k<f;k++){
if(temp[1+m+k]->lchild!=NULL){
temp[++i]=temp[1+m+k]->lchild;
}
if(temp[1+m+k]->rchild!=NULL){
temp[++i]=temp[1+m+k]->rchild;
}
}
m=m+f;
}
}
void LevelOrder(BTNode *bt){
BTNode *p;
BTNode *qu[MaxSize];
int front,rear;
front=rear=0;
rear++;qu[rear]=bt;
int i=-1;
while(front!=rear){
front=(front+1)%MaxSize;
p=qu[front];
cout<<p->data;
if(p->lchild!=NULL){
rear=(rear+1)%MaxSize;
qu[rear]=p->lchild;
}
if(p->rchild!=NULL){
rear=(rear+1)%MaxSize;
qu[rear]=p->rchild;
}
}
}
int main(){
char tree[MaxSize];
cin>>tree;
int n;
cin>>n;
BTNode *bt;
CreateBTree(bt,tree);
LevelCount(bt,n);
DestroyBTree(bt);
return 0;
}
|