1. 程式人生 > >二叉樹、二叉連結串列

二叉樹、二叉連結串列

</pre><pre name="code" class="cpp">#include <iostream>
using namespace std;
typedef char elemtype;
int n=0;
typedef struct BiNode
{
	elemtype data;
	BiNode *lchild, *rchild;
}BiNode;
class BiTree
{    
public:
	BiTree(){root=Creat();}
	~BiTree(){Release(root);}   
	BiNode * Getroot();
	void PreOrder(BiNode *root); //前序遍歷遞迴演算法
	void PreOrder2(BiNode *root);  //前序遍歷非遞迴演算法
	void InOrder(BiNode *root);   //中序遍歷遞迴演算法
	void InOrder2(BiNode *root); //中序遍歷非遞迴演算法
	void PostOrder(BiNode *root); //後序遍歷遞迴演算法
	void PostOrder2(BiNode *root); //後序遍歷非遞迴演算法
	void LeverOrder(BiNode *root); //層序遍歷
	int  Depth(BiNode *root)  ; //求深度
	void Count(BiNode *root); //求二叉樹的結點個數
private:
	BiNode *root; 
	BiNode* Creat();
	void Release(BiNode *root);
};

BiNode* BiTree::Getroot( )
{
	return root;
}
void   BiTree::PreOrder(BiNode *root) 
{
	if (root ==NULL)  return;    //遞迴呼叫的結束條件 
	else {
		cout<<root->data;         //訪問根結點bt的資料域
		PreOrder(root->lchild);    //前序遞迴遍歷bt的左子樹
		PreOrder(root->rchild);    //前序遞迴遍歷bt的右子樹
	}
}
void BiTree::PreOrder2(BiNode *root) 
{
	int top= -1;      //採用順序棧,並假定不會發生上溢
	BiNode * s[100];
	while (root!=NULL || top!= -1)
	{
		while (root!= NULL)
		{
			cout<<root->data;
			s[++top]=root;
			root=root->lchild;  
		}
		if (top!= -1) { 
			root=s[top--];
			root=root->rchild;  
		}
	}
}


void BiTree::InOrder (BiNode *root)//
{
	if (root==NULL) return;     
	else {
		InOrder(root->lchild); 
		cout<<root->data; 
		InOrder(root->rchild);
	}
}
void BiTree::InOrder2(BiNode *root)
{
	int top=-1;  //採用順序棧,並假定不會發生上溢
	BiNode* s[100];
	while (root !=NULL||top!=-1)
	{
		while(root!=NULL)
		{
			s[++top]=root;  //將根指標root入棧
			root=root->lchild;
		}
		if (top!=-1)          //棧非空
		{
			root=s[top--];
			cout<<root->data;
			root=root->rchild;
		}
	}
}
void BiTree::PostOrder(BiNode *root)
{ 
	if (root==NULL) return; 
	else {
		PostOrder(root->lchild); 
		PostOrder(root->rchild); 
		cout<<root->data;          
	}
}

void BiTree::LeverOrder(BiNode *root)
{
	int front,rear;
	BiNode * Q[100];
	BiNode *q;
	front=rear=-1;    //採用順序佇列,並假定不會產生上溢
	if(root==NULL) return ;
	Q[++rear]=root;
	while (front!=rear)
	{
		q=Q[++front];
		cout<<q->data;
		if(q->lchild!=NULL)  Q[++rear]=q->lchild;
		if(q->rchild!=NULL)  Q[++rear]=q->rchild;
	}
}

/*
 *前置條件:空二叉樹
 *輸    入:資料ch;
 *功    能:初始化一棵二叉樹,建構函式呼叫
 *輸    出:無
 *後置條件:產生一棵二叉樹
 */
BiNode* BiTree::Creat()
{
	BiNode* root;
	elemtype ch;
	cout<<"請輸入建立一棵二叉樹的結點資料"<<endl;
	cin>>ch;
	if (ch=='#') root = NULL;
	else{ 
		root = new BiNode;       //生成一個結點
		root->data=ch;
		root->lchild = Creat();    //遞迴建立左子樹
		root->rchild = Creat();    //遞迴建立右子樹
	} 
	return root;
}

void BiTree::Count(BiNode *root)  //n為全域性量並已初始化為0
{
	if (root) {
		Count(root->lchild);
		n++;
		Count(root->rchild);
	}
}
int  BiTree::Depth(BiNode *root)  //求二叉樹深度
{
	int hl ,hr;
	if (root==NULL) return 0;
	else {
		hl= Depth(root->lchild);
		hr= Depth(root ->rchild);
		return max(hl, hr)+1;
	}
}
void BiTree::Release(BiNode *root)
{
	if (root!=NULL)
	{
		Release(root->lchild); //釋放左子樹
		Release(root->rchild); //釋放右子樹
		delete root;    //釋放根結點
	}
}

void main()
{
	BiTree bt;
//	cout<<"請輸入二叉樹序列,如:AB#D##C##"<<endl;
	BiNode *root=bt.Getroot();
	cout<<"樹的前序遍歷序列為:"<<endl;
	bt.PreOrder(root);
	cout<<endl;
	cout<<"------中序遍歷------ "<<endl;
	bt.InOrder(root);
	cout<<endl;
	cout<<"------後序遍歷------ "<<endl;
	bt.PostOrder(root);
	cout<<endl;
	cout<<"------層序遍歷------ "<<endl;
	bt.LeverOrder(root);
	cout<<endl;
	cout<<bt.Depth(root)<<endl;
}