# Application of Binary Tree Traversal (Build Binary Tree, Find Tree Depth, Copy Binary Tree)

Keywords: data structure Binary tree

### Establishing Binary Tree Storage Structure--Binary Chain List

The same binary tree can be built in different order, which corresponds to different codes and different user inputs. The process of building a binary tree in different order is different but the final result is the same, so the binary tree can be built by traversing in order, but the output can be scanned by traversing in order. To avoid ambiguity, the left and right child pointers of leaf nodes must be entered with spaces (or other characters) when the user enters, indicating that the pointer is NULL, and the user must enter it strictly in the established order. The following is an example of building a binary tree with sequential traversal: (building a binary tree with paths traversed in order, reading and outputting from paths traversed in order)

```#include<stdio.h>

typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;    //Left and right child pointer
}BiTNode, *BiTree;

//Create a binary tree Users enter data along paths traversed in order
//To avoid ambiguity, the left and right children of the leaf nodes should be entered as spaces when the user enters them.
void CreateBiTree(BiTree &T)
{
char c;
scanf("%c", &c);
if (c == ' ')
{  //If it is the first input, the input space indicates that this is an empty tree, and if it is the space entered on subsequent recursive calls, T->l(r) child is NULL
T = NULL;
}
else      //If a specific value is entered, a new node is opened to enter the data into the data field and recursively manipulate its left and right child pointers
{
T = new BiTNode;
T->data = c;
CreateBiTree(T->lchild);     //The paths traversed in order are established, so the order of the three rows is fixed
CreateBiTree(T->rchild);     //Users must also enter in the order in which they are traversed
}
}

//Each node of a binary tree is traversed through a variety of operations that should be written out separately and placed in the visit function
void visit(char c,int lever)
{
printf("%c Located at %d layer\n", c, lever);
}
//Pre-Ordered Traversal of Binary Trees
void Preoder(BiTree T, int lever)
{
if (T)     //Determine whether to proceed by determining if the node is empty
{
Preoder(T->lchild, lever + 1);
visit(T->data, lever);
Preoder(T->rchild, lever + 1);
}
}

void main()
{
int lever = 1;
BiTree T = NULL;
CreateBiTree(T);
Preoder(T, lever);
}

```

### Finding the Tree Depth of a Binary Tree

Two auxiliary variables, h and depth, need to be defined in advance when finding the tree depth. Where h denotes the level at which the node T points, with an initial value of 1 and depth denoting the maximum level currently found, with an initial value of 0; Is also a recursive function, the idea is: first traverse from the root node, while recording the current number of layers h, compare depth, if depth < h, then   depth = h; Then start traversing their left and right children to do the same thing, so call the function for tree depth again. Form recursion from this.

```#include<stdio.h>

typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;    //Left and right child pointer
}BiTNode, *BiTree;

//Create a binary tree Users enter data along paths traversed in order
//To avoid ambiguity, the left and right children of the leaf nodes should be entered as spaces when the user enters them.
void CreateBiTree(BiTree &T)
{
char c;
scanf("%c", &c);
if (c == ' ')
{  //If it is the first input, the input space indicates that this is an empty tree, and if it is the space entered on subsequent recursive calls, T->l(r) child is NULL
T = NULL;
}
else      //If a specific value is entered, a new node is opened to enter the data into the data field and recursively manipulate its left and right child pointers
{
T = new BiTNode;
T->data = c;
CreateBiTree(T->lchild);     //The paths traversed in order are established, so the order of the three rows is fixed
CreateBiTree(T->rchild);     //Users must also enter in the order in which they are traversed
}
}

//Each node of a binary tree is traversed through a variety of operations that should be written out separately and placed in the visit function
void visit(char c,int lever)
{
printf("%c Located at %d layer\n", c, lever);
}
//Pre-Ordered Traversal of Binary Trees
void Preoder(BiTree T, int lever)
{
if (T)     //Determine whether to proceed by determining if the node is empty
{
Preoder(T->lchild, lever + 1);
visit(T->data, lever);
Preoder(T->rchild, lever + 1);
}
}

//Find Tree Depth
void BiTreeDepth(BiTree T, int h, int &depth)
{
//T is the root node of the tree, h is the level at which the node referred to by T has an initial value of 1, depth is the maximum level currently found and its initial value is 0
if (T)
{
if (h > depth)
depth = h;
BiTreeDepth(T->lchild, h + 1, depth);
BiTreeDepth(T->rchild, h + 1, depth);
}
}

void main()
{
int h = 1, depth = 0;
int lever = 1;
BiTree T = NULL;
CreateBiTree(T);
Preoder(T, lever);
BiTreeDepth(T, h, depth);
printf("The depth of the tree is:%d\n", depth);
}

```

### Duplicate a binary tree

Duplicate binary tree refers to a binary tree that already exists in the computer. Now it is necessary to regenerate a binary tree according to the original binary tree structure. The essence of duplicate binary tree is to create a new binary chain table according to the binary chain table of the original binary tree. "Copy" can be done either in a sequential traversal or in a sequential traversal. The following is an example of replication in a sequential traversal:

The idea of the algorithm is to construct two functions, one to return the pointer that generates the new node and the other to get the root pointer of the replica. The former is to assign a pointer to a new node, the latter is to use recursion. If the left subtree of the root node is known not to be empty, then the function of the call itself is assigned a pointer to the left subtree. If the right subtree of the root node is known not to be empty, then the function of the call itself is assigned a pointer to the right subtree. Finally, the root pointer is generated from the pointer of the left subtree and right subtree and data elements.

```#include<stdio.h>

typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;    //Left and right child pointer
}BiTNode, *BiTree;

//Create a binary tree Users enter data along paths traversed in order
//To avoid ambiguity, the left and right children of the leaf nodes should be entered as spaces when the user enters them.
void CreateBiTree(BiTree &T)
{
char c;
scanf("%c", &c);
if (c == ' ')
{  //If it is the first input, the input space indicates that this is an empty tree, and if it is the space entered on subsequent recursive calls, T->l(r) child is NULL
T = NULL;
}
else      //If a specific value is entered, a new node is opened to enter the data into the data field and recursively manipulate its left and right child pointers
{
T = new BiTNode;
T->data = c;
CreateBiTree(T->lchild);     //The paths traversed in order are established, so the order of the three rows is fixed
CreateBiTree(T->rchild);     //Users must also enter in the order in which they are traversed
}
}

//Each node of a binary tree is traversed through a variety of operations that should be written out separately and placed in the visit function
void visit(char c,int lever)
{
printf("%c Located at %d layer\n", c, lever);
}
//Pre-Ordered Traversal of Binary Trees
void Preoder(BiTree T, int lever)
{
if (T)     //Determine whether to proceed by determining if the node is empty
{
visit(T->data, lever);
Preoder(T->lchild, lever + 1);
Preoder(T->rchild, lever + 1);
}
}

//Duplicate Binary Tree
BiTNode *GetTreeNode(char item, BiTNode *lptr, BiTNode *rptr)
{
//Generates a node whose element value is item, left pointer is lptr, right pointer is rptr
BiTree T;
T = new BiTNode;
T->data = item;
T->lchild = lptr;
T->rchild = rptr;
return T;
}
BiTNode *CopyTree(BiTree T)
{
//Known that the root node of a binary tree is T, returns the root pointer of its replica
BiTNode *newlptr, *newrptr, *newnode;
if (!T)
return NULL;     //Duplicate an empty tree
if (T->lchild)
newlptr = CopyTree(T->lchild);     //Copy (traverse) left subtree
else
newlptr = NULL;
if (T->rchild)
newrptr = CopyTree(T->rchild);     //Copy (traverse) right subtree
else
newrptr = NULL;
newnode = GetTreeNode(T->data, newlptr, newrptr);   //Generate Root Node
return newnode;
}

void main()
{
int lever = 1;
BiTree T = NULL;
BiTree F;   //F for receiving copies
CreateBiTree(T);
printf("The initial binary tree is\n");
Preoder(T, lever);
F = CopyTree(T);
printf("Copies are\n");
Preoder(F, lever);
}

```

This note is based on Yan Weimin's "Tutorial of Data Structure and Application Algorithms"

All code will work correctly on Visual Studio 2017

If there are any errors, you are welcome to point out

Posted by gatoruss on Fri, 05 Nov 2021 09:42:37 -0700