注册 登录
编程论坛 数据结构与算法

树的建立

yan876768390 发布于 2010-11-08 09:37, 518 次点击
程序代码:
题目是建立一个二叉树:[local]1[/local]




这是我写的代码,但是不知道那里出错了,我找了好久,还是没有找到。望各位高手指点一下。谢谢了....

#include <iostream.h>

 struct node

 {
   int data;
   node *lchild,*rchild;

 };


 void CreateBiTree()

{        node *root;

     for(int i=0;i<8;i++)
     {
    switch(i)
     {
     case 0:
         root=new node ;
         root->data=1;
         root->lchild=NULL;
         root->rchild=NULL;
     case 1:
        root->lchild=new node ;
        root->lchild->data=2;
        root->lchild->lchild=NULL;
        root->lchild->rchild=NULL;
     case 2:
         root->rchild=new node;
         root->rchild->data=3;
         root->rchild->lchild=NULL;
         root->rchild->rchild=NULL;

     case 3:
         root->lchild->lchild=new node;
         root->lchild->lchild->data=4;
         root->lchild->lchild->lchild=NULL;
         root->lchild->lchild->rchild=NULL;
     case 4:
         root->lchild->rchild=new node;
         root->lchild->rchild->data=5;
         root->lchild->rchild->lchild=NULL;
         root->lchild->rchild->rchild=NULL;
     case 5:
         root->rchild->lchild->data=6;
         root->rchild->lchild->lchild=NULL;
         root->rchild->lchild->rchild=NULL;
     case 6:
         root->lchild->rchild->lchild=new node;
         root->lchild->rchild->lchild->data=7;
         root->lchild->rchild->lchild->lchild=NULL;
         root->lchild->rchild->lchild->rchild=NULL;
     case 7:
         root->lchild->rchild->rchild=new node;
         root->lchild->rchild->rchild->data=8;
         root->lchild->rchild->rchild->lchild=NULL;
         root->lchild->rchild->rchild->rchild=NULL;
     }

 }

 }

void main()
{  


     CreateBiTree();
   
}
3 回复
#2
瓦药墙2010-11-08 12:00
#include <iostream.h>
struct node
{
   int data;
   struct node *lchild,*rchild;
};

void CreateBiTree()

{        struct node *root;

     for(int i=0;i<8;i++)
     {
    switch(i)
     {
     case 0:
         root=new node ;
         root->data=1;
         root->lchild=NULL;
         root->rchild=NULL;
         break;
     case 1:
        root->lchild=new node ;
        root->lchild->data=2;
        root->lchild->lchild=NULL;
        root->lchild->rchild=NULL;
        break;
     case 2:
         root->rchild=new node;
         root->rchild->data=3;
         root->rchild->lchild=NULL;
         root->rchild->rchild=NULL;
         break;

     case 3:
         root->lchild->lchild=new node;
         root->lchild->lchild->data=4;
         root->lchild->lchild->lchild=NULL;
         root->lchild->lchild->rchild=NULL;
        break;
     case 4:
         root->lchild->rchild=new node;
         root->lchild->rchild->data=5;
         root->lchild->rchild->lchild=NULL;
         root->lchild->rchild->rchild=NULL;
         break;
     case 5:
         root->rchild->lchild=new node;
         root->rchild->lchild->data=6;
         root->rchild->lchild->lchild=NULL;
         root->rchild->lchild->rchild=NULL;
         break;
     case 6:
         root->lchild->rchild->lchild=new node;
         root->lchild->rchild->lchild->data=7;
         root->lchild->rchild->lchild->lchild=NULL;
         root->lchild->rchild->lchild->rchild=NULL;
         break;
     case 7:
         root->lchild->rchild->rchild=new node;
         root->lchild->rchild->rchild->data=8;
         root->lchild->rchild->rchild->lchild=NULL;
         root->lchild->rchild->rchild->rchild=NULL;
         break;
     }
}
}

void main()
{  


     CreateBiTree();
   
}
#3
瓦药墙2010-11-08 12:07
这样 写法也太那个神马了吧
#4
yan8767683902010-11-08 15:59
谢谢了,我改好了程序了。我是新手,望以后多多帮助
1