标题:树的建立
只看楼主
yan876768390
Rank: 1
等 级:新手上路
帖 子:8
专家分:2
注 册:2010-11-7
结帖率:50%
已结贴  问题点数:20 回复次数:3 
树的建立
程序代码:
 题目是建立一个二叉树:[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();
    
}


未命名.jpg (16.58 KB)
搜索更多相关主题的帖子: color 
2010-11-08 09:37
瓦药墙
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:218
专家分:556
注 册:2009-9-16
得分:10 
#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();
   
}
2010-11-08 12:00
瓦药墙
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:218
专家分:556
注 册:2009-9-16
得分:10 
这样 写法也太那个神马了吧
2010-11-08 12:07
yan876768390
Rank: 1
等 级:新手上路
帖 子:8
专家分:2
注 册:2010-11-7
得分:0 
谢谢了,我改好了程序了。我是新手,望以后多多帮助
2010-11-08 15:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-325271-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.171712 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved