首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

二叉树创建时的指针有关问题

2013-01-22 
二叉树创建时的指针问题部分源代码:typedef char ElemTypetypedef struct node{ElemType datastruct nod

二叉树创建时的指针问题
部分源代码:
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node * lchild;
struct node * rchild;
}node, *Tree;

void CreateTree( Tree tree )
{
tree = ( Tree )malloc( sizeof( struct node ) );
        ......
}

int main( void )
{
Tree bitree;
CreateTree( bitree );

system( "pause" );
return 0;
}
程序报错!
1."The variable bitree is being used without being initialized"
我不明白,在main函数中创建了一个指向node的指针。
在CreateTree中对其进行初始化为什么不行呢?
2.我在网上看了一些程序,
比如这个:
typedef struct treenode  
{  
    char data;  
    int weight;  
    treenode *left;  
    treenode *right;  
}Treenode,*Treep;  
  
//初始化二叉树  
void init_tree(Treep &root)  
{  
    root=NULL;    
}  
我不明白init_tree中的形参的意思?root是什么类型的变量?Treep在这里是将它强制转换吗成Treep型吗?
好纠结啊?
[解决办法]
C语言写法
不要和C++里面的引用(&)搞混了

#include<stdio.h>
#include<stdlib.h>
typedef  struct  btnode{
   char  data;
   struct btnode  *lchild, *rchild;
   }BTNode;
typedef struct{
   BTNode elem[20];
   int front,rear;
   }SqQueue;
BTNode * CreatTree( void );
void  Preorder(BTNode *bt );
void  Inorder(BTNode *bt );
void  Postorder(BTNode *bt );
void  Levelsorder(BTNode *bt);
void  ExChangeLeafs(BTNode *bt);
void  EnQueue(SqQueue *sq,BTNode *bt);
void  DelQueue(SqQueue *sq,BTNode *p);
void  InitQueue(SqQueue *sq);
void Disptree(BTNode *bt);
main()
{
  BTNode *t;
  char s[10];
  for(;;)
  {
      printf("1--------CreatTree    2-----------Preorder  \n");
      printf("3--------Inorder      4-----------Postorder  \n");
      printf("5--------Levelsorder  6-----------ExchangeLeafs(Preorder)  \n");
      printf("7--------Exit\n");
      gets(s);
      switch(*s)
      {
        case '1':  t=CreatTree();
                   break;
case '2':  Preorder(t);
                   printf("\n");
                   break;
        case '3':  Inorder(t);
                   printf("\n");


                   break;
        case '4':  Postorder(t);
                   printf("\n");
                   break;
case '5':  Levelsorder(t);
                   printf("\n");
   break;
case '6':  ExChangeLeafs(t);
   Disptree(t);
                   printf("\n");
                   break;
        case '7':  printf("exit \n");
                   exit(0);
    }
  }
}
 #define  MAXNUM  20
 BTNode * CreatTree( void )
{     int i=1,j;char ch;
      BTNode *p[MAXNUM+1],*t,*s;
      t=NULL;
      printf("\n enter i,ch: \n");
      scanf("%d,%c",&i,&ch);
      while( i!=0 && ch!='#')
     {
       s=(BTNode *)malloc(sizeof(BTNode));
       s->data=ch;
       s->lchild=s->rchild=NULL;
       p[i]=s;
       if(i==1) t=s;
       else
      {
          j=i/2;
          if(i%2==0) p[j]->lchild=s;
          else p[j]->rchild=s;
       }
       scanf("%d,%c",&i,&ch);
      }
      ch=getchar();
      return t;
 }
void  Preorder(BTNode *bt )
{
     if (bt)
     {
       printf("%5c",bt->data);
       Preorder(bt->lchild);
       Preorder(bt->rchild);
      }
 }

void  Inorder(BTNode *bt )
{
    if (bt)
   {
    Inorder(bt->lchild);
    printf("%5c",bt->data);
    Inorder(bt->rchild);
    }
}

void  Postorder(BTNode *bt )
{
     if (bt)
     {
        Postorder(bt->lchild);
        Postorder(bt->rchild);
printf("%5c",bt->data);
      }


 }
void ExChangeLeafs(BTNode *bt)
{
    BTNode *temp;
    if(bt)
    {
      temp=bt->lchild;
      bt->lchild=bt->rchild;
      bt->rchild=temp;
      ExChangeLeafs(bt->lchild);
      ExChangeLeafs(bt->rchild);
     }
}
void  Levelsorder(BTNode *bt)
{  
   BTNode p;
   SqQueue sq;
   InitQueue(&sq);
   EnQueue(&sq,bt);
   while(sq.front!=sq.rear)
  { 
    DelQueue(&sq,&p);
    printf("%5c",p.data);
    if(p.lchild) EnQueue(&sq,p.lchild);
    if(p.rchild) EnQueue(&sq,p.rchild);
   }
}
void InitQueue(SqQueue *sq)
{
  sq->front=sq->rear=0;
}
void  EnQueue(SqQueue *sq ,BTNode *bt)
{ sq->elem[sq->rear]=*bt;
  sq->rear=(sq->rear+1)%20;
}
void  DelQueue(SqQueue *sq,BTNode *p)
{
   *p=sq->elem[sq->front];
   sq->front=(sq->front+1)%20;

}
void Disptree(BTNode *bt)
{
    if(bt)
    {
        printf("%c",bt->data);
        if(bt->lchild 
[解决办法]
 bt->rchild)
        {
            printf("(");
            Disptree(bt->lchild);
            if(bt->rchild)
                printf(",");
            Disptree(bt->rchild);
            printf(")");
        }
    }
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int a;
struct node *lchild,*rchild;
}Binnode;
Binnode *Creattree()
{
Binnode *t;
        int m;
        scanf("%d",&m);
if(m==0) t=NULL;
else{
    t=(Binnode *)malloc(sizeof(Binnode));
t->data=m;
t->lchild=Creattree();
t->rchild=Creattree();
}
return t;
}
//用中序遍历二叉树(递归算法)
void Inorder(Binnode *t)
{
if(t)
{
Inorder(t->lchild);
printf("%2d",t->data);
Inorder(t->rchild);
}
}
//求二叉树高度
int high(Binnode *t)
{
int h,lh,rh;
if(t==NULL)return 0;
else
{
lh=high(t->lchild);
rh=high(t->rchild);
h=lh>rh?lh+1:rh+1;
return h;
}
}
//求二叉树的叶子总数
int leafcount(Binnode *t,int n)
{
if(t)
{
if(t->lchild==NULL&&t->rchild==NULL) n++;
                    n=leafcount(t->lchild,n);


    n=leafcount(t->rchild,n);
                    
}
return n;  
}
void main()
{
int n=0;
Binnode *t;
printf("input data:");
t=Creattree();
printf("\n Inorder:");
Inorder(t);
printf("\n the leaf's number:");
n=leafcount(t,0);
printf("%d",n);
printf("\n the Bintree's high:%d",high(t)); 
system("pause");
}


[解决办法]
引用:
部分源代码:
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node * lchild;
struct node * rchild;
}node, *Tree;

void CreateTree( Tree tree )
{
tree = ( Tree )mall……

此tree非彼tree,创建函数中的tree只是主函数中tree的一份复制,如果想完成初始化,要把主函数中的tree的地址传递过去,此时已变成指针的指针!

热点排行
Bad Request.