使用C语言编程有菜单的实现读入一串字符创建一棵二叉排序树,然后分别实现:二叉排序树的查找;二叉排序树的插入;二叉排序树的删除等操作
时间: 2023-08-21 08:10:57 浏览: 70
二叉排序树实现教师成果管理系统源码
以下是一个简单的实现,包含了菜单、创建二叉排序树、查找、插入和删除操作。需要注意的是,这个实现并没有考虑内存管理和错误处理,仅供参考。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
void printMenu() {
printf("1. 创建二叉排序树\n");
printf("2. 查找元素\n");
printf("3. 插入元素\n");
printf("4. 删除元素\n");
printf("5. 退出\n");
}
TreeNode* createBST() {
TreeNode *root = NULL;
char input[100];
printf("请输入一串数字(以空格分隔):");
fgets(input, 100, stdin);
char *p = input;
int val;
while (sscanf(p, "%d", &val) != EOF) {
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
if (root == NULL) {
root = node;
} else {
TreeNode *parent = NULL;
TreeNode *cur = root;
while (cur != NULL) {
parent = cur;
if (val < cur->val) {
cur = cur->left;
} else {
cur = cur->right;
}
}
if (val < parent->val) {
parent->left = node;
} else {
parent->right = node;
}
}
p++;
}
return root;
}
TreeNode* find(TreeNode *root, int val) {
TreeNode *cur = root;
while (cur != NULL) {
if (cur->val == val) {
return cur;
} else if (val < cur->val) {
cur = cur->left;
} else {
cur = cur->right;
}
}
return NULL;
}
void insert(TreeNode **root, int val) {
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
if (*root == NULL) {
*root = node;
} else {
TreeNode *parent = NULL;
TreeNode *cur = *root;
while (cur != NULL) {
parent = cur;
if (val < cur->val) {
cur = cur->left;
} else {
cur = cur->right;
}
}
if (val < parent->val) {
parent->left = node;
} else {
parent->right = node;
}
}
}
void delete(TreeNode **root, int val) {
TreeNode *parent = NULL;
TreeNode *cur = *root;
while (cur != NULL && cur->val != val) {
parent = cur;
if (val < cur->val) {
cur = cur->left;
} else {
cur = cur->right;
}
}
if (cur == NULL) {
printf("未找到该元素\n");
return;
}
if (cur->left == NULL && cur->right == NULL) {
if (cur == *root) {
*root = NULL;
} else if (cur == parent->left) {
parent->left = NULL;
} else {
parent->right = NULL;
}
free(cur);
} else if (cur->left == NULL) {
if (cur == *root) {
*root = cur->right;
} else if (cur == parent->left) {
parent->left = cur->right;
} else {
parent->right = cur->right;
}
free(cur);
} else if (cur->right == NULL) {
if (cur == *root) {
*root = cur->left;
} else if (cur == parent->left) {
parent->left = cur->left;
} else {
parent->right = cur->left;
}
free(cur);
} else {
TreeNode *successorParent = cur;
TreeNode *successor = cur->right;
while (successor->left != NULL) {
successorParent = successor;
successor = successor->left;
}
cur->val = successor->val;
if (successor == successorParent->left) {
successorParent->left = successor->right;
} else {
successorParent->right = successor->right;
}
free(successor);
}
}
void printTree(TreeNode *root) {
if (root != NULL) {
printTree(root->left);
printf("%d ", root->val);
printTree(root->right);
}
}
int main() {
TreeNode *root = NULL;
int choice, val;
while (1) {
printMenu();
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
root = createBST();
break;
case 2:
printf("请输入要查找的元素:");
scanf("%d", &val);
if (find(root, val) != NULL) {
printf("找到了该元素\n");
} else {
printf("未找到该元素\n");
}
break;
case 3:
printf("请输入要插入的元素:");
scanf("%d", &val);
insert(&root, val);
break;
case 4:
printf("请输入要删除的元素:");
scanf("%d", &val);
delete(&root, val);
break;
case 5:
return 0;
}
printf("当前二叉排序树:");
printTree(root);
printf("\n");
}
}
```
阅读全文