二叉树的遍历与应用

#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;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值