关闭

二叉树相关操作之一

259人阅读 评论(0) 收藏 举报
分类:

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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3563次
    • 积分:148
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档