当前位置:编程学习 > C/C++ >>

c++我找上打的错了77个

#include<stdio.h>
#define TREEMAX 100
typedef struct BT//定义叉数据结构
{
 char data;
 BT *lchild;
 BT *rchild;
}BT;
BT *CreateTree();
void ShowTree(BT *T);
void Preorredr(BT *T);
void Postorder(BT *T);
void Levelorde(BT *T);
void Inorder(BT *T);
void Leafnum(BT *T);
void Nodenum(BT *T);
int TreeDepth(BT *t);
int count=0;//定义计算结点个数的变量

void main()
{
 BT *T=NULL;
 char ch1,ch2,a;
 ch1='y';
 while(ch1=='y'||ch1=='y');
 {
  printf("\n");
  printf("\n\t\t             二叉树子系统");
  pritf("\n\t\t**************************************");
  pritf("\n\t\t*      1----------建二叉树        *");
  pritf("\n\t\t*      2----------凹入显示        *");
  pritf("\n\t\t*      3----------先序遍历       *");
  pritf("\n\t\t*      4----------中序遍历        *");
  pritf("\n\t\t*      5----------后续遍历        *");
     pritf("\n\t\t*      6----------层次遍历       *")
  pritf("\n\t\t*      7----------求叶子树        *");
  pritf("\n\t\t*      8----------求节点数       *");
  pritf("\n\t\t*      9----------求树深度        *");
  pritf("\n\t\t*      0----------返    回       *");
  pritf("\n\t\t***************************************");
  pritf("\n\t\t请选择单号(0--9):");
  scanf("%c",&ch2);
  getchar();
  printf("\n");
  switch(ch2)
  {
  case'1':
                pritf("\n\t\t请按先序序列输入:\n");
          pritf("\n\t\t说明:输入节点('0'表示后继结点为空)后按回车键。\n");
          pritf("\n\t\t请输入结点:\n");
    T=CreateTree();
    printf("\n\t\t二叉树成功建立!\n");
    break;

  case'2':
   ShowTree(T);
   break;

  case'3':
   printf("\n\t\t该二叉树的先序遍历序列为:");
   preorder(T);
   break;

        case'4':
   printf("\n\t\t该二叉树的中序遍历序列为:");
   Inorder(T);
   break;

        case'5':
   printf("\n\t\t该二叉树的后序遍历序列为:");
   Postorde(T);
   break;

        case'6':
   printf("\n\t\t该二叉树的层次遍历序列为:");
   Levelorder(T);
   break;

        case'7':
   count=0;Leafnum(T)
   printf("\n\t\t该二叉树有%d个叶子。\n",count);
   break;

  case'8':
   count=0;Nodenum(T);
   printf("\n\t\t该二叉树有%d个结点。\n",count);
   break;
  case'9':
   printf("\n\t\t该二叉树的深度是:%d",TreeDepth(T));
   break;
  case'0':
   ch1='n';break;
  default:
   printf("\n\t\t***请注意:输入有误!***");
  }
  if(ch2!='0')
  {
   printf("\n\n\t\t按回车继续,按任意键返回主菜单!\n");
   a=getcher();
   {
    getcher();ch1='n';
   }
  }
 }
}
BT*CreateTreee()//建立二叉树
{
 BT *t;
 char x;
 scanf("%c",&x);
 getchar();
 if(x=='0')
  t=NULL;
else
{
 t=new BT;
 t->data=x;
 printf("\n\t\t请输入%c结点的左子节点:",t->data);
 t->lchild=CreateTree();
 printf("\n\t\t请输入%c结点的右子节点:",t->data);
 t->rchild=CreateTree();
}
return t;
}
void Preorder(BT *T)//先序编历
{
 if(T)
 {
  printf("%3c",T->data);
  preoder(T->lchild);
  preoder(T->rchild);
 }
}
void Inorder(BT *T)//中序编历
{
 if(T)
 {
  Inorder(T->lchild);
  printf("%3c",T->data);
  Inorder(T->rchild);
 }
}

void Postorder(BT *T)//后序编列
{
 if(T)
 {
  Postorder(T->lchild);
  Postorder(T->rchild);
  printf("%3c",T->data);
 }
}

void Levelorder(BT *T)//层次遍历
{
 int i,j;
 BT *q[100],*p;
 p=T;
 if(p! =NULL)
 {
  i=1;q[i]=p;j=2;
 }
 while(i! =j)
 {
  p=q[i];printf("%3c",p->data);
  if(p->lchild! =NULL)
  {
   q[j]=p->lchild;j++;
  }
  if(p->rchild;j++;
 }
 i++;
}
}

void Leafnum(BT *T)//求叶子数
{
 if(T)
 {
  if(T->lchild==NULL&&T->rchild==NULL)
   count++;
  Leafnum(T->lchild);
  Leafnum(T->rchild);
 }
}

void Nodenum(BT *T)//求结点数
{
 if(T)
 {
  count++;
  Nodenum(T->lchild);
  Nodenum(T->rchild);
 }
}

int TreeDepth(BT *t)
{
 int ldep,rdep;
 if(T==NULL)
  return0;
 else
 {
  ldep=TreeDepth(T->lchild);
  rdep=TreeDepth(T->lchild);
  if(ldep>rdep)
   return ldep+1;
  else
   return rdep+1;
 }
}

void ShowTree(BT *T)//凹入法显示二叉树
{
 BT *stack[TREEMAX],*p;
 int level[TREEMAX][2],top,n,i,width=4;
 if(T! =NULL)
 {
  printf("\n\t\t凹入表示法;\n\t\t");
  top=1;
  stack[top]=T;
  level[top][0]=width;
  while(top>0)
  {
   p=stack[top];
   n=level[top][0]=width;
   while(top>0)
   {
    p=stack[top];
    n=level[top][0];
    for(i=1;i<=n;i++)
     printf("");
    printf("%c",p->data);
    for(i=n+1;i<30;i+=2)
     printf("@");
    printf("\n\t\t");
    top--;
    if(p->rchild! =NULL)
    {
     top++;
     stack[top]=p->rchild;
     level[top][0]=n+width;
     level[top][1]=2;
    }
    if(p->lchild! =NULL)
    {
     top++;
     stack[top]=p->lchild;
     level[top][0]=n+width;
     level[top][1]=1;
    }
   }
  }
 }

 


还会加分的哦

追问:来自手机问问哪里呀
答案:你抄错了

#include<stdio.h>
#define TREEMAX 100
typedef struct BT//定义叉数据结构
{
 char data;
 BT *lchild;
 BT *rchild;
}BT;
BT *CreateTree();
void ShowTree(BT *T);
void Preorder(BT *T);
void Postorder(BT *T);
void Levelorder(BT *T);
void Inorder(BT *T);
void Leafnum(BT *T);
void Nodenum(BT *T);
int TreeDepth(BT *t);
int count=0;//定义计算结点个数的变量
void main()
{
 BT *T=NULL;
 char ch1,ch2,a;
 ch1='y';
 while(ch1=='y'||ch1=='y');
 {
  printf("\n");
  printf("\n\t\t             二叉树子系统");
  printf("\n\t\t**************************************");
  printf("\n\t\t*      1----------建二叉树        *");
  printf("\n\t\t*      2----------凹入显示        *");
  printf("\n\t\t*      3----------先序遍历       *");
  printf("\n\t\t*      4----------中序遍历        *");
  printf("\n\t\t*      5----------后续遍历        *");
     printf("\n\t\t*      6----------层次遍历       *");
  printf("\n\t\t*      7----------求叶子树        *");
  printf("\n\t\t*      8----------求节点数       *");
  printf("\n\t\t*      9----------求树深度        *");
  printf("\n\t\t*      0----------返    回       *");
  printf("\n\t\t***************************************");
  printf("\n\t\t请选择单号(0--9):");
  scanf("%c",&ch2);
  getchar();
  printf("\n");
  switch(ch2)
  {
  case'1':
                printf("\n\t\t请按先序序列输入:\n");
          printf("\n\t\t说明:输入节点('0'表示后继结点为空)后按回车键。\n");
          printf("\n\t\t请输入结点:\n");
    T=CreateTree();
    printf("\n\t\t二叉树成功建立!\n");
    break;
  case'2':
   ShowTree(T);
   break;
  case'3':
   printf("\n\t\t该二叉树的先序遍历序列为:");
   Preorder(T);
   break;
        case'4':
   printf("\n\t\t该二叉树的中序遍历序列为:");
   Inorder(T);
   break;
        case'5':
   printf("\n\t\t该二叉树的后序遍历序列为:");
   Postorder(T);
   break;
        case'6':
   printf("\n\t\t该二叉树的层次遍历序列为:");
   Levelorder(T);
   break;
        case'7':
   count=0;Leafnum(T);
   printf("\n\t\t该二叉树有%d个叶子。\n",count);
   break;
  case'8':
   count=0;Nodenum(T);
   printf("\n\t\t该二叉树有%d个结点。\n",count);
   break;
  case'9':
   printf("\n\t\t该二叉树的深度是:%d",TreeDepth(T));
   break;
  case'0':
   ch1='n';break;
  default:
   printf("\n\t\t***请注意:输入有误!***");
  }
  if(ch2!='0')
  {
   printf("\n\n\t\t按回车继续,按任意键返回主菜单!\n");
   a=getchar();
   {
    getchar();ch1='n';
   }
  }
 }
}
BT*CreateTree()//建立二叉树
{
 BT *t;
 char x;
 scanf("%c",&x);
 getchar();
 if(x=='0')
  t=NULL;
else
{
 t=new BT;
 t->data=x;
 printf("\n\t\t请输入%c结点的左子节点:",t->data);
 t->lchild=CreateTree();
 printf("\n\t\t请输入%c结点的右子节点:",t->data);
 t->rchild=CreateTree();
}
return t;
}
void Preorder(BT *T)//先序编历
{
 if(T)
 {
  printf("%3c",T->data);
  Preorder(T->lchild);
  Preorder(T->rchild);
 }
}
void Inorder(BT *T)//中序编历
{
 if(T)
 {
  Inorder(T->lchild);
  printf("%3c",T->data);
  Inorder(T->rchild);
 }
}
void Postorder(BT *T)//后序编列
{
 if(T)
 {
  Postorder(T->lchild);
  Postorder(T->rchild);
  printf("%3c",T->data);
 }
}
void Levelorder(BT *T)//层次遍历
{
 int i,j;
 BT *q[100],*p;
 p=T;
 if(p!=NULL)
 {
  i=1;q[i]=p;j=2;
 }
 while(i!=j)
 {
  p=q[i];printf("%3c",p->data);
  if(p->lchild!=NULL)
  {
   q[j]=p->lchild;j++;
  }
  if(p->rchild)j++;
 
 i++;
}
}
void Leafnum(BT *T)//求叶子数
{
 if(T)
 {
  if(T->lchild==NULL&&T->rchild==NULL)
   count++;
  Leafnum(T->lchild);
  Leafnum(T->rchild);
 }
}
void Nodenum(BT *T)//求结点数
{
 if(T)
 {
  count++;
  Nodenum(T->lchild);
  Nodenum(T->rchild);
 }
}
int TreeDepth(BT *t)
{
 int ldep,rdep;
 if(t==NULL)
  return 0;
 else
 {
  ldep=TreeDepth(t->lchild);
  rdep=TreeDepth(t->lchild);
  if(ldep>rdep)
   return ldep+1;
  else
   return rdep+1;
 }
}
void ShowTree(BT *T)//凹入法显示二叉树
{
 BT *stack[TREEMAX],*p;
 int level[TREEMAX][2],top,n,i,width=4;
 if(T!=NULL)
 {
  printf("\n\t\t凹入表示法;\n\t\t");
  top=1;
  stack[top]=T;
  level[top][0]=width;
  while(top>0)
  {
   p=stack[top];
   n=level[top][0]=width;
   while(top>0)
   {
    p=stack[top];
    n=level[top][0];
    for(i=1;i<=n;i++)
     printf("");
    printf("%c",p->data);
    for(i=n+1;i<30;i+=2)
     printf("@");
    printf("\n\t\t");
    top--;
    if(p->rchild!=NULL)
    {
     top++;
     stack[top]=p->rchild;
     level[top][0]=n+width;
     level[top][1]=2;
    }
    if(p->lchild!=NULL)
    {
     top++;
     stack[top]=p->lchild;
     level[top][0]=n+width;
     level[top][1]=1;
    }
   }
  }
 }
}

上一个:谁能给我些C++编程的实例?
下一个:介绍几个能学C++入门的网站

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,