二级指针实现二叉树的构造以二叉树的三种递归遍历具体代码实现

二级指针实现二叉树的构造

首先二级指针作为函数参数的作用:在函数外部定义一个指针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;
}

猜你喜欢

转载自blog.csdn.net/jfwzy109127/article/details/84110751
今日推荐