实现二叉树非递归算法的完整代码数据结构c语言版
时间: 2023-06-08 22:08:47 浏览: 127
二叉树非递归遍历-二叉树非递归遍历-二叉树非递归遍历-二叉树非递归遍历
好的,这里是二叉树非递归算法的完整代码,数据结构为C语言版:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
typedef struct StackNode {
TreeNode* node;
struct StackNode* next;
} StackNode;
typedef struct Stack {
StackNode* top;
} Stack;
void initStack(Stack* stack) {
stack->top = NULL;
}
int isEmpty(Stack* stack) {
return stack->top == NULL;
}
void push(Stack* stack, TreeNode* node) {
StackNode* newNode = (StackNode*) malloc(sizeof(StackNode));
newNode->node = node;
newNode->next = stack->top;
stack->top = newNode;
}
TreeNode* pop(Stack* stack) {
if (isEmpty(stack)) {
return NULL;
}
StackNode* topNode = stack->top;
TreeNode* top = topNode->node;
stack->top = topNode->next;
free(topNode);
return top;
}
TreeNode* peek(Stack* stack) {
if (isEmpty(stack)) {
return NULL;
}
return stack->top->node;
}
void destroyStack(Stack* stack) {
while (!isEmpty(stack)) {
pop(stack);
}
}
void preOrderTraversal(TreeNode* root) {
if (!root) {
return;
}
Stack stack;
initStack(&stack);
push(&stack, root);
while (!isEmpty(&stack)) {
TreeNode* node = pop(&stack);
printf("%d ", node->val);
if (node->right) {
push(&stack, node->right);
}
if (node->left) {
push(&stack, node->left);
}
}
destroyStack(&stack);
}
void inOrderTraversal(TreeNode* root) {
if (!root) {
return;
}
Stack stack;
initStack(&stack);
TreeNode* node = root;
while (node || !isEmpty(&stack)) {
while (node) {
push(&stack, node);
node = node->left;
}
node = pop(&stack);
printf("%d ", node->val);
node = node->right;
}
destroyStack(&stack);
}
void postOrderTraversal(TreeNode* root) {
if (!root) {
return;
}
Stack stack;
initStack(&stack);
TreeNode* lastVisited = NULL;
TreeNode* node = root;
while (node || !isEmpty(&stack)) {
if (node) {
push(&stack, node);
node = node->left;
} else {
TreeNode* peekNode = peek(&stack);
if (peekNode->right && lastVisited != peekNode->right) {
node = peekNode->right;
} else {
printf("%d ", peekNode->val);
lastVisited = pop(&stack);
}
}
}
destroyStack(&stack);
}
int main() {
TreeNode* root = (TreeNode*) malloc(sizeof(TreeNode));
root->val = 1;
root->left = (TreeNode*) malloc(sizeof(TreeNode));
root->left->val = 2;
root->right = (TreeNode*) malloc(sizeof(TreeNode));
root->right->val = 3;
root->left->left = (TreeNode*) malloc(sizeof(TreeNode));
root->left->left->val = 4;
root->left->right = (TreeNode*) malloc(sizeof(TreeNode));
root->left->right->val = 5;
root->right->left = (TreeNode*) malloc(sizeof(TreeNode));
root->right->left->val = 6;
root->right->right = (TreeNode*) malloc(sizeof(TreeNode));
root->right->right->val = 7;
printf("pre order traversal: ");
preOrderTraversal(root);
printf("\n");
printf("in order traversal: ");
inOrderTraversal(root);
printf("\n");
printf("post order traversal: ");
postOrderTraversal(root);
printf("\n");
free(root->left->right);
free(root->left->left);
free(root->right->right);
free(root->right->left);
free(root->left);
free(root->right);
free(root);
return 0;
}
```
以上就是二叉树非递归算法的完整代码,包括前序遍历、中序遍历和后序遍历。感谢您的提问,有什么其他问题可以继续问我。
阅读全文