[C] 二叉树 c数据结构 →→→→→进入此内容的聊天室

来自 , 2020-03-28, 写在 C, 查看 158 次.
URL http://www.code666.cn/view/a088ea20
  1. /*二叉树
  2. */
  3.  
  4. typedef struct bitnode
  5. {
  6.         char data;
  7.         struct bitnode *lchild, *rchild;
  8. } bitnode, *bitree;
  9. void createbitree ( t,n )
  10. bitnode ** t;
  11. int *n;
  12. {
  13.         char x;
  14.         bitnode *q;
  15.         *n=*n+1;
  16.         printf ( "\n Input %d DATA:",*n );
  17.         x=getchar();
  18.         if ( x!='\n' ) getchar();
  19.         if ( x=='\n' )
  20.                 return;
  21.         q= ( bitnode* ) malloc ( sizeof ( bitnode ) );
  22.         q->data=x;
  23.         q->lchild=NULL;
  24.         q->rchild=NULL;
  25.         *t=q;
  26.         printf ( " This Address is: %o, Data is: %c,\n Left Pointer is: %o, Right Pointer is: %o",q,q->data,q->lchild,q->rchild );
  27.         createbitree ( &q->lchild,n );
  28.         createbitree ( &q->rchild,n );
  29.         return;
  30. }
  31.  
  32. void visit ( e )
  33. bitnode *e;
  34. {
  35.         printf ( " Address: %o, Data: %c, Left Pointer: %o, Right Pointer: %o\n",e,e->data,e->lchild,e->rchild );
  36. }
  37.  
  38. void preordertraverse ( t )
  39. bitnode *t;
  40. {
  41.         if ( t )
  42.         {
  43.                 visit ( t );
  44.                 preordertraverse ( t->lchild );
  45.                 preordertraverse ( t->rchild );
  46.                 return ;
  47.         }
  48.         else
  49.                 return ;
  50. }
  51. void countleaf ( t,c )
  52. bitnode *t;
  53. int *c;
  54. {
  55.         if ( t!=NULL )
  56.         {
  57.                 if ( t->lchild==NULL && t->rchild==NULL )
  58.                 {
  59.                         *c=*c+1;
  60.                 }
  61.                 countleaf ( t->lchild,c );
  62.                 countleaf ( t->rchild,c );
  63.         }
  64.         return;
  65. }
  66. int treehigh ( t )
  67. bitnode *t;
  68. {
  69.         int lh,rh,h;
  70.         if ( t==NULL )
  71.                 h=0;
  72.         else
  73.         {
  74.                 lh=treehigh ( t->lchild );
  75.                 rh=treehigh ( t->rchild );
  76.                 h= ( lh>rh ? lh:rh ) +1;
  77.         }
  78.         return h;
  79. }
  80.  
  81. main()
  82. {
  83.         bitnode *t;
  84.         int count=0;
  85.         int n=0;
  86.         printf ( "\n Please input TREE Data:\n" );
  87.         createbitree ( &t,&n );
  88.         printf ( "\n This is TREE struct: \n" );
  89.         preordertraverse ( t );
  90.         countleaf ( t,&count );
  91.         printf ( "\n This TREE has %d leaves ",count );
  92.         printf ( " , High of The TREE is: %d\n",treehigh ( t ) );
  93. }
  94.  

回复 "二叉树 c数据结构"

这儿你可以回复上面这条便签

captcha