满二叉树建立过程中的问题
下面是我写的代码,有个问题是(解释:满二叉树建立中,由我们输入深度D)为什么找到了父节点的地址,但是返回值为空,希望回答的人看完程序和运行结果回答,不知道是代码的问题还是软件的问题
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <malloc.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct _Node
{
int id;
struct _Node *pParent,*pLeft,*pRight;
}*pNode,Node;
typedef struct _PBtree
{
pNode root;
}*pBtree,Btree;
pNode find(pNode m,int i)
{ pNode node;
if(m->id==i) {printf("已经找到%d节点地址为 %d\n",i,m);return (m);
}
if(m->pLeft!=NULL)
find(m->pLeft,i);
if(m->pRight!=NULL)
find(m->pRight,i);
}
int add1(pNode root,int parent,int i)
{ pNode np;
pNode pnew;
pnew=(pNode)malloc(sizeof(Node));printf("新节点重新生成地址为%d\n",pnew);
pnew->id=i;
printf("%d传输成功\n",pnew->id);
pnew->pParent=NULL;
pnew->pLeft=NULL;
pnew->pRight=NULL;
np=find(root,parent);
printf("%d节点返回的地址%d\n",parent,np);
if(np==NULL )
{
printf("bomm");return (0);
}
pnew->pParent=np;
np->pLeft=pnew;
printf("成功建立节点%d \n",i);
}
int add2(pNode root,int parent,int i)
{ pNode np;
pNode pnew;
pnew=(pNode)malloc(sizeof(Node));printf("新节点重新生成地址为%d\n",pnew);
pnew->id=i;
printf("%d传输成功\n",pnew->id);
pnew->pParent=NULL;
pnew->pLeft=NULL;
pnew->pRight=NULL;
np=find(root,parent);
printf("%d节点返回的地址%d\n",parent,np);
if(np==NULL )
{
printf("bomm");return (0);
}
pnew->pParent=np;
np->pRight=pnew;
printf("成功建立节点%d \n",i);
return (0);
}
void browse(pNode i)
{
printf("%d ",i->id);
if (i->pLeft!=NULL) browse(i->pLeft);
if (i->pRight!=NULL) browse(i->pRight);
}
int main(int argc, char** argv) {
int D,n,j;
scanf("%d",&D);
pBtree pbtree;
pNode pnode;
pnode=(pNode)malloc(sizeof(Node));
pbtree=(pBtree)malloc(sizeof(Btree));
pbtree->root=pnode;
pnode->id=1;
pnode->pLeft=NULL;
pnode->pRight=NULL;
pnode->pParent=NULL;
j=pow(2,D-1);
for(n=1;n<j;n++)
{
add1(pbtree->root,n,2*n);
add2(pbtree->root,n,2*n+1);
}
browse(pbtree->root);
return 0;
}