#include <stdio.h>
#include<stdlib.h>
#include<stdlib.h>
int meun() {
int a = 0;
printf("\n**************************************\n");
printf("* 1.建立二叉链表树 *\n");
printf("* 2.显示先序,中序,后序列表 *\n");
printf("* 3.统计叶节点,非叶结点,节点总数 *\n");
printf("* 4.交换二叉树所有节点的左右子树 *\n");
printf("* 5.帮助 *\n");
printf("* 6.退出 *\n");
printf("**************************************\n");
printf("请输入菜单前的序号:");
scanf_s("%d", &a);
return a;
printf("\n**************************************\n");
printf("* 1.建立二叉链表树 *\n");
printf("* 2.显示先序,中序,后序列表 *\n");
printf("* 3.统计叶节点,非叶结点,节点总数 *\n");
printf("* 4.交换二叉树所有节点的左右子树 *\n");
printf("* 5.帮助 *\n");
printf("* 6.退出 *\n");
printf("**************************************\n");
printf("请输入菜单前的序号:");
scanf_s("%d", &a);
return a;
}
#define ElemType char
//节点声明,数据域、左孩子指针、右孩子指针
int SumNode = 0;
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
//先序建立二叉树
BiTree CreateBiTree() {
char ch;
BiTree T;
ch = getchar();
if (ch == '#') {
T = NULL;
}
else {
SumNode++;
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;//返回根节点
}
//先序遍历二叉树
void PreOrderTraverse(BiTree T) {
if (T) {
printf("%c", T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
//节点声明,数据域、左孩子指针、右孩子指针
int SumNode = 0;
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
//先序建立二叉树
BiTree CreateBiTree() {
char ch;
BiTree T;
ch = getchar();
if (ch == '#') {
T = NULL;
}
else {
SumNode++;
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;//返回根节点
}
//先序遍历二叉树
void PreOrderTraverse(BiTree T) {
if (T) {
printf("%c", T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
//中序遍历
void InOrderTraverse(BiTree T) {
if (T) {
PreOrderTraverse(T->lchild);
printf("%c", T->data);
PreOrderTraverse(T->rchild);
}
}
//后序遍历
void PostOrderTraverse(BiTree T) {
if (T) {
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
printf("%c", T->data);
}
}
void InOrderTraverse(BiTree T) {
if (T) {
PreOrderTraverse(T->lchild);
printf("%c", T->data);
PreOrderTraverse(T->rchild);
}
}
//后序遍历
void PostOrderTraverse(BiTree T) {
if (T) {
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
printf("%c", T->data);
}
}
int GetSumNode() { //节点总数
return SumNode;
}
return SumNode;
}
int SumLeaf(BiTree T)
{ //求叶子节点的个数
int sum = 0, m, n;
if (T)
{
if ((!T->lchild) && (!T->rchild))
sum++;
m = SumLeaf(T->lchild);
sum += m;
n = SumLeaf(T->rchild);
sum += n;
}
return sum;
}
{ //求叶子节点的个数
int sum = 0, m, n;
if (T)
{
if ((!T->lchild) && (!T->rchild))
sum++;
m = SumLeaf(T->lchild);
sum += m;
n = SumLeaf(T->rchild);
sum += n;
}
return sum;
}
int SumNoLeaf(BiTree T) //求非叶子节点的个数
{
int sum = GetSumNode() - SumLeaf(T);
return sum;
}
{
int sum = GetSumNode() - SumLeaf(T);
return sum;
}
int Depth(BiTree T) //求二叉树的深度
{
int dep = 0, depl, depr;
if (!T) dep = 0;
else
{
depl = Depth(T->lchild);
depr = Depth(T->rchild);
dep = 1 + (depl > depr ? depl : depr);
}
printf("%d", dep);
return dep;
}
{
int dep = 0, depl, depr;
if (!T) dep = 0;
else
{
depl = Depth(T->lchild);
depr = Depth(T->rchild);
dep = 1 + (depl > depr ? depl : depr);
}
printf("%d", dep);
return dep;
}
void main() {
BiTree T;
T = CreateBiTree();
bool loop = true;
int a;
while (loop) {
a = meun();
switch (a) {
case 1:
printf("请输入:\n");
T = CreateBiTree();//建立
break;
case 2:
printf("先序:\n");
PreOrderTraverse(T);//输出
printf("\n中序:\n");
InOrderTraverse(T);
printf("\n后序:\n");
PostOrderTraverse(T);
break;
case 3:
printf("节点总数:\n");
printf("%d",GetSumNode());
printf("\n叶节点数:\n");
printf("%d",SumLeaf(T));
printf("\n非叶节点数:\n");
printf("%d", SumNoLeaf(T));
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
T = CreateBiTree();
bool loop = true;
int a;
while (loop) {
a = meun();
switch (a) {
case 1:
printf("请输入:\n");
T = CreateBiTree();//建立
break;
case 2:
printf("先序:\n");
PreOrderTraverse(T);//输出
printf("\n中序:\n");
InOrderTraverse(T);
printf("\n后序:\n");
PostOrderTraverse(T);
break;
case 3:
printf("节点总数:\n");
printf("%d",GetSumNode());
printf("\n叶节点数:\n");
printf("%d",SumLeaf(T));
printf("\n非叶节点数:\n");
printf("%d", SumNoLeaf(T));
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
}
}