用C语言实现二叉树的非递归遍历,输入二叉树后,输出先序,中序,后序,层次遍历结果
时间: 2023-07-21 20:43:48 浏览: 94
好的,以下是用C语言实现二叉树的非递归遍历的代码,包括先序遍历、中序遍历、后序遍历和层次遍历的结果输出。
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构体
typedef struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
// 定义栈结构体
typedef struct Stack {
int top;
int capacity;
struct TreeNode** array;
} Stack;
// 创建栈
Stack* create_stack(int capacity) {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (TreeNode**)malloc(capacity * sizeof(TreeNode*));
return stack;
}
// 判断栈是否为空
int is_empty(Stack* stack) {
return stack->top == -1;
}
// 判断栈是否已满
int is_full(Stack* stack) {
return stack->top == stack->capacity - 1;
}
// 入栈
void push(Stack* stack, TreeNode* node) {
if (is_full(stack)) {
return;
}
stack->array[++stack->top] = node;
}
// 出栈
TreeNode* pop(Stack* stack) {
if (is_empty(stack)) {
return NULL;
}
return stack->array[stack->top--];
}
// 获取栈顶元素
TreeNode* peek(Stack* stack) {
if (is_empty(stack)) {
return NULL;
}
return stack->array[stack->top];
}
// 创建二叉树
TreeNode* create_tree() {
int val;
scanf("%d", &val);
if (val == -1) {
return NULL;
}
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = val;
root->left = create_tree();
root->right = create_tree();
return root;
}
// 非递归先序遍历
void preorder_traversal(TreeNode* root) {
if (root == NULL) {
return;
}
Stack* stack = create_stack(100);
push(stack, root);
while (!is_empty(stack)) {
TreeNode* node = pop(stack);
printf("%d ", node->val);
if (node->right != NULL) {
push(stack, node->right);
}
if (node->left != NULL) {
push(stack, node->left);
}
}
printf("\n");
}
// 非递归中序遍历
void inorder_traversal(TreeNode* root) {
if (root == NULL) {
return;
}
Stack* stack = create_stack(100);
TreeNode* node = root;
while (node != NULL || !is_empty(stack)) {
while (node != NULL) {
push(stack, node);
node = node->left;
}
node = pop(stack);
printf("%d ", node->val);
node = node->right;
}
printf("\n");
}
// 非递归后序遍历
void postorder_traversal(TreeNode* root) {
if (root == NULL) {
return;
}
Stack* stack1 = create_stack(100);
Stack* stack2 = create_stack(100);
push(stack1, root);
while (!is_empty(stack1)) {
TreeNode* node = pop(stack1);
push(stack2, node);
if (node->left != NULL) {
push(stack1, node->left);
}
if (node->right != NULL) {
push(stack1, node->right);
}
}
while (!is_empty(stack2)) {
TreeNode* node = pop(stack2);
printf("%d ", node->val);
}
printf("\n");
}
// 非递归层次遍历
void level_traversal(TreeNode* root) {
if (root == NULL) {
return;
}
Queue* queue = create_queue(100);
enqueue(queue, root);
while (!is_empty(queue)) {
int size = get_size(queue);
for (int i = 0; i < size; i++) {
TreeNode* node = dequeue(queue);
printf("%d ", node->val);
if (node->left != NULL) {
enqueue(queue, node->left);
}
if (node->right != NULL) {
enqueue(queue, node->right);
}
}
}
printf("\n");
}
int main() {
TreeNode* root = create_tree();
printf("先序遍历结果为:");
preorder_traversal(root);
printf("中序遍历结果为:");
inorder_traversal(root);
printf("后序遍历结果为:");
postorder_traversal(root);
printf("层次遍历结果为:");
level_traversal(root);
return 0;
}
```
输入格式为二叉树的先序遍历结果(-1表示空节点),例如:
```
1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1
```
输出结果为:
```
先序遍历结果为:1 2 4 5 3 6
中序遍历结果为:4 2 5 1 3 6
后序遍历结果为:4 5 2 6 3 1
层次遍历结果为:1 2 3 4 5 6
```
阅读全文