# Binary tree creation and traversal

Posted by Valect on Sat, 04 Apr 2020 01:40:39 +0200

Second level pointer creates a binary tree

```//==========================================Define the head
#include <iostream>
using namespace std;
struct BiTNode{
char data;
struct BiTNode *lchild, *rchild;//Left and right children
};
void CreateBiTree(BiTNode**T);//This must be a second level pointer and cannot be changed to bitnode * * & T
void Inorder(BiTNode* T);//Can be changed to bitnode * & T
void PreOrderTraverse(BiTNode* T);//Can be changed to bitnode * & T
void Posorder(BiTNode* T);//Can be changed to bitnode * & T
//===========================================Main function
int main(){
cout<<"Create a tree where A->Z The character represents the data of the tree“#”Represents an empty tree:“<<endl;
BiTNode*T=NULL;//Define T as a first level pointer
CreateBiTree(&T);//So, what we have to pass here is the address of T
cout<<"First order recursive traversal:"<<endl;
PreOrderTraverse(T);
cout<<endl;
cout<<"Middle order recursive traversal:"<<endl;
Inorder(T);
cout<<endl;
cout<<"Subsequent recursive traversal:"<<endl;
Posorder(T);
cout<<endl;
system("pause");
return 1;
}
//=============================================First order recursion to create binary tree
void CreateBiTree(BiTNode** T){
char ch;
if((ch=getchar())=='#') *T=NULL;//Note: Yes * T=NULL;
else{
(*T)=new BiTNode;//Be careful
(*T)->data=ch;//Be careful
CreateBiTree(&(*T)->lchild);//Be careful
CreateBiTree(&(*T)->rchild);//Be careful
}
}
//===============================================First order recursive traversal of binary tree
void PreOrderTraverse(BiTNode* T){
//First order recursive traversal of binary tree
if(T){//Execute when the node is not empty
cout<<T->data;
PreOrderTraverse(T->lchild);//
PreOrderTraverse(T->rchild);
}
else cout<<"";
}//PreOrderTraverse
//================================================Middle order ergodic binary tree
void Inorder(BiTNode* T){//Middle order recursively traversing binary tree
if(T){//bt=null fallback
Inorder(T->lchild);//Middle order traversal left subtree
cout<<T->data;//Access parameters
Inorder(T->rchild);//Middle order traversal right subtree
}
else cout<<"";
}//Inorder
//=================================================Postorder recursive traversal of binary tree
void Posorder(BiTNode* T){
if(T){
Posorder(T->lchild);//Backward recursive traversal of left subtree
Posorder(T->rchild);//Backward recursive traversal of right subtree
cout<<T->data;//Access root
}
else cout<<"";
}
//=================================================
```

First level pointer creates a binary tree

```//==========================================Define the head
#include <iostream>
using namespace std;
struct BiTNode{
char data;
struct BiTNode *lchild, *rchild;//Left and right children
};
void CreateBiTree(BiTNode* &T);
void Inorder(BiTNode* &T);
void PreOrderTraverse(BiTNode* &T);
void Posorder(BiTNode* &T);
//===========================================Main function
int main(){
cout<<"Create a tree where A->Z The character represents the data of the tree“#”Represents an empty tree:“<<endl;
BiTNode*T=NULL;//
CreateBiTree(T);//Passing the first level pointer T
cout<<"First order recursive traversal:"<<endl;
PreOrderTraverse(T);
cout<<endl;
cout<<"Middle order recursive traversal:"<<endl;
Inorder(T);
cout<<endl;
cout<<"Subsequent recursive traversal:"<<endl;
Posorder(T);
cout<<endl;
system("pause");
return 1;
}
//=============================================First order recursion to create binary tree
void CreateBiTree(BiTNode* &T){
char ch;
if((ch=getchar())=='#')T=NULL;
else{
T=new BiTNode;//
T->data=ch;//
CreateBiTree(T->lchild);//
CreateBiTree(T->rchild);//
}
}
//===============================================First order recursive traversal of binary tree
void PreOrderTraverse(BiTNode* &T){
//First order recursive traversal of binary tree
if(T){//Execute when the node is not empty
cout<<T->data;
PreOrderTraverse(T->lchild);//
PreOrderTraverse(T->rchild);
}
else cout<<"";
}//PreOrderTraverse
//================================================Middle order ergodic binary tree
void Inorder(BiTNode* &T){//Middle order recursively traversing binary tree
if(T){//bt=null fallback
Inorder(T->lchild);//Middle order traversal left subtree
cout<<T->data;//Access parameters
Inorder(T->rchild);//Middle order traversal right subtree
}
else cout<<"";
}//Inorder
//=================================================Postorder recursive traversal of binary tree
void Posorder(BiTNode* &T){
if(T){
Posorder(T->lchild);//Backward recursive traversal of left subtree
Posorder(T->rchild);//Backward recursive traversal of right subtree
cout<<T->data;//Access root node
}
else cout<<"";
}
//=================================================
```

Summary: the difference between the two lies only in the difference of creating this function in binary tree (respectively, void CreateBiTree(BiTNode**T), void createbitree (bitnode * & T)), and the difference when the main function calls this function (respectively, createbitree (& T), CreateBiTree(T)); for traversal, they are the same.
First level pointer must use reference;
Second level pointer cannot be referenced;
For secondary pointers:
Establish:

```void CreateBiTree(BiTNode** T){
char ch;
if((ch=getchar())=='#') *T=NULL;//Note: Yes * T=NULL;
else{
(*T)=new BiTNode;//Be careful
(*T)->data=ch;//Be careful
CreateBiTree(&(*T)->lchild);//Be careful
CreateBiTree(&(*T)->rchild);//Be careful
}
}```

Call:

```BiTNode*T=NULL;
CreateBiTree(&T)```

For first level pointers:
Establish:

```void CreateBiTree(BiTNode* &T){
char ch;
if((ch=getchar())=='#')T=NULL;
else{
T=new BiTNode;//
T->data=ch;//
CreateBiTree(T->lchild);//
CreateBiTree(T->rchild);//
}
}```

Call:

```BiTNode*T=NULL;
CreateBiTree(T)```