counbitr.c

来自「《数据结构》教材源程序,可以让你轻松的根据教材学习数据结构」· C语言 代码 · 共 34 行

C
34
字号
#include<stdio.h>
#include<stdlib.h>
typedef char datatype;
typedef struct node            /*二叉树结点定义*/
 {
  datatype data;
  struct node *lchild,*rchild;
  }bintnode;
typedef bintnode *bintree;
void createbintree(bintree *t)       
{/*按照前序遍历的顺序建立一棵给定的二叉树*/
 char ch;
 if ((ch=getchar())==' ')
      *t=NULL;
      else {
	  *t=(bintnode *)malloc(sizeof(bintnode));
	  (*t)->data=ch;
	  createbintree(&(*t)->lchild);
	  createbintree(&(*t)->rchild);
      }
 }
int numofnode(bintree t)  /*统计二叉树t中的结点数*/
 { if (t==NULL)  return 0;
   else return(numofnode(t->lchild)+numofnode(t->rchild)+1);
 }

main()
{ bintree root;
  int num;
  createbintree(&root);
  num=numofnode(root);
  printf("the number of the tree is %d",num);
}
  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?