Inorder traversal of binary tree (C language)

We explain in-order traversal of binary trees from two directions ( recursion + iteration )

1. Recursion

Thought:

Traverse from the root node to its left child, always visit the left child of each node, return when it reaches NULL, record the value of each node when returning, and then visit the right child of the node, if it is NULL, return directly The previous layer, if not NULL, repeat the above operation until all nodes are traversed.

code show as below:

void BTreeInOrder(struct TreeNode* root,int* arry,int* returnSize){//中序遍历
    if(NULL==root){//判出条件
        return;
    }
    BTreeInOrder(root->left,arry,returnSize);
    arry[(*returnSize)++]=root->val;
    BTreeInOrder(root->right,arry,returnSize);
}

The specific operation process: (as shown in the figure)

2.  Iteration:

Thought: (Pre-order traversal is to record the node value when pushing into the stack (for details, see the pre-order traversal I wrote before), and in-order traversal is to record the node value when popping the stack)

We know how the recursive process works according to the above, so we can use the stack to imitate this process, so as to use iteration to realize the in-order traversal of the binary tree. The left child is pushed into the stack until the top element of the stack is NULL, and the next loop is entered. First, NULL is popped off the stack, and then the value and address of the top element of the stack are recorded and then popped off the stack, and then the right child of the recorded element is pushed onto the stack, which is NULL will continue the second loop, if not NULL will exit the second loop, continue the first loop until all nodes are visited.

code show as below:

typedef struct TreeNode BTNode;

typedef struct Stack{//C语言中没有栈,所以我自己定义个栈的结构体方便后面使用
    BTNode* a_[100];//存储结点的指针数组
    int size;//数组长度
}Stack;

void StackPush(Stack* b,BTNode* root){//入栈
    b->a_[b->size++]=root;
}

void StackPop(Stack* b){//出栈
    b->size--;
}

int* inorderTraversal(struct TreeNode* root, int* returnSize){//中序遍历
    int* a=(int*)malloc(sizeof(int)*100);//动态创建数组用来存储遍历时的结点数值
    if(NULL==a){
        printf("申请节点失败!\n");
        return NULL;
    }
    Stack b;//创建栈变量
    int i=0;
    BTNode* root_temp;//创建一个指针变量用来记录出栈时的栈顶元素
    b.size=0;//初始化栈
    StackPush(&b,root);//先将根节点入栈
    while(NULL != b.a_[b.size-1]){//第一个循环
        StackPush(&b,b.a_[b.size-1]->left);//将栈顶元素的左孩子入栈,直到栈顶为NULL时进入
//下一个循环
        while(NULL == b.a_[b.size-1]){//第二个循环
            StackPop(&b);//向将NULL出栈
            if(0==b.size){//判断是否访问完所有结点
                (*returnSize)=i;
                return a;
            }
            a[i++]=b.a_[b.size-1]->val;//记录栈顶元素的值
            root_temp=b.a_[b.size-1];//记录栈顶元素的地址
            StackPop(&b);//将栈顶元素出栈
            StackPush(&b,root_temp->right);//将记录的元素的右孩子入栈,继续进行循环
        }
    }
    (*returnSize)=i;
    return a;
}

The specific operation process: (as shown in the figure)

 

Guess you like

Origin blog.csdn.net/weixin_49312527/article/details/122398475