# Basic operation, traversal (recursive and non recursive) and node number of binary tree

If there is any mistake, please correct it, thank you.

If you think what you write is OK, use your little hand to praise and pay attention!

catalogue

Binary tree

Create binary tree

Traversal of binary tree

Hierarchical traversal of binary tree

Depth (height) of the tree

On the nodes of binary tree

Judge complete binary tree and full binary tree

The complete code is as follows

Test, result screenshot

## Binary tree

A binary tree has a degree of no more than 2 of Orderly A tree is another tree shaped knot structure
Features: each node has at most two subtrees; Binary tree Subtrees can be divided into left and right, and their order cannot be reversed arbitrarily
Property 1: in the second part of binary tree At most on layer i The (i-1) power of 2 Nodes
Property 2: depth k There are at most (k-1) power of 2 Nodes
Property 3: suppose there are n2 nodes with degree 2, n1 nodes with degree 1 and n0 nodes with degree 0 in the binary tree, then:
n0 = n 2+1
Property 4: the depth of a complete binary tree with n nodes is k=log2(n+1) (rounded up)   or k＝   log2(n)   (rounded down)  + one         ----- Log2 is the base 2 logarithm
Nature 5:   Number the complete binary tree from top to bottom, from left to right, The root number is 1 .
For No i Node of X yes:
1. If i = 1, X is the root; If i > 1, the number of X's parents is    i/2 (rounded down) ;
2. If 2I > N, then x has no left child, and X is the leaf node; If x has a left child, the number of the left child of X is 2i ；
3. If 2I + 1 > N, X has no right child; If x has a right child, the number of the right child of X is 2i＋1 ；
Property 6: a common binary tree with n nodes n＋1 Null pointer
```typedef struct BiTNode{
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree; ```

## Create binary tree

The code here uses the idea of recursion to create a binary tree in order, and represents the empty with '#';

To create the binary tree in the figure above, enter abd#g###e##c#fh#i###

```void CreateBiTree(BiTree &bt){
char ch;
cin >> ch;
if (ch == '#')
bt=NULL;
else
{
bt = new BiTNode;
bt -> data = ch;
CreateBiTree (bt -> lchild);
CreateBiTree (bt -> rchild);
}
}```

## Traversal of binary tree

Preorder traversal: Root -- left -- right

Middle order traversal: left -- root -- right

Post order traversal: left -- right -- root

The basic operation of traversing a binary tree is to access nodes. No matter which order is traversed, the time complexity of a binary tree with n nodes is O(n).
For the non recursive traversal algorithm, the auxiliary space required is the maximum capacity of the stack in the traversal process, that is, the depth of the tree. In the worst case, it is n, so its space complexity is also O(n).
Given the preorder traversal sequence and middle order traversal sequence, middle order traversal sequence and post order traversal sequence, hierarchical traversal sequence and middle order traversal sequence of a binary tree, a binary tree can be uniquely determined;
Given the preorder traversal sequence and postorder traversal sequence of a binary tree, a binary tree cannot be uniquely determined.
Recursive traversal and non recursive traversal of first order, middle order and second order
```//Traversal recursive algorithm -- preorder
void PreOrderTraverse1(BiTree bt){
if (bt)
{
cout << bt -> data << " ";   //root
PreOrderTraverse1(bt -> lchild);  //Left
PreOrderTraverse1(bt -> rchild);  //right
}
}
//Traversal non recursive algorithm -- using stack first
void PreOrderTraverse2(BiTree bt){
stack <BiTree> S;
BiTree p = bt;
while (p || !S.empty())
{
while (p)
{
S.push(p);
cout << p -> data << " ";
p = p -> lchild;
}
if (!S.empty())
{
p = S.top();
S.pop();
p = p -> rchild;
}
}
}

//Traversal recursive algorithm -- middle order
void InOrderTraverse1(BiTree bt){
if(bt)
{
InOrderTraverse1 (bt -> lchild);   //Left
cout << bt -> data << " ";        //root
InOrderTraverse1 (bt -> rchild);   //right
}
}
//Traversal non recursive algorithm -- using middle order stack
void InOrderTraverse2(BiTree bt){
stack <BiTree> S;
BiTree p = bt;
while (p || !S.empty())
{
if (p)
{
S.push(p);
p = p -> lchild;
}
else
{
p = S.top();
S.pop();
cout << p -> data << " ";
p = p -> rchild;
}
}
}

//Traversal recursive algorithm -- post order
void PostOrderTraverse1(BiTree bt){
if(bt)
{
PostOrderTraverse1 (bt -> lchild);   //Left
PostOrderTraverse1 (bt -> rchild);   //right
cout << bt -> data << " ";  	   //root
}
}
//Traversal non recursive algorithm -- using middle order stack
void PostOrderTraverse2(BiTree bt){
stack <BiTree> S1, S2;
BiTree p = NULL;
S1.push(bt);
while (!S1.empty())
{
p = S1.top();
S1.pop();
S2.push(p);
if (p -> lchild)
S1.push(p -> lchild);
if (p -> rchild)
S1.push(p -> rchild);
}
while (!S2.empty())
{
p = S2.top();
cout << p -> data << " ";
S2.pop();
}
}```

## Hierarchical traversal of binary tree

Thought:

1. Empty tree, end.
2. Initialize an empty queue Q, and the tree root enters the queue;
3. The peer e element goes out of the queue and accesses E;
4. If e there is a left child, the left child will join the team;
5. If e the right child is left, the right child will join the team;
6. If the column is not empty, execute 3; Otherwise it ends.
```//Hierarchical traversal -- using queues
void LevelOrderTraverse(BiTree bt){
queue <BiTree> Q;
BiTNode *p = bt;
if (bt)
{
Q.push (bt);
while (!Q.empty())
{
p = Q.front ();    //Queue header element out of queue
cout << p -> data << " ";
if (p -> lchild)
Q.push (p -> lchild);  //Non empty left child in queue
if (p -> rchild)
Q.push (p -> rchild);  //Non empty right child in queue
Q.pop ();
}
}
}```

## Depth (height) of the tree

Idea: according to the definition of binary tree, if the binary tree is empty, the depth is 0, otherwise the depth is the larger of the left and right subtrees plus 1.

```int Height(BiTree bt){
if(bt == NULL)
return 0;
else
{
int m = Height (bt -> lchild);
int n = Height (bt -> rchild);
if(m > n)
return (m + 1);
else
return (n + 1);
}
}```

## On the nodes of binary tree

Node: traverse every, we all know

Leaf node: this node has no left or right children. It is a leaf node

```//All nodes in binary tree
int CountTree (BiTree bt){
if (bt == NULL)
return 0;
else
return CountTree (bt -> lchild) + CountTree(bt -> rchild) + 1;
}
//Leaf node number
int CountLeaf (BiTree bt){
if (! bt)
return 0;
if (! bt -> lchild && ! bt -> rchild)
return 1;
else
return CountLeaf (bt -> lchild) + CountLeaf (bt -> rchild);
}
//Number of nodes with judgment degree of 1
int Count_1Node(BiTree bt){
if (!bt)
return 0;
if ((!bt -> lchild) && (bt -> rchild) || (bt -> lchild) && (!bt -> rchild))
return Count_1Node(bt -> lchild) + Count_1Node(bt -> rchild) + 1;
else
return Count_1Node(bt -> lchild) + Count_1Node(bt -> rchild);
}```

## Judge complete binary tree and full binary tree

Complete binary tree: number the binary tree with depth k and N nodes from top to bottom, from left to right, and the number is consistent with the first n nodes in the full binary tree with depth k.

characteristic:

1) Depth Lh of left subtree of each node i i -The depth Rh of its right subtree i Equal to 0 or 1, that is, leaf nodes can only appear on the two layers with the largest or sub largest hierarchy.
2) The number of complete binary tree nodes n satisfies the (k-1) power of 2 < the k-power of N < 2 - 1
3) A full binary tree must be a complete binary tree, otherwise it does not hold.

4) In a complete binary tree with n nodes:

The number of nodes with degree 1 is (n+1)%2

The number of nodes with degree 0 is (n + 1) / 2 (rounded down)

The number of nodes with degree 2 is (n + 1) / 2 (rounded up)  - one

5) The first k-1 layer is full, and the k-th layer can be dissatisfied, but the nodes of the k-th layer are concentrated on the left

Full binary tree: the depth is k, and the total number of nodes n = k Power of 2 - 1

characteristic:

1) The number of nodes in each layer reaches the maximum

2) There are only nodes with degree 0 and degree 2, and the number of nodes with degree 1 n1 = 0

The full binary tree can be judged according to: the total number of nodes n = 2 to the power of k - 1   and    The number of nodes with a degree of one n1 = 0

```//Determine whether it is a complete binary tree
bool Is_Comp(BiTree bt)
{
bool res = true;
if (!bt)
return res;
std:: queue <BiTree> Q;
Q.push(bt);
while (!Q.empty())
{
bt = Q.front();
Q.pop();
if (!bt)
break;
Q.push (bt -> lchild);
Q.push (bt -> rchild);
}
while (!Q.empty())
{
bt = Q.front();
Q.pop();
if (bt)
{
res = false;
break;
}
}
return res;
}
```

## The complete code is as follows

```​
#include<bits/stdc++.h>
using namespace std;
//Node structure
typedef struct BiTNode{
char data;
struct BiTNode *lchild, *rchild;  //Left and right child pointers
}BiTNode, *BiTree;

//Create binary tree first
void CreateBiTree(BiTree &bt){
char ch;
cin >> ch;
if (ch == '#')
bt=NULL;
else
{
bt = new BiTNode;    //  bt = (BiTNode*) malloc (sizeof(BiTNode)) //
bt -> data = ch;
CreateBiTree (bt -> lchild);
CreateBiTree (bt -> rchild);
}
}

//Traversal recursive algorithm -- preorder
void PreOrderTraverse1(BiTree bt){
if (bt)
{
cout << bt -> data << " ";   //root
PreOrderTraverse1 (bt -> lchild);  //Left
PreOrderTraverse1 (bt -> rchild);  //right
}
}

//Traversal non recursive algorithm -- using stack first
void PreOrderTraverse2(BiTree bt){
stack <BiTree> S;
BiTree p = bt;
while (p || !S.empty())
{
while (p)
{
S.push (p);
cout << p -> data << " ";
p = p -> lchild;
}
if (!S.empty ())
{
p = S.top ();
S.pop ();
p = p -> rchild;
}
}
}

//Traversal recursive algorithm -- middle order
void InOrderTraverse1(BiTree bt){
if (bt)
{
InOrderTraverse1 (bt -> lchild);   //Left
cout << bt -> data << " ";        //root
InOrderTraverse1 (bt -> rchild);   //right
}
}

//Traversal non recursive algorithm -- using middle order stack
void InOrderTraverse2(BiTree bt){
stack <BiTree> S;
BiTree p = bt;
while (p || !S.empty())
{
if (p)
{
S.push (p);
p = p -> lchild;
}
else
{
p = S.top ();
S.pop ();
cout << p -> data << " ";
p = p -> rchild;
}
}
}

//Traversal recursive algorithm -- post order
void PostOrderTraverse1(BiTree bt){
if (bt)
{
PostOrderTraverse1 (bt -> lchild);   //Left
PostOrderTraverse1 (bt -> rchild);   //right
cout << bt -> data << " ";  	   //root
}
}
//Traversal non recursive algorithm -- using middle order stack
void PostOrderTraverse2(BiTree bt){
stack <BiTree> S1, S2;
BiTree p = NULL;
S1.push (bt);
while (!S1.empty ())
{
p = S1.top ();
S1.pop ();
S2.push (p);
if (p -> lchild)
S1.push (p -> lchild);
if (p -> rchild)
S1.push (p -> rchild);
}
while (!S2.empty ())
{
p = S2.top ();
cout << p -> data << " ";
S2.pop ();
}
}

//Hierarchical traversal -- using queues
void LevelOrderTraverse(BiTree bt){
queue <BiTree> Q;
BiTNode *p = bt;
if (bt)
{
Q.push (bt);
while (!Q.empty ())
{
p = Q.front ();    //Queue header element out of queue
cout << p -> data << " ";
if (p -> lchild)
Q.push (p -> lchild);  //Non empty left child in queue
if (p -> rchild)
Q.push (p -> rchild);  //Non empty right child in queue
Q.pop ();
}
}
}

//Tree height
int Height(BiTree bt){
if (bt == NULL)
return 0;
else
{
int m = Height (bt -> lchild);
int n = Height (bt -> rchild);
if(m > n)
return (m + 1);
else
return (n + 1);
}
}

//All nodes in binary tree
int CountTree (BiTree bt){
if (bt == NULL)
return 0;
else
return CountTree (bt -> lchild) + CountTree (bt -> rchild) + 1;
}

//Number of leaf nodes -- traverse. If the node has no left and right children, it is a leaf node -- 2
int CountLeaf (BiTree bt){
if (!bt)
return 0;
if (!bt -> lchild && ! bt -> rchild)
return 1;
else
return CountLeaf (bt -> lchild) + CountLeaf (bt -> rchild);
}

//Number of nodes with judgment degree of 1
int Count_1Node(BiTree bt){
if (!bt)
return 0;
if ((!bt -> lchild) && (bt -> rchild) || (bt -> lchild) && (!bt -> rchild))
return Count_1Node (bt -> lchild) + Count_1Node (bt -> rchild) + 1;
else
return Count_1Node (bt -> lchild) + Count_1Node (bt -> rchild);
}

//Determine whether it is a complete binary tree
bool Is_Comp(BiTree bt)
{
bool res = true;
if (!bt)
return res;
std:: queue <BiTree> Q;
Q.push (bt);
while (!Q.empty())
{
bt = Q.front ();
Q.pop();
if (!bt)
break;
Q.push (bt -> lchild);
Q.push (bt -> rchild);
}
while (!Q.empty ())
{
bt = Q.front ();
Q.pop ();
if (bt)
{
res = false;
break;
}
}
return res;
}

int main(){
BiTNode *bt;
int leaves;
printf("To create a binary tree:");
CreateBiTree (bt);
printf("(recursion  )Preorder traversal:");
PreOrderTraverse1 (bt);
printf("\n(non-recursive )Preorder traversal:");
PreOrderTraverse2 (bt);
printf("\n(recursion  )Middle order traversal:");
InOrderTraverse1 (bt);
printf("\n(non-recursive )Middle order traversal:");
InOrderTraverse2 (bt);
printf("\n(recursion  )Post order traversal:");
PostOrderTraverse1 (bt);
printf("\n(non-recursive )Post order traversal:");
PostOrderTraverse2(bt);
printf("\n Hierarchy traversal:");
LevelOrderTraverse (bt);
printf("\n The height of the tree is:");
cout << Height (bt);
printf("\n The number of all nodes is:");
cout << CountTree (bt);
printf("\n The number of leaf nodes is:");
cout << CountLeaf (bt);
printf("\n(yes:1;no:0)Full binary tree:");
if (Count_1Node (bt) == 0 && CountTree (bt) == pow (2, (float)Height (bt)) - 1)
printf("1");
else
printf("0");
printf("\n(yes:1;no:0)Complete binary tree:");
cout << Is_Comp(bt);

return 0;
}

​```

### Test, result screenshot

If there is any mistake, please correct it, thank you.

I think the writing is OK. Use your little hand to praise and pay attention!

Tags: Algorithm Binary tree

Posted on Wed, 10 Nov 2021 08:45:11 -0500 by tapdancingtenor