## 二叉树采用链式存储结构,设计一个递归算法设计一棵给定二叉树的所有结点数

• 行者武松
2019-07-17 22:54:53
// ch6m.cpp : 定义控制台应用程序的入口点。
#include<string.h>
#include<malloc.h>
#include<stdio.h>
#define MAXNUM 100
//#define FALSE 0
typedef struct BiTNode
{
char data;
struct BiTNode * lchild,* rchild;
}BiTNode;

BiTNode * creatNode()
{
int i,j;
char ch;
BiTNode *s[100];
printf("请依次输入结点的序号:\n");
scanf("%d",&i);
printf("请输入该结点需要存放的字符：\n");
getchar();
scanf("%c",&ch);
while(i!=0)
{
BiTNode * p = (BiTNode*)malloc(sizeof(BiTNode));
p->data = ch;
p->lchild = NULL;
p->rchild = NULL;
s[i] = p;
if(i!=1)
{
j = i/2;
if(i%2 == 0)
s[j]->lchild = s[i];
else
s[j]->rchild = s[i];

}

printf("请依次输入第i个结点:\n");
scanf("%d",&i);
printf("请输入该结点需要存放的字符：\n");
getchar();
scanf("%c",&ch);
}
return s[1];

}
void PreorderTraverse(BiTNode *b)
{
if(b!=NULL)
{
printf("%c ",b->data);
printf("\n");
PreorderTraverse(b->lchild);
PreorderTraverse(b->rchild);
}
}

int main(void)
{
int n;
BiTNode * b;
b=creatNode();
while(1)
{
printf("\n"); printf("\n"); printf("\n");
printf("-------------菜单-----------------------------\n");
printf(" 按-----------1-------进入先序递归遍历 ：\n");
printf("----------------------------------------------\n");
printf(":");
scanf("%d",&n);
printf("\n"); printf("\n");
switch(n)
{
case 1: PreorderTraverse(b);break;
}
}
return 0;
}
0 0
• 青衫无名
2019-07-17 22:54:53
int Count(Bitree T)// 根结点指针T
{
int n = 0;
if (T != NULL)
n = 1 + Count(T->leftchild) + Count(T->rightchild);
return n;
}
0 0

+ 订阅