1. 程式人生 > >y-can-choose-kiss-me

y-can-choose-kiss-me

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