Binary tree to achieve sequence traversal

We will implement the sequence traversal
of the tree according to the structure of the binary tree (specifically the binary search tree) in the following figure. For the realization of the binary tree: click here

Insert picture description hereIn order to realize the sequence traversal of the tree, the more common method is to use a queue to achieve.

Let us simulate the process of traversal. Suppose we have a queue queue , the queue status of the initial queue there is nothing in
the queue queue the front and rear pointers to the original location (blue arrow of FIG front and rear common location), as shown below:
Insert picture description here

1. We meet 3 nodes and enqueue the node.
At this time , the situation in the queue is as follows (the blue arrow in the figure indicates the common position of front and rear ):
Insert picture description here

2. Put the left and right child nodes of the third node into the queue. When each subtree node enters the queue, the rear pointer should also move backwards (that is, the position of the blue arrow) as shown below:
Insert picture description here3. The front pointer moves one bit back to red At the arrow
Insert picture description here
4. We need to get the left and right nodes of the node pointed by the current red arrow (that is, node 1 in the above figure) to join the queue (the left subtree of node 1 is empty, so it is not queued, the right subtree node is 2 Join it), and at the same time, move the rear pointer to the position of the blue arrow in the figure below.
Insert picture description hereRepeat the procedure 3 and 4, until the front in the rear behind the pointer, representing complete binary tree traversal.

Understand the principle, the above code implementation is another matter, the main implementation method is divided into recursion and loop.
What I give below is the recursive version.
My assumption is that the queue holds the address of each node, and each time the queue is entered is not the value of the specific node content, but the address of the memory space where the node is located.
If you want to understand in detail, it is recommended to follow the logic of the code step by step to simulate, you can understand why this is done ~, this is also the fastest method.

void LevelTravel(Node* queue[] , Node *t , int front , int rear)
{
    if (front > rear) return;
    queue[front] = t;
    printf("%d ", queue[front]->data);
    if(t->left)
    queue[++rear] = t->left;
    if(t->right)
    queue[++rear] = t->right;
    LevelTravel(queue,queue[front+1], front+1 , rear);
}

The following is the complete code:
There are only four nodes in the following code test, but more node tests have passed without problems.

#include<stdio.h>
#include<malloc.h>
/**/
typedef struct node
{
	int data;
	struct node* left, * right;
}Node;

void insert(Node** t, int data)
{
	if (*t == NULL)
	{
		(*t) = (Node*)malloc(sizeof(Node));
        (*t)->data = data;
        (*t)->left = (*t)->right = NULL;
       
	}
	else if (data >= (*t)->data)
	{
		insert(&((*t)->right), data);
	}
	else insert(&((*t)->left), data);
}
/**前序遍历 根左右**/
void PreOrderTravel(Node* T)
{
    if (T == NULL )
        return;
    printf("%d ", T->data);
    PreOrderTravel(T->left);
    PreOrderTravel(T->right);
}

/**中序遍历 左根右**/
void InOrderTravel(Node* T)
{
    if (T == NULL)
        return;
    InOrderTravel(T->left);
    printf("%d ", T->data);
    InOrderTravel(T->right);
}

/**后序遍历 左右根**/
void TailOrderTravel(Node* T)
{
    if (T == NULL)
        return;
    TailOrderTravel(T->left);
    TailOrderTravel(T->right);
    printf("%d ", T->data);
}

void LevelTravel(Node* queue[] , Node *t , int front , int rear)
{
    if (front > rear) return;
    queue[front] = t;
    printf("%d ", queue[front]->data);
    if(t->left)
    queue[++rear] = t->left;
    if(t->right)
    queue[++rear] = t->right;
    LevelTravel(queue,queue[front+1], front+1 , rear);
}

int main()
{
    Node* t = NULL;
    int i,a[4] = { 3,1,4,2 };
    Node* queue[10];
    for(i = 0; i < 4; i++)
        insert(&t, a[i]);
    PreOrderTravel(t); printf("\n");

    InOrderTravel(t); printf("\n");

    TailOrderTravel(t); printf("\n");
    LevelTravel(queue, t, 0, 0);
}

Run screenshot as follows:
Insert picture description here

Published 7 original articles · won 12 · views 776

Guess you like

Origin blog.csdn.net/M_H_T__/article/details/105624520