二級指標實現二叉樹的構造以二叉樹的三種遞迴遍歷具體程式碼實現
二級指標實現二叉樹的構造
首先二級指標作為函式引數的作用:在函式外部定義一個指標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;
}