C语言非递归二叉树遍历用栈实现
时间: 2023-07-07 19:11:55 浏览: 108
二叉树的非递归遍历一般使用栈来实现。以下是 C 语言实现二叉树非递归遍历的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
// 定义栈结构
struct Stack {
struct TreeNode *data[MAX_SIZE];
int top;
};
// 初始化栈
void initStack(struct Stack *s) {
s->top = -1;
}
// 判断栈是否为空
int isEmpty(struct Stack *s) {
return s->top == -1;
}
// 判断栈是否已满
int isFull(struct Stack *s) {
return s->top == MAX_SIZE - 1;
}
// 入栈
void push(struct Stack *s, struct TreeNode *node) {
if (isFull(s)) {
printf("Stack is full.\n");
return;
}
s->top++;
s->data[s->top] = node;
}
// 出栈
struct TreeNode *pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return NULL;
}
struct TreeNode *node = s->data[s->top];
s->top--;
return node;
}
// 二叉树非递归前序遍历
void preorderTraversal(struct TreeNode *root) {
if (root == NULL) {
return;
}
struct Stack s;
initStack(&s);
push(&s, root);
while (!isEmpty(&s)) {
struct TreeNode *node = pop(&s);
printf("%d ", node->val);
if (node->right != NULL) {
push(&s, node->right);
}
if (node->left != NULL) {
push(&s, node->left);
}
}
}
// 二叉树非递归中序遍历
void inorderTraversal(struct TreeNode *root) {
if (root == NULL) {
return;
}
struct Stack s;
initStack(&s);
struct TreeNode *node = root;
while (node != NULL || !isEmpty(&s)) {
while (node != NULL) {
push(&s, node);
node = node->left;
}
node = pop(&s);
printf("%d ", node->val);
node = node->right;
}
}
// 二叉树非递归后序遍历
void postorderTraversal(struct TreeNode *root) {
if (root == NULL) {
return;
}
struct Stack s;
initStack(&s);
struct TreeNode *node = root;
struct TreeNode *lastVisit = NULL;
while (node != NULL || !isEmpty(&s)) {
while (node != NULL) {
push(&s, node);
node = node->left;
}
node = s.data[s.top];
if (node->right == NULL || node->right == lastVisit) {
printf("%d ", node->val);
lastVisit = node;
node = NULL;
pop(&s);
} else {
node = node->right;
}
}
}
int main() {
struct TreeNode *root = (struct TreeNode *) malloc(sizeof(struct TreeNode));
struct TreeNode *left = (struct TreeNode *) malloc(sizeof(struct TreeNode));
struct TreeNode *right = (struct TreeNode *) malloc(sizeof(struct TreeNode));
root->val = 1;
root->left = left;
root->right = right;
left->val = 2;
left->left = NULL;
left->right = NULL;
right->val = 3;
right->left = NULL;
right->right = NULL;
printf("前序遍历结果:");
preorderTraversal(root);
printf("\n中序遍历结果:");
inorderTraversal(root);
printf("\n后序遍历结果:");
postorderTraversal(root);
return 0;
}
```
这里实现了二叉树的非递归前序、中序和后序遍历。需要注意的是,后序遍历的实现比较复杂,需要设置一个 lastVisit 指针来记录上一个访问的结点,以保证不重复访问。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)