//二叉树的二叉链表存储表示
//杨鑫
#include <stdio.h>
#include <stdlib.h>
#define max(a, b) a > b ? a : b //自定义max()函数
typedef char TELemType;
//定义结二叉树的构体
typedef struct BTree
{
TELemType data;
struct BTree *lChild;
struct BTree *rChild;
}BinTree;
//二叉树的创建
BinTree* CreateTree(BinTree *T)
{
char temp;
scanf("%c", &temp);
if(temp == '0')
return 0;
T = (BinTree *)malloc(sizeof(BinTree));
T->data = temp;
T->lChild = CreateTree(T->lChild); //递归创建左子树
T->rChild = CreateTree(T->rChild); //递归创建右子树
return T;
}
//计算叶子结点的数量
int sumleft(BinTree *T)
{
int sum = 0, leftNum, rightNum;
if(T)
{
if((!T->lChild) && (!T->rChild))
{
sum++;
}
leftNum = sumleft(T->lChild);
sum += leftNum;
rightNum = sumleft(T->lChild);
sum += rightNum;
}
return sum;
}
//先序遍历二叉树
void PreOrderTraverse(B
数据结构之---C语言实现二叉树的二叉链表存储表示
最新推荐文章于 2023-07-20 15:58:35 发布