滿二叉樹建立過程中的問題
下面是我寫的代碼,有個問題是(解釋:滿二叉樹建立中,由我們輸入深度D)為什么找到了父節(jié)點(diǎn)的地址,但是返回值為空,希望回答的人看完程序和運(yùn)行結(jié)果回答,不知道是代碼的問題還是軟件的問題
#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("已經(jīng)找到%d節(jié)點(diǎn)地址為 %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("新節(jié)點(diǎn)重新生成地址為%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節(jié)點(diǎn)返回的地址%d\n",parent,np);
? ? ?if(np==NULL )
{
printf("bomm");return (0);
? ? ?}
? ? ? ?pnew->pParent=np;
? np->pLeft=pnew;
? ??
? ? ?printf("成功建立節(jié)點(diǎn)%d \n",i);
?
} ?
int add2(pNode root,int parent,int i)
{ ? ?pNode np;
? ? ?pNode pnew;
? ? ?pnew=(pNode)malloc(sizeof(Node));printf("新節(jié)點(diǎn)重新生成地址為%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節(jié)點(diǎn)返回的地址%d\n",parent,np);
? ? ?if(np==NULL )
{
? printf("bomm");return (0);
? ? ?}
? ? ? pnew->pParent=np;
np->pRight=pnew;
? ??
? ? ? printf("成功建立節(jié)點(diǎn)%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;
}
2015-10-23
至于關(guān)于建立樹的方法的方面,也希望有大神在解決了我的問題之后相互交流一下,
2015-10-23
建樹方法有誤