用层序遍历输出求度为一的节点数.求大佬帮我看我的函数该怎么改
#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
using namespace std;
#define OK 1
#define NodeNum 5
#define ERROR -1
typedef int Elemtype;
typedef int Status;
typedef struct Bitnode{
Elemtype data;
struct Bitnode *lchild,*rchild;
}Bitnode,*Bitree;
typedef struct QNode
{
Bitree base;
int front;
int rear;
}LinkQueue;
int QueueInit(LinkQueue &Q)
{
Q.base = (Bitnode *)malloc((NodeNum+3) * sizeof(Bitnode));//队列基址指针
if(NULL == Q.base)//若分配内存失败
{
cout<<"存储分配失败!"<<endl<<endl;
return -1;
}
Q.front = Q.rear = 0;//初始化队头和队尾指针
return 1;
}
int QueueIn(LinkQueue &Q,Bitree node)
{
if(Q.front == (Q.rear+1)%(NodeNum+3))
{
cout<<"队列已满!"<<endl<<endl;
return -1;
}
Q.base[Q.rear] = *node;
Q.rear = (Q.rear + 1)%(NodeNum+3);//队尾指针前进一位
return 1;
}
int QueueEmpty(LinkQueue &Q)
{
if(Q.front==Q.rear)
return ERROR;
return OK;
}
int QueueOut(LinkQueue &Q)
{
Bitnode p;
if(Q.rear == Q.front)
{
cout<<"队列已空!"<<endl<<endl;
return -1;
}
p = Q.base[Q.front];
return p.data;
Q.front = (Q.front + 1)%(NodeNum+3);//队头指针前进一位
}
char ch;
void Inordertraverse(Bitree T)
{
if(T)
{
Inordertraverse(T->lchild);
cout<<(char)T->data<<" ";
Inordertraverse(T->rchild);
}
}
void Createbitree(Bitree &T)
{
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=new Bitnode;
T->data=ch;
Createbitree(T->lchild);
Createbitree(T->rchild);
}
#include<stdlib.h>
#include<bits/stdc++.h>
using namespace std;
#define OK 1
#define NodeNum 5
#define ERROR -1
typedef int Elemtype;
typedef int Status;
typedef struct Bitnode{
Elemtype data;
struct Bitnode *lchild,*rchild;
}Bitnode,*Bitree;
typedef struct QNode
{
Bitree base;
int front;
int rear;
}LinkQueue;
int QueueInit(LinkQueue &Q)
{
Q.base = (Bitnode *)malloc((NodeNum+3) * sizeof(Bitnode));//队列基址指针
if(NULL == Q.base)//若分配内存失败
{
cout<<"存储分配失败!"<<endl<<endl;
return -1;
}
Q.front = Q.rear = 0;//初始化队头和队尾指针
return 1;
}
int QueueIn(LinkQueue &Q,Bitree node)
{
if(Q.front == (Q.rear+1)%(NodeNum+3))
{
cout<<"队列已满!"<<endl<<endl;
return -1;
}
Q.base[Q.rear] = *node;
Q.rear = (Q.rear + 1)%(NodeNum+3);//队尾指针前进一位
return 1;
}
int QueueEmpty(LinkQueue &Q)
{
if(Q.front==Q.rear)
return ERROR;
return OK;
}
int QueueOut(LinkQueue &Q)
{
Bitnode p;
if(Q.rear == Q.front)
{
cout<<"队列已空!"<<endl<<endl;
return -1;
}
p = Q.base[Q.front];
return p.data;
Q.front = (Q.front + 1)%(NodeNum+3);//队头指针前进一位
}
char ch;
void Inordertraverse(Bitree T)
{
if(T)
{
Inordertraverse(T->lchild);
cout<<(char)T->data<<" ";
Inordertraverse(T->rchild);
}
}
void Createbitree(Bitree &T)
{
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=new Bitnode;
T->data=ch;
Createbitree(T->lchild);
Createbitree(T->rchild);
}
}
int countone(Bitree bt)
{
int num=0;
LinkQueue Q;
Bitree p;
if(bt){QueueInit(Q);
QueueIn(Q,bt);}
while(!QueueEmpty(Q))
{p=QueueOut(Q); printf(p->data); //
if(p->lchild && !p->rchild ||!p->lchild && p->rchild)num++;//
if(p->lchild) QueueIn(Q,p->lchild); //
if(p->rchild) QueueIn(Q,p->rchild); //
}
}
if(bt)
return num;
}
{
int num=0;
LinkQueue Q;
Bitree p;
if(bt){QueueInit(Q);
QueueIn(Q,bt);}
while(!QueueEmpty(Q))
{p=QueueOut(Q); printf(p->data); //
if(p->lchild && !p->rchild ||!p->lchild && p->rchild)num++;//
if(p->lchild) QueueIn(Q,p->lchild); //
if(p->rchild) QueueIn(Q,p->rchild); //
}
}
if(bt)
return num;
}