C++ 二叉树的基本操作

一:目的

用C++实现二叉树的基本操作,建立和遍历;

二:介绍

在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。

二叉树的遍历方式有三种,前序遍历,中序遍历,后序遍历。

它的前序遍历顺序为:ABDGHCEIF(规则是先是根结点,再前序遍历左子树,再前序遍历右子树)

它的中序遍历顺序为:GDHBAEICF(规则是先中序遍历左子树,再是根结点,再是中序遍历右子树)

它的后序遍历顺序为:GHDBIEFCA(规则是先后序遍历左子树,再是后序遍历右子树,再是根结点)

三:实现

1. 首先创建二叉树结构,以及二叉树的类,定义在BinaryTree.h中

#include <iostream>
using namespace std;

typedef char ElemType;

//定义树的节点
typedef struct BiNode
{
	ElemType data;			//节点的数据类型
	BiNode * left;			//左子树
	BiNode * right;			//右子树
	BiNode(ElemType val)
	{
		data = val;
		left = NULL;
		right = NULL;
	}
}BiNode,*BiTree;


//二叉树类
class BinaryTree
{
public:
	void Create();				//递归的创建二叉树的节点
	int getSize();				//递归得到树的节点数目
	int getHeight();			//递归得到树的高度
     void preOrder();			//前序遍历
     void inOrder();			//中序遍历
     void postOrder();			//后序遍历
     void distroy();			//删除二叉树

private:
	BiTree create();						//递归的创建二叉树的节点
	void preOrder(BiTree root);				//前序遍历
	void inOrder(BiTree root);				//中序遍历
	void postOrder(BiTree root);			//后序遍历
	void distroy(BiTree root);				//摧毁树
	int getHeight(BiTree root);				//递归得到树的高度
	void AddNode(const ElemType key,int direction, BiTree root);         //添加节点
	BiTree m_root;      //根节点
	int size;			//节点总数
};
2. 具体实现在BinaryTree.cpp中
# include "BinaryTree.h"


#pragma region 私有成员函数
//添加节点
//key为要插入的值,direction是从左子树插入还是右子树插入,root为从哪个节点插入
void BinaryTree::AddNode(const ElemType key,int direction, BiTree root)
{
	if(direction == 0)
	{
		//从左子树插入
		if(root->left == NULL)
			root->left = new BiNode(key);
		else
			AddNode(key, direction,root->left);
	}
	else if(direction == 1)
	{
		//从右子树插入
		if(root->right == NULL)
			root->right = new BiNode(key);
		else
			AddNode(key, direction,root->right);
	}
}

////二叉树的建立,按前序遍历的方式建立二叉树
BiTree BinaryTree::create()
{
	 BiTree current = NULL; 
	 ElemType val;
     cin >> val;//输入键值
     
	 if(val == '#')//标识当前子树为空,转向下一节点
     {
         return NULL;
     }  
     else
	 {   //递归的创建左右子树
		 size++;
         current = new BiNode(val);
		 current->left = create();
         current->right = create();
         return current;
     }
}

//删除二叉树(private 函数)
void BinaryTree ::distroy(BiTree root)
{
	if(root)
	{
		distroy(root->left);
		distroy(root->right);
		delete root;
		root = NULL;
		size = 0;
	}
}

//递归得到树的高度
int BinaryTree::getHeight(BiTree root)
{
	if(root == NULL)
		return 0;
	int left_height = getHeight(root->left);
	int right_height = getHeight(root->right);
	return (left_height>right_height)? (left_height+1):(right_height+1);
}

//前序遍历
void BinaryTree::preOrder(BiTree root)
{
	if(root == NULL)
		return;
	else
	{
		cout << root->data << "  -->  ";     //首先打印根节点
		preOrder(root->left);				 //接着遍历左子树
		preOrder(root->right);				 //接着遍历右子树
	}
}

//中序遍历
void BinaryTree::inOrder(BiTree root)
{
	if(root == NULL)
		return;
	else
	{
		inOrder(root->left);				 //首先遍历左子树
		cout << root->data << "  -->  ";     //接着打印根节点
		inOrder(root->right);				 //接着遍历右子树
	}
}

//后序遍历
void BinaryTree::postOrder(BiTree root)
{
	if(root == NULL)
		return;
	else
	{
		postOrder(root->left);				 //首先遍历左子树
		postOrder(root->right);				 //接着遍历右子树
		cout << root->data << "  -->  ";     //接着打印根节点
	}
}
#pragma endregion


#pragma region 公有成员函数
////二叉树的建立
void BinaryTree::Create()
{
	size = 0;
	m_root = create();
}

//删除二叉树
void BinaryTree ::distroy()
{
	distroy(m_root);
}

//递归得到树的高度
int BinaryTree::getHeight()
{
	return getHeight(m_root);
}

//前序遍历
void BinaryTree::preOrder()
{
	cout << "前序遍历的结果如下:"<<endl;
	preOrder(m_root);
	cout << endl;
}

//中序遍历
void BinaryTree::inOrder()
{
	cout << "中序遍历的结果如下:"<<endl;
	inOrder(m_root);
	cout << endl;
}

//后序遍历
void BinaryTree::postOrder()
{
	cout << "后序遍历的结果如下:"<<endl;
	postOrder(m_root);
	cout << endl;
}

//获取大小
int BinaryTree::getSize()
{
	//这里是创建时候直接进行了计数
	//也可以利用遍历的方式获取,当节点有值,就加1
	return size;
}
#pragma endregion
3. 简单测试
前序遍历的方式创建如下图所示二叉树,应当输入:ABDG##H###CE#I##F##

测试代码为:
BinaryTree tree;
cout << "按前序遍历方式创建树" <<endl;
//"ABDG##H###CE#I##F##";
tree.Create();
cout << "树的高度为:" << tree.getHeight() <<endl;
cout << "树的节点为:" << tree.getSize() <<endl;
tree.preOrder();			//前序遍历
tree.inOrder();				//中序遍历
tree.postOrder();			//后序遍历
tree.distroy();				//摧毁树
system("pause");
测试结果为:





参考:
https://www.cnblogs.com/SarahZhang0104/p/5827532.html
https://www.cnblogs.com/liuamin/p/6269950.html









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章