Files
Data-Structure/Dev-C++/ExerciseBook/06.55/06.55.cpp
2020-01-15 02:40:28 +08:00

65 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <stdio.h>
#include <stdlib.h> // 提供malloc、realloc、free、exit原型
#include "Status.h" //**▲01 绪论**//
#include "BiTree.h" //**▲06 树和二叉树**//
/*
* 计算二叉树中每个结点的子孙数目
*/
int Algo_6_55(BiTree T);
// 先序输出二叉树及各结点其子孙数目
void PreOrderPrint(BiTree T);
int main(int argc, char* argv[]) {
BiTree T;
printf("创建二叉树先序序列T...\n");
InitBiTree(&T);
CreateBiTree(&T, "TestData.txt");
PrintTree(T);
printf("\n");
printf("先序输出二叉树结点值及其相应的子孙数目:\n");
Algo_6_55(T);
PreOrderPrint(T);
return 0;
}
/*
* 计算二叉树中每个结点的子孙数目
*/
int Algo_6_55(BiTree T) {
int l, r;
if(T == NULL) {
return 0;
} else {
T->DescNum = 0;
if(T->lchild != NULL) {
l = Algo_6_55(T->lchild);
T->DescNum += l + 1;
}
if(T->rchild != NULL) {
r = Algo_6_55(T->rchild);
T->DescNum += r + 1;
}
}
return T->DescNum;
}
// 先序输出二叉树及各结点其子孙数目
void PreOrderPrint(BiTree T) {
if(T != NULL) {
printf("结点 %c 的子孙数目 %d\n", T->data, T->DescNum);
PreOrderPrint(T->lchild);
PreOrderPrint(T->rchild);
}
}