#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef char ElemType;
//定义结构体
typedef struct BSTNode
{
ElemType data;
struct BSTNode* RChild;
struct BSTNode* LChild;
}BSTNode,*BSTree;
//输入一个二叉树的扩展先序遍历序列(例如ABC##DE#G##F###),创建该二叉树;
void CreatBSTree(BSTree& T)
{
char ch = 0;
cin >> ch;
if (ch == '#')
{
T = NULL;
}
else
{
T = new BSTNode;
T->data = ch;
CreatBSTree(T->LChild);
CreatBSTree(T->RChild);
}
}
//中序遍历
void InOrderTraverse(BSTree T)
{
if (T)
{
InOrderTraverse(T->LChild);
cout << T->data;
InOrderTraverse(T->RChild);
}
}
//先序遍历
void priorTraverse(BSTree T)
{
if (T)
{
cout << T->data;
priorTraverse(T->LChild);
priorTraverse(T->RChild);
}
}
//后序遍历
void postTraverse(BSTree T)
{
if (T)
{
postTraverse(T->LChild);
postTraverse(T->RChild);
cout << T->data;
}
}
//计算二叉树的深度
int DepthTree(BSTree& T)
{
int m = 0;
int n = 0;
if (T == NULL)
{
return 0;//树为空深度为0
}
else
{
m = DepthTree(T->LChild);
n = DepthTree(T->RChild);
if (m > n)
{
return m + 1;
}
else
{
return n + 1;
}
}
}
//计算节点个数
int NodeCount(BSTree& T)
{
int count = 0;
if (T == NULL)
{
return 0;
}
else
{
return NodeCount(T->LChild) + NodeCount(T->RChild) + 1;
}
}
//叶子节点的个数
int LeafNodeCount(BSTree& T)
{
int count = 0;
if (T == NULL)
{
return 0;
}
else if (T->LChild == NULL && T->RChild == NULL)
{
return 1;
}
else
{
return LeafNodeCount(T->LChild) + LeafNodeCount(T->RChild);
}
}
//计算度为1的节点
int Node1Count(BSTree& T)
{
int count = 0;
if (T == NULL)
{
return 0;
}
if ((T->LChild == NULL && T->RChild != NULL) || (T->RChild == NULL && T->LChild != NULL))
{
return 1;
}
return Node1Count(T->LChild) + Node1Count(T->RChild);
}
//计算度为2的节点
int Node2Count(BSTree& T)
{
int count = 0;
if (T == NULL)
{
return 0;
}
if ((T->LChild != NULL && T->RChild != NULL) || (T->RChild != NULL && T->LChild != NULL))
{
return 1;
}
return Node2Count(T->LChild) + Node2Count(T->RChild);
}
BSTree CopyBSTree(BSTree T)
{
if (T == NULL)
{
return NULL;
}
BSTree NewNode = new BSTNode;
NewNode->data = T->data;
NewNode->LChild = CopyBSTree(T->LChild);
NewNode->RChild = CopyBSTree(T->RChild);
return NewNode;
}
//新二叉树的前序遍历
void priorTraverseNewNode(BSTree NewNode)
{
if (NewNode)
{
cout << NewNode->data;
priorTraverseNewNode(NewNode->LChild);
priorTraverseNewNode(NewNode->RChild);
}
}
//新二叉树的中序遍历
void InOrderTraverseNewNode(BSTree NewNode)
{
if (NewNode)
{
InOrderTraverseNewNode(NewNode->LChild);
cout << NewNode->data;
InOrderTraverseNewNode(NewNode->RChild);
}
}
//新二叉树的后序遍历
void postTraverseNewNode(BSTree NewNode)
{
if (NewNode)
{
postTraverseNewNode(NewNode->LChild);
postTraverseNewNode(NewNode->RChild);
cout << NewNode->data;
}
}
//主函数
int main()
{
cout << "1.创建二叉树" << endl;
cout << "2.中序遍历的树" << endl;
cout << "3.前序遍历的树" << endl;
cout << "4.后序遍历的树" << endl;
cout << "5.二叉树的深度" << endl;
cout << "6.二叉树的节点个数" << endl;
cout << "7.叶子节点的个数" << endl;
cout << "8.度为1的节点的个数" << endl;
cout << "9.度为2的节点的个数为" << endl;
cout << "10.复制一个二叉树" << endl;
cout << "11.新二叉树的先序遍历" << endl;
cout << "12.新二叉树的中序遍历" << endl;
cout << "13.新二叉树的后序遍历" << endl;
BSTree T = NULL;
BSTree NewNode = NULL;
int choose = -1;
int capacity = 0;
char str = 0;
while (choose != 0)
{
cout << "请输入choose:" << endl;
cin >> choose;
switch (choose)
{
case 1:
cout << "请输入ch:" << endl;
CreatBSTree(T);
cout << "创建成功" << endl;
break;
case 2:
cout << "中序遍历的树为:" << endl;
InOrderTraverse(T);
printf("\n");
break;
case 3:
cout << "先序遍历的树为:" << endl;
priorTraverse(T);
printf("\n");
break;
case 4:
cout << "后序遍历的树为:" << endl;
postTraverse(T);
printf("\n");
break;
case 5:
cout << "二叉树的深度为:" << endl;
cout << DepthTree(T) << endl;
break;
case 6:
cout << "二叉树的节点个数为:" << endl;
cout << NodeCount(T) << endl;
break;
case 7:
cout << "叶子节点的个数为:" << endl;
cout << LeafNodeCount(T) << endl;
break;
case 8:
cout << "度为1的节点的个数为:" << endl;
cout << Node1Count(T) << endl;
break;
case 9:
cout << "度为2的节点的个数为:" << endl;
cout << Node2Count(T) << endl;
break;
case 10:
cout << "复制一个二叉树" << endl;
NewNode = CopyBSTree(T);
if (CopyBSTree(T) != NULL)
{
cout << "复制成功" << endl;
}
break;
case 11:
cout << "新二叉树的先序遍历" << endl;
priorTraverseNewNode(NewNode);
printf("\n");
break;
case 12:
cout << "新二叉树的中序遍历" << endl;
InOrderTraverseNewNode(NewNode);
printf("\n");
break;
case 13:
cout << "新二叉树的后序遍历" << endl;
postTraverseNewNode(NewNode);
printf("\n");
break;
}
}
}