二叉树相关操作之一

//erchashu.h

#include<iostream>
#include<stdio.h>
#include<queue>

using namespace std;


template <class T> 
struct BinTreeNode {	      //二叉树结点类定义
     T data;	 		      //数据域
     BinTreeNode<T> *leftChild, *rightChild;
 	                                           //左子女、右子女链域
     BinTreeNode ()                //构造函数
     { 
		 leftChild = NULL;
		 rightChild = NULL;
	 }
	 BinTreeNode (T x)
     {
		 BinTreeNode<T> *l = NULL;
		 BinTreeNode<T> *r = NULL;
		 data = x;
		 leftChild = l; 
		 rightChild = r; 
	 }
};


template <class T>
class BinaryTree
{
	public:
	void CreateBinTree1()
	{
		CreateBinTree(root);
	}
	void PreOrder1()
	{
		PreOrder(root);
	}
	void InOrder1()
	{
		InOrder(root);
	}
	void PostOrder1()
	{
		PostOrder(root);
	}
	void levelOrder1()
	{
		levelOrder(root);
	}
	int Hight1()
	{
		return Hight(root);
	}
	int Size1()
	{
		return Size(root);
	}
	int leafSize1()
	{
		return leafSize(root);
	}
	int doubleSize1()
	{
		return doubleSize(root);
	}
	int singleSize1()
	{
		return singleSize(root);
	}
	private:
	void CreateBinTree(BinTreeNode<T> *& subTree);    //建立二叉树	
	void PreOrder(BinTreeNode<T> *& subTree);			//前序、
	void InOrder(BinTreeNode<T> *& subTree);				//中序、
	void PostOrder(BinTreeNode<T> *& subTree);			//后序
	void levelOrder(BinTreeNode<T> *& root);					//层次顺序遍历二叉树的算法
	int Hight(BinTreeNode<T> *  subTree);               //二叉树深度、
	int Size(BinTreeNode<T> *  subTree);					//所有结点总数、
	int leafSize(BinTreeNode<T> *  subTree);				//叶子结点数、
	int doubleSize(BinTreeNode<T> *  subTree);			//双孩子结点个数、
	int singleSize(BinTreeNode<T> *  subTree);			//单孩子结点个数的算法
	BinTreeNode<T> *root;
	char ch;
};




template <class T>
void BinaryTree<T>::CreateBinTree(BinTreeNode<T> *& subTree)    //建立二叉树
{
	scanf("%c", &ch);
	if (ch == 'a')
	{
		subTree = NULL;
	}
	else 
	{
		subTree = new BinTreeNode<T>;
		if (subTree == NULL) 
		{
			cerr << "存储分配错!" << endl;
			exit (1);
		}
		subTree->data = ch;		
		CreateBinTree(subTree->leftChild);
		CreateBinTree(subTree->rightChild);
	}
}

template <class T>
void BinaryTree<T>::PreOrder(BinTreeNode<T> *& subTree)			//前序、
{
	if (subTree != NULL)
	{
		cout << subTree->data;
		PreOrder(subTree->leftChild);
		PreOrder(subTree->rightChild);
	}
}

template <class T>
void BinaryTree<T>::InOrder(BinTreeNode<T> *& subTree)				//中序、
{
	if (subTree != NULL)
	{
		InOrder(subTree->leftChild);
		cout << subTree->data;
		InOrder(subTree->rightChild);
	}
}


template <class T>
void BinaryTree<T>::PostOrder(BinTreeNode<T> *& subTree)			//后序
{
	if (subTree != NULL)
	{
		PostOrder(subTree->leftChild);
		PostOrder(subTree->rightChild);
		cout << subTree->data;
	}
}



template <class T>
void BinaryTree<T>::levelOrder(BinTreeNode<T> *& root)					//层次顺序遍历二叉树的算法
{
	if (root == NULL) return;
	queue<BinTreeNode<T> * > Q;  
	Q.push(root); 	
	while (!Q.empty()) 
	{
		cout << Q.front()->data;
		if (Q.front()->leftChild != NULL)
		{   
			Q.push(Q.front()->leftChild);
		}
		if (Q.front()->rightChild != NULL)
		{
			Q.push(Q.front()->rightChild);
		}
		Q.pop();
	}			 
}



template <class T>
int BinaryTree<T>::Hight(BinTreeNode<T> *  subTree)               //二叉树深度、
{
	if (subTree == NULL)
		return 0;
	else 
	{
		int i = Hight(subTree->leftChild);
		int j = Hight(subTree->rightChild);
		return i > j ? i + 1 : j + 1;
	}
}

template <class T>
int BinaryTree<T>::Size(BinTreeNode<T> *  subTree)					//所有结点总数、
{
	if (subTree == NULL)
		return 0;
	else
		return 1 + Size(subTree->leftChild) + Size(subTree->rightChild);
}

template <class T>
int BinaryTree<T>::leafSize(BinTreeNode<T> *  subTree)				//叶子结点数、
{
	if (subTree == NULL)
	{
		return 0;
	}
	if (subTree->leftChild == NULL && subTree->rightChild == NULL)
	{
		return 1;
	} 
	else 
	{
		return leafSize(subTree->leftChild) + leafSize(subTree->rightChild);
	}
}

template <class T>
int BinaryTree<T>::doubleSize(BinTreeNode<T> *  subTree)			//双孩子结点个数、
{
	if (subTree == NULL)
		return 0;
	else 
	{
		if (subTree->leftChild != NULL && subTree->rightChild != NULL)
			return 1;
		else
			return doubleSize(subTree->leftChild) + doubleSize(subTree->rightChild);
	}
}


template <class T>
int BinaryTree<T>::singleSize(BinTreeNode<T> *  subTree)			//单孩子结点个数的算法
{
	if (subTree == NULL)
		return 0;
	else 
	{
		if (subTree->leftChild == NULL || subTree->rightChild == NULL)
			return 1;
		else
			return singleSize(subTree->leftChild) + singleSize(subTree->rightChild);
	}
}

//main.h

#include"erchashu.h"


int main()
{
	BinaryTree<char> s;
	s.CreateBinTree1();
	cout << "前序" << endl;
	s.PreOrder1();
	cout << endl;
	cout << "中序" << endl;
	s.InOrder1();
	cout << endl;
	cout << "后序" << endl;
	s.PostOrder1();
	cout << endl;
	cout << "层次顺序遍历" << endl;
	s.levelOrder1();
	cout << endl;
	cout << "二叉树深度、" << endl;
	cout << s.Hight1() << endl;
	cout << "所有结点总数" << endl;
	cout << s.Size1() << endl;
	cout << "叶子结点数" << endl;
	cout << s.leafSize1() << endl;
	cout << "双孩子结点个数" << endl;
	cout << s.doubleSize1() << endl;
	cout << "单孩子结点个数" << endl;
	cout << s.singleSize1() << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值