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

一个关于二叉树的有关问题(调试没错,运行出错,关于指针指错)

2012-03-09 
一个关于二叉树的问题(调试没错,运行出错,关于指针指错)#includestdio.h#includestdlib.h#includecon

一个关于二叉树的问题(调试没错,运行出错,关于指针指错)
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
  char data;
  struct node *lchild,*rchild;
}BiTree;
int Num=0;
void CreatBiTree(BiTree *root)
{
  char ch;
  fflush(stdin);
  printf("Please input a char to be a tree node:(press enter exit a child node)\n");
  scanf("%c",&ch);
  fflush(stdin);
  if(ch!='\n')
  {
  root=(BiTree *)malloc(sizeof(BiTree)); 
  if(root==NULL) {printf("Over Flow\n";return;)}
  root->data=ch;
  printf("%c",root->data);
  printf("you already input %d char.\n",++Num);
  CreatBiTree(root->lchild);
  CreatBiTree(root->rchild);
  }
}
void FrontPrintBiTree(BiTree *root)
{
  if(root!=NULL)
  {
  printf("%4c",root->data);

  if(root->lchild)
  FrontPrintBiTree(root->lchild);
  if(root->rchild)
  FrontPrintBiTree(root->rchild);
  }

}
void MildPrintBiTree(BiTree *root)
{
  if(root!=NULL)
  {
  if(root->lchild)
  MildPrintBiTree(root->lchild);
  printf("%4c",root->data);
  if(root->rchild)
  MildPrintBiTree(root->rchild);
  }

}
void RearPrintBiTree(BiTree *root)
{
  if(root!=NULL)
  {
  if(root->lchild)
  RearPrintBiTree(root->lchild);
  if(root->rchild)
  RearPrintBiTree(root->rchild);
  printf("%4c",root->data);
  }

}
int main()
{
  BiTree root;
  int a;
  CreatBiTree(&root);
  do
  {
  printf("Please choose a type what print the bitree:(1,front;2,mild;3,rear)\n");
  scanf("%d",&a);
  fflush(stdin);
  }while(a!=1&&a!=2&&a!=3);
  printf("%4c",root.data);
  switch(a)
  {
  case 1:FrontPrintBiTree(&root);break;
  case 2:MildPrintBiTree(&root);break;
  case 3:RearPrintBiTree(&root);break;
  default :main();break;
  }
  getch();
  return 0;
}
在win-tc和vc中调试了好几天就是不能调出错误!希望老鸟们能帮一下!

[解决办法]

C/C++ code
/*1.CreatBiTree(BiTree*root)函数返回了根节点的地址,以用于以后的遍历。2.输入#是为了使lchild,rchild指向NULL。3.可以输入ab##cd##e等检查遍历的输出,#代表的是NULL。4.遍历中有两个if判断是多余的。 */#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<malloc.h>typedef struct node{  char data;  struct node *lchild,*rchild;}BiTree;int Num=0;BiTree *CreatBiTree(BiTree *root){  char ch;  fflush(stdin);  printf("Please input a char to be a tree node:(press # to return)\n");  scanf("%c",&ch);  fflush(stdin);  if(ch=='#')  {      root=NULL;  }  else  {      root=(BiTree *)malloc(sizeof(BiTree));        root->data=ch;      printf("%c\n",root->data);      printf("you already input %d char.\n",++Num);      root->lchild=CreatBiTree(root->lchild);      root->rchild=CreatBiTree(root->rchild);  }  return root;}void FrontPrintBiTree(BiTree *root){  if(root!=NULL)  {  printf("%4c",root->data);  //if(root->lchild)  FrontPrintBiTree(root->lchild);  //if(root->rchild)  FrontPrintBiTree(root->rchild);  }}void MildPrintBiTree(BiTree *root){  if(root!=NULL)  {  //if(root->lchild)  MildPrintBiTree(root->lchild);  printf("%4c",root->data);  //if(root->rchild)  MildPrintBiTree(root->rchild);  }}void RearPrintBiTree(BiTree *root){  if(root!=NULL)  {  //if(root->lchild)  RearPrintBiTree(root->lchild);  //if(root->rchild)  RearPrintBiTree(root->rchild);  printf("%4c",root->data);  }}int main(){  BiTree *root=NULL;  int a;  root=CreatBiTree(root);  do  {  printf("Please choose a type what print the bitree:(1,front;2,mild;3,rear)\n");  scanf("%d",&a);  //fflush(stdin);  }while(a!=1&&a!=2&&a!=3);  //printf("%4c",root.data);  switch(a)  {        case 1:FrontPrintBiTree(root);break;  case 2:MildPrintBiTree(root);break;  case 3:RearPrintBiTree(root);break;  default :main();break;  }  getch();  return 0;} 


[解决办法]
[code=C/C++][/code]#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BiTree;
int Num=0;
void CreatBiTree(BiTree *root)
{
char ch;
fflush(stdin);
printf("Please input a char to be a tree node:(press enter exit a child node)\n");
scanf("%c",&ch);
fflush(stdin);
if(ch!='\n')
{
root=(BiTree *)malloc(sizeof(BiTree));
if(root==NULL) {printf("Over Flow\n");return;}
root->data=ch;
printf("%c",root->data);
printf("you already input %d char.\n",++Num);
CreatBiTree(root->lchild);
CreatBiTree(root->rchild);
}
}
void FrontPrintBiTree(BiTree *root)
{
if(root!=NULL)
{
printf("%4c",root->data);

if(root->lchild)
FrontPrintBiTree(root->lchild);
if(root->rchild)
FrontPrintBiTree(root->rchild);
}

}
void MildPrintBiTree(BiTree *root)
{
if(root!=NULL)
{
if(root->lchild)
MildPrintBiTree(root->lchild);
printf("%4c",root->data);
if(root->rchild)
MildPrintBiTree(root->rchild);
}

}
void RearPrintBiTree(BiTree *root)
{
if(root!=NULL)
{
if(root->lchild)
RearPrintBiTree(root->lchild);
if(root->rchild)
RearPrintBiTree(root->rchild);
printf("%4c",root->data);
}

}
int main()
{
BiTree root;
int a;
CreatBiTree(&root);
do
{
printf("Please choose a type what print the bitree:(1,front;2,mild;3,rear)\n");
scanf("%d",&a);
fflush(stdin);
}while(a!=1&&a!=2&&a!=3);
printf("%4c",root.data);
switch(a)
{
case 1:FrontPrintBiTree(&root);break;
case 2:MildPrintBiTree(&root);break;
case 3:RearPrintBiTree(&root);break;
default :main();break;
}
getch();
return 0;
}

[解决办法]
C Code:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct _TNODE
{
char data;
struct _TNODE *Lchild;
struct _TNODE *Rchild;
}BTree;

#defineSIZE_OF_BTNODEsizeof(struct _TNODE)

void Create_BTree(BTree **BT);
void Print_BTree(BTree *BT, int layer);
int Hight_Of_BTree(BTree *BT);
void Visit_BTree(BTree *BT);
void Pre_Ord_Tranverse(BTree *BT);
void In_Ord_Tranverse(BTree *BT);
void Last_Ord_Tranverse(BTree *BT);
void Count_Leaf(BTree *BT,int *Count);

int main()
{
BTree *bt = NULL;
int layer = 1;
int Hight = 0;
int count = 0;

printf("Create BTree ('.' == NULL):\n\n");
Create_BTree(&bt);
printf("\nThe BTree struct is :\n");
Print_BTree(bt, layer);

Hight = Hight_Of_BTree(bt);
printf("\nThe hight of the tree is : %d\n", Hight);

printf("\nThe pre order tranverse is :\n");
Pre_Ord_Tranverse(bt);

printf("\nThe In order tranverse is :\n");
In_Ord_Tranverse(bt);

printf("\nThe Last order tranverse is :\n");
Last_Ord_Tranverse(bt);

Count_Leaf(bt, &count);
printf("\nThe BTree has %d leaves!!\n", count);

puts("");
return 0;
}

void Create_BTree(BTree **BT)
{
char ch;

ch = getchar();
if(ch == '.')
{
(*BT) = NULL;
}
else
{
(*BT ) = (BTree *) malloc(SIZE_OF_BTNODE);


if((*BT) == NULL)
{
printf("Memory assign failure!!\n");
exit(1);
}
(*BT)->data = ch;
Create_BTree(&((*BT)->Lchild));
Create_BTree(&((*BT)->Rchild));
}
}

void Print_BTree(BTree *BT, int layer)
{
int i;

if( BT == NULL)
{
return;
}
else
{
Print_BTree(BT->Rchild, layer + 1);
for(i = 0; i < layer; ++i)
{
printf(" ");
}
printf("%c\n", BT->data );
Print_BTree(BT->Lchild, layer + 1);
}
}

int Hight_Of_BTree(BTree *BT)
{
int Lh, Rh, H;

if(BT == NULL)
{
H = 0;
}
else
{
Lh = Hight_Of_BTree(BT->Lchild);
Rh = Hight_Of_BTree(BT->Rchild);
H = ((Lh > Rh)? Lh : Rh ) + 1;
}
return H;
}

void Visit_BTree(BTree *BT)
{
if(BT != NULL)
{
printf("%c ->", BT->data);
}
else
{
return;
}
}

void Pre_Ord_Tranverse(BTree *BT)
{
if(BT == NULL)
{
return;
}
else
{
Visit_BTree(BT);
if(BT->Lchild != NULL)
{
Pre_Ord_Tranverse(BT->Lchild);
}
if(BT->Rchild != NULL)
{
Pre_Ord_Tranverse(BT->Rchild);
}
}
}

void In_Ord_Tranverse(BTree *BT)
{
if(BT == NULL)
{
return;
}
else
{
if(BT->Lchild != NULL)
{
In_Ord_Tranverse(BT->Lchild);
}
Visit_BTree(BT);
if(BT->Rchild != NULL)
{
In_Ord_Tranverse(BT->Rchild);
}
}
}

void Last_Ord_Tranverse(BTree *BT)
{
if(BT == NULL)
{
return;
}
else
{
if(BT->Lchild != NULL)
{
Last_Ord_Tranverse(BT->Lchild);
}
if(BT->Rchild != NULL)
{
Last_Ord_Tranverse(BT->Rchild);
}
Visit_BTree(BT);
}
}

void Count_Leaf(BTree *BT,int *Count)
{
if(BT == NULL)
{
return;
}
else
{
if(BT->Rchild == NULL && BT->Lchild == NULL)
{
*Count += 1;
}
Count_Leaf(BT->Lchild, Count);
Count_Leaf(BT->Rchild, Count);
}
}
/*
Create BTree ('.' == NULL):

abc..d..e..

The BTree struct is :
e
a
d
b
c

The hight of the tree is : 3

The pre order tranverse is :
a ->b ->c ->d ->e ->
The In order tranverse is :
c ->b ->d ->a ->e ->
The Last order tranverse is :
c ->d ->b ->e ->a ->
The BTree has 3 leaves!!

Press any key to continue
*/

热点排行