|
|
#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(); } |
程序代码: 题目是建立一个二叉树:[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();
}
这是我写的代码,但是不知道那里出错了,我找了好久,还是没有找到。望各位高手指点一下。谢谢了....
#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();
}
