二級指針實現二叉樹的構造以二叉樹的三種遞歸遍歷具體代碼實現

二級指針實現二叉樹的構造

首先二級指針作爲函數參數的作用:在函數外部定義一個指針p,在函數內給指針賦值,函數結束後對指針p生效,那麼我們就需要二級指針。不懂沒有關係,繼續往下看~加油!

在如上的A指向B、B指向C的指向關係中,如果A、B、C都是變量,即C是普通變量,B是一級指針變量,其中存放着C的地址,A是二級指針變量,其中存放着B的地址,則這3個變量分別在內存中佔據各自的存儲單元,它們之間的相互關係下圖所示,相互之間的前後位置關係並不重要.此時,B是一級指針變量,B的值(即C的地址)是一級指針數據;A是二級指針變量,A的值(即B的地址)是二級指針數據.

我們都知道二叉樹的建立可以用兩種方式

1.    class中聲明函數

        BT(){
        cout<<"請輸入建立二叉樹的節點數據"<<endl;
        this->root=Creat();
        }
        BT_node* Creat();

2.     BT(){
           cout<<"請按先序輸入二叉樹的節點信息"<<endl;
           Creat(&root);
       }
        void Creat(BT_node **root);

兩種函數是等價的,即BT_node* Creat();和void Creat(BT_node **root);效果是一樣的。

很多人包括剛開始學習二叉樹的我,都認爲第二個函數就只要用一級指針不就應該完成嘛?爲什麼非要用二級指針。

那麼問題來了!這兩個函數需要達到的目的是什麼????是不是創建一顆二叉樹,然後把你創建的二叉樹的根指針賦值給當前二叉樹的根指針!也就是你需要改變根節點指針的值!!

第一個函數是如何改變的呢??通過BT_node* Creat();函數return回一個指針,然後在構造函數中通過 this->root=Creat();改變二叉樹的根指針。

第二個函數呢??我們在剛開始接觸指針時,一定遇到交換兩個數的操作。顯然void  Fun_swap(int a,int b);是無法實現交換a和b的值。因爲這個時候我們傳遞的只是a和b的副本,副本的交換並不能交換內存空間中a和b的值。也就是我們熟知的,如果你要改變某個數,必須通過地址傳遞實現,也就是通過指針來實現!

那麼我們回到這裏~我們要實現的是改變二叉樹根指針的值,如果這時候通過一級指針void Creat(BT_node *root);是沒有辦法實現的,類比之前交換兩個數,這裏必須使用指針的指針才能改變指針的值!也就是void Creat(BT_node **root);

具體構造方法以及三種遞歸遍歷操作如下

#include <iostream>
using namespace std;
struct BT_node{
	char data;
	struct BT_node *lchild,*rchild;
};
class BT{
	public:
		BT_node *root;
	public:
		BT();
		BT_node* Creat();
		~BT();
		void Release(BT_node *root);
		void PreOrder(BT_node*root);
		void InOrder(BT_node*root);
		void PostOrder(BT_node *root);
};
BT::BT(){
	cout<<"請輸入建立二叉樹的節點數據"<<endl;
	this->root=Creat();
}
BT_node* BT::Creat(){
	BT_node *root;
	char ch;
	cin>>ch;
	if(ch=='#'){
		root=NULL;
	}else{
		root=new BT_node;
		root->data=ch;
		root->lchild=Creat();
		root->rchild=Creat();
	}
	return root;
}
BT::~BT(){
	Release(root);
}
void BT::Release(BT_node *root){
	if(root){
		Release(root->rchild);
		Release(root->lchild);
		delete root;
	}
}
void BT::PreOrder(BT_node *root){
	if(root==NULL){
		return;
	}else{
		cout<<root->data<<'\t';
		PreOrder(root->lchild);
		PreOrder(root->rchild);
	}
}
void BT::LevelOrder(BT_node *root){
	Queue Q;//隊列q
	if(root==NULL){
		return;
	}
	int i=0;
	Q.EnQueue(root);//隊頭元素入隊列
	while(!Q.Empty()){
		BT_node *p=Q.DeQueue();
		cout<<p->data<<'\t';
		if(p->lchild!=NULL){
			Q.EnQueue(p->lchild);
		}
		if(p->rchild!=NULL){
			Q.EnQueue(p->rchild);
		}
	} 
}
//中序遍歷 
void BT::InOrder(BT_node *root){
	if(root==NULL){
		return;
	}else{
		InOrder(root->lchild);
		cout<<root->data<<'\t';
		InOrder(root->rchild);
	}
}
void BT::PostOrder(BT_node *root){
	if(root==NULL){
		return;
	}else{
		PostOrder(root->lchild);
		PostOrder(root->rchild);
		cout<<root->data<<'\t';
	}
}
int main(){
	BT test;
	cout<<"先序遍歷"<<endl; 
	test.PreOrder(test.root);
	cout<<endl; 
	cout<<"中序遍歷"<<endl; 
	test.InOrder(test.root);
	cout<<endl; 
	cout<<"後序遍歷"<<endl; 
	test.PostOrder(test.root);
	cout<<endl; 
	return 0;
}
#include <iostream>
using namespace std;
//定義節點數據類型 
struct BT_node{
	char data;
	struct BT_node *lchild ,*rchild;
}; 
class BT{
	public:
		struct BT_node *root=NULL;
	public:
		BT();
		void Creat(BT_node **root);
		void Pre_order(BT_node *root);
		~BT();
		void Release(BT_node *root);
};
BT::BT(){
	cout<<"請按先序輸入二叉樹的節點信息"<<endl;
	Creat(&root);
}
void BT::Creat(BT_node **root){
	char ch;
	cin>>ch;
	if(ch=='#'){
		*root=NULL;
	}else{
		*root=new BT_node;
		(*root)->data=ch;
		Creat(&((*root)->lchild));
		Creat(&((*root)->rchild));
	}
}
void BT::Pre_order(BT_node *root){
	if(root==NULL){
		return;
	}else{
		cout<<root->data<<'\t';
		Pre_order(root->lchild);
		Pre_order(root->rchild);
	}
}
BT::~BT(){
	Release(root);
} 
void BT::Release(BT_node *root){
	if(root){
		Release(root->rchild);
		Release(root->lchild);
		delete root;
	}
}
int main(){
	BT test;
	test.Pre_order(test.root);
	if(test.root==NULL){
		cout<<"二叉樹爲空!"<<endl; 
	}
	return 0;
}

 

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