# Realization of front, middle and back recursive and non-recursive traversal and layer order traversal of binary tree chain storage

#### 1. Linked storage structure of binary tree

``````#define ElementType char
typedef struct BTNode{

ElementType data;
struct BTNode *left,*right;
}BTNode,*BinTree;
``````

#### 2. Preorder, inorder and postorder recursive traversal of binary tree

Both time complexity and space complexity are O(n)

##### prologue
• No operation if the binary tree is empty;
• if not empty,
• Visit the root node first
• Then traverse the left subtree in preorder
• Traverse the right subtree in preorder
``````void PreOderTraverse(BinTree T){

if(!T) return;
else {

printf("%c\t",T->data);
PreOderTraverse(T->left);
PreOderTraverse(T->right);
}
}
``````
##### In sequence
• No operation if the binary tree is empty;
• if not empty,
• Inorder traversal of the left subtree
• visit the root node
• Inorder traversal of the right subtree
``````void PreOderTraverse(BinTree T){

if(!T) return;
else {

printf("%c\t",T->data);
PreOderTraverse(T->left);
PreOderTraverse(T->right);
}
}
``````
##### Subsequent
• No operation if the binary tree is empty;
• if not empty,
• Postorder traversal of the left subtree
• Postorder traversal of the right subtree
• visit the root node
``````void PreOderTraverse(BinTree T){

if(!T) return;
else {

printf("%c\t",T->data);
PreOderTraverse(T->left);
PreOderTraverse(T->right);
}
}
``````

The input binary tree is as follows:

##### run test
``````int main(void)
{

//创建二叉树
BinTree A = (BinTree)malloc(sizeof(struct BTNode));
A->data = 'A';

BinTree B = (BinTree)malloc(sizeof(struct BTNode));
B->data = 'B';

BinTree C = (BinTree)malloc(sizeof(struct BTNode));
C->data = 'C';

BinTree D = (BinTree)malloc(sizeof(struct BTNode));
D->data = 'D';

BinTree E = (BinTree)malloc(sizeof(struct BTNode));
E->data = 'E';

BinTree F = (BinTree)malloc(sizeof(struct BTNode));
F->data = 'F';

A->left = B;A->right = C;
B->left = D;B->right = E;
C->left = F;C->right = NULL;
D->left = NULL;D->right =NULL;
E->left = NULL;E->right =NULL;
F->left = NULL;F->right =NULL;

//前序遍历
printf("先序遍历：");
PreOderTraverse(A);
printf("\n");
//中序遍历
printf("中序遍历：");
InOderTraverse(A);
printf("\n");
//后序遍历
printf("后序遍历：");
PostOrderTraverse(A);
printf("\n");
//释放内存
free(A);
free(B);
free(C);
free(D);
free(E);
free(F);

return 0;
}
``````

Output result:

#### 3. Non-recursive traversal of a binary tree

In-order non-recursive traversal, using the chain storage of the stack to achieve in-order non-recursive traversal

##### Chained storage structure of the stack storing binary tree nodes, creating an empty stack, judging empty, popping, and pushing
``````/*栈的结点结构*/
typedef struct SNode{

BinTree data;
struct SNode *next;
}*Stack;

/*创建空栈*/
Stack CreateStack(){

//创造头结点S，S不定义任何元素
Stack S = (Stack)malloc(sizeof(struct SNode));
S->next = NULL;
return S;
}

/*判断栈空不空*/
int IsEmpty(Stack S){

return (S->next == NULL);
}

/*入栈*/
void Push(Stack S,BinTree T){

Stack newone = (Stack)malloc(sizeof(struct SNode));
newone->data = T;
newone->next = S->next;
S->next = newone;
}

/*出栈*/
BinTree Pop(Stack S){

if (!S)
{

return NULL;
}else{

Stack tmp;BinTree T;

tmp = S->next;//获取要删除的结点
S->next = tmp->next;//更新栈顶的下一个结点地址
T = tmp->data;//获取被弹出的栈元素的值

free(tmp);//释放弹出的栈顶结点内存
return T;
}
}
``````
##### Preorder non-recursive traversal of a binary tree
``````void InOrderTraverse(BinTree T){

Stack S = CreateStack();//创建有头结点的空栈的指针
BinTree p = T;//临时树结点
BinTree q;
while (p || !IsEmpty(S)) //树不空或者栈空
{

if (p)
{

Push(S,p);//将根结点的指针放入栈中
p = p->left;//查看左子树，遍历左子树
}else{

//如果左子树遍历完，则从栈弹出根结点的值，开始遍历右子树
q = Pop(S);
printf("%c\t",q->data);
p = q->right;
}
}
}
``````
##### run test
``````
//创建二叉树同上
//测试中序非递归遍历二叉树
printf("中序非递归遍历：\n");
InOrderTraverse(A);
``````

The output is as follows:

#### 4. Level order traversal

Hierarchical traversal, realized by using the sequential storage structure of the queue

##### The sequential storage structure of the queue storing the binary tree nodes, creating an empty queue, judging empty, entering the queue, and exiting the queue
``````/*定义队列的顺序存储结构*/
typedef struct QNode
{

BinTree DataArray[MaxSize];//存放二叉树结点地址的数组
int front,rear;//队头和队尾下标
/*当front = rear时，链表为空*/
}*Queue;

/*创建空队列*/
Queue CreateQ(){

Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->front = Q->rear = -1;
return Q;
}

/*入队,尾加*/

if ((Q->rear+1)%MaxSize == Q->front) return; /* 判断队列是否满 */
Q->rear = (Q->rear+1)%MaxSize;
Q->DataArray[Q->rear] = bt;
}

/* 出队，头删 */
BinTree DeQ(Queue Q){

if (Q->front == Q->rear) //判断队列是不是空
{

printf("\nit is null.\n");
return NULL;
}else{

Q->front = (Q->front+1)%MaxSize;
BinTree bt = Q->DataArray[Q->front];
return bt;
}
}

/* 判断队列是否为空 */
int IsEmpty(Queue Q){

return (Q->front == Q->rear);
}
``````
##### level traversal of binary tree
``````void LevelTraversal(BinTree T){

if(!T) return; //空二叉树
//创建空队列
Queue Q = CreateQ();
BinTree p;
//入队
while (!IsEmpty(Q)) //队不为空则循环
{

//出队
p = DeQ(Q);
printf("%c\t",p->data);
}
}
``````
##### run test
``````//测试层次非递归遍历二叉树
printf("层次非递归遍历：\n");
LevelTraversal(A);
``````

The result is shown in the figure

### Guess you like

Origin blog.csdn.net/weixin_44848852/article/details/119388129
Recommended
Ranking
Daily