用c语言编写根据二叉树中序遍历的非递归算法,在二叉树t查找值为x的元素,若找到且其左子树为空,则将值为y的元素插入成为其左孩子,否则若其右孩子为空,则将y插入成为其右孩子。插入失败,返回值为0,否则返回值为1。
时间: 2024-03-14 19:45:53 浏览: 55
自己编写的实验二叉树的后序遍历非递归算法c语言实现
5星 · 资源好评率100%
好的,根据您的要求,我来为您提供 C 语言的代码实现:
```
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结构体
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 定义栈结构体
typedef struct Stack {
TreeNode **data;
int top;
int maxSize;
} Stack;
// 初始化栈
Stack *initStack(int maxSize) {
Stack *stack = (Stack*)malloc(sizeof(Stack));
stack->data = (TreeNode**)malloc(sizeof(TreeNode*) * maxSize);
stack->top = -1;
stack->maxSize = maxSize;
return stack;
}
// 判断栈是否为空
int isEmpty(Stack *stack) {
return stack->top == -1;
}
// 入栈
void push(Stack *stack, TreeNode *node) {
if (stack->top == stack->maxSize - 1) {
return;
}
stack->data[++stack->top] = node;
}
// 出栈
TreeNode *pop(Stack *stack) {
if (isEmpty(stack)) {
return NULL;
}
return stack->data[stack->top--];
}
// 获取栈顶元素
TreeNode *top(Stack *stack) {
if (isEmpty(stack)) {
return NULL;
}
return stack->data[stack->top];
}
// 遍历二叉树的中序遍历
void inorder(TreeNode *root) {
Stack *stack = initStack(100);
TreeNode *p = root;
while (p || !isEmpty(stack)) {
while (p) {
push(stack, p);
p = p->left;
}
if (!isEmpty(stack)) {
p = pop(stack);
printf("%d ", p->val);
p = p->right;
}
}
}
// 在二叉树中查找节点
TreeNode *findNode(TreeNode *root, int x) {
Stack *stack = initStack(100);
TreeNode *p = root;
while (p || !isEmpty(stack)) {
while (p) {
push(stack, p);
p = p->left;
}
if (!isEmpty(stack)) {
p = pop(stack);
if (p->val == x) {
return p;
}
p = p->right;
}
}
return NULL;
}
// 插入节点
int insertNode(TreeNode *root, int x, int y) {
TreeNode *node = findNode(root, x);
if (!node) {
return 0;
}
if (!node->left) {
TreeNode *newNode = (TreeNode*)malloc(sizeof(TreeNode));
newNode->val = y;
newNode->left = NULL;
newNode->right = NULL;
node->left = newNode;
return 1;
} else if (!node->right) {
TreeNode *newNode = (TreeNode*)malloc(sizeof(TreeNode));
newNode->val = y;
newNode->left = NULL;
newNode->right = NULL;
node->right = newNode;
return 1;
}
return 0;
}
int main() {
// 构造二叉树
TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = 5;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = 3;
root->left->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->left->val = 1;
root->left->left->left = NULL;
root->left->left->right = NULL;
root->left->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->val = 4;
root->left->right->left = NULL;
root->left->right->right = NULL;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = 7;
root->right->left = (TreeNode*)malloc(sizeof(TreeNode));
root->right->left->val = 6;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->right->val = 8;
root->right->right->left = NULL;
root->right->right->right = NULL;
// 遍历二叉树的中序遍历
inorder(root);
printf("\n");
// 在二叉树中查找节点
TreeNode *node = findNode(root, 4);
if (node) {
printf("Find node with value 4.\n");
} else {
printf("Not find node with value 4.\n");
}
// 插入节点
int res = insertNode(root, 4, 9);
if (res) {
printf("Insert node with value 9 successfully.\n");
} else {
printf("Insert node with value 9 failed.\n");
}
// 遍历二叉树的中序遍历
inorder(root);
printf("\n");
return 0;
}
```
以上代码实现了根据二叉树中序遍历的非递归算法,在二叉树中查找值为 x 的元素,若找到且其左子树为空,则将值为 y 的元素插入成为其左孩子,否则若其右孩子为空,则将 y 插入成为其右孩子。若插入失败,则返回值为 0,否则返回值为 1。
阅读全文