如何在数据结构中,以二叉链表为存储结构,建立一棵二叉树,输出其先序,中序,后序遍历序列,统计其叶子

如题所述

下面我写的代码:

/* Note:Your choice is C IDE */

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

struct lbtree

{

 char data;

 struct lbtree *lchild,*rchild;

};

struct lbtree *createbtree();

void preorder(struct lbtree *root);

void inorder(struct lbtree *root);

void postorder(struct lbtree *root);

treedepth(struct lbtree *root);

numberofleaf(struct lbtree *root);

main()

{

  struct lbtree *root;

  int i;

  printf("create a tree\n");

  printf("please input nodes of tree\n");

  root=NULL;

  root=createbtree();

  if(root==NULL)printf("This is an empty tree!\n");

  else

  {

   printf("\n");

   printf("1.The preorder traverse \n");

   printf("2.The inorder traverse \n");

   printf("3.The postorder traverse \n");

   printf(" Please choose a kind of order\n");

   scanf("%d",&i);

     switch(i)

     {  

    case 1:preorder(root);break;

    case 2:inorder(root);break;

    case 3:postorder(root);break;

    default:printf("error!\n");

      }

   }

   printf("\n The depth of the btree is %d",treedepth(root));

   printf("\n The leafnumber of the btree is %d",numberofleaf(root));

  }

  

 struct lbtree *createbtree()

 {

  char ch,t;

  struct lbtree *root;

  printf("please input the data,'$' is the end.\n");

    scanf("%c",&ch);

    scanf("%c",&t);

  if(ch=='$')  

    return(NULL);

  else

  {

   root=(struct lbtree *)malloc(sizeof(struct lbtree));

   root->data=ch;

   root->lchild=createbtree();

   root->rchild=createbtree();

   return(root);

  }

  printf("ok!");

 }

 

 void preorder(struct lbtree *root)

 {

  if(root)

  {   

    printf(" %c",root->data);

    preorder(root->lchild);

    preorder(root->rchild);

   }

 }

 void inorder(struct lbtree *root)

 {

  if(root){inorder(root->lchild);

        printf(" %c",root->data);

        inorder(root->rchild);

          }

 }

 void postorder(struct lbtree *root)

 {

  if(root)

  {

   postorder(root->lchild);

   postorder(root->rchild);

   printf(" %c",root->data);

  }

 }

 

 treedepth(struct lbtree *root)

 {

  int depth,dl,dr;

  if(!root)depth=0;

  else

  {

   dl=treedepth(root->lchild);

   dr=treedepth(root->rchild);

   if(dl>dr)depth=dl+1;

   else depth=dr+1;

  }

  return(depth);

 }

 

 numberofleaf(struct lbtree *root)

 {

  int num=0,num1,num2;

  if(root)

    {

     if((root->lchild==NULL)&&(root->rchild==NULL))num=1;

       else

         { num1=numberofleaf(root->lchild);

           num2=numberofleaf(root->rchild);

           num=num1+num2;

         }

    }

  return(num);

  }

运行结果如图:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-28
#include "stdio.h"
#include "malloc.h"
#define ELEMTYPE char
typedef struct BiTNode
{ ELEMTYPE data;
struct BiTNode *lchild,*rchild;
} BiTNode;
BiTNode *bulid() /*建树*/
{ BiTNode *q;
BiTNode *s[20];
int i,j;
char x;
printf("请按顺序输入二叉树的结点以输入0和*号结束\n");
printf("请输入你要输入的为第几个结点i=\n");
scanf("%d",&i);
printf("请输入你要输入该结点的数为x=");
getchar();
scanf("%c",&x);
while(i!=0&&x!='*')
{q=(BiTNode*)malloc(sizeof(BiTNode));
q->data=x;
q->rchild=NULL;
q->lchild=NULL;
s[i]=q;

if(i!=1)
{ j=i/2;
if(i%2==0)
s[j]->lchild=q;
else
s[j]->rchild=q;
}

printf("请输入你要输入的为第几个结点x=\n");
scanf("%d",&i);
printf("请输入你要输入该结点的数x=");
getchar();
scanf("%c",&x);
}
return s[1];
}
void preoder(BiTNode *bt) /*先序遍历*/
{ if(bt!=NULL)
{
printf("%c\n",(bt->data));

preoder(bt->lchild);

preoder(bt->rchild);
}
}
void InOrder(BiTNode *bt) /*中序遍历*/
{ if(bt!=NULL)
{ InOrder(bt->lchild);
printf("%c\n",bt->data);
InOrder(bt->rchild);
}
}
void postOrder(BiTNode *bt) /*后序遍历*/
{ if(bt!=NULL)
{ postOrder(bt->lchild);
postOrder(bt->rchild);
printf("%c\n",bt->data);
}
}

main()
{ int a;
BiTNode *bt;
bt=bulid();
k1: printf("需要先序遍历输出请输入1,中序遍历请输入2,后序遍历请输入3,结束输入0:");
scanf("%d",&a);
switch(a)
{
case(1): preoder(bt); goto k1;
case(2): InOrder(bt); goto k1;
case(3): postOrder(bt); goto k1;
case(0): break;
}
}

3本回答被提问者采纳
第2个回答  2011-04-29
随便一本数据结构的入门书上都有