C语言二叉排序树通讯录
时间: 2023-07-16 08:15:27 浏览: 49
好的,下面是一个用 C 语言实现的二叉排序树通讯录的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义通讯录结构体,包含姓名和电话号码
typedef struct Contact {
char name[20];
char phone[12];
} Contact;
// 定义二叉排序树结点
typedef struct TreeNode {
Contact contact;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 插入通讯录结点到二叉排序树中
TreeNode *insertNode(TreeNode *root, Contact contact) {
if (root == NULL) {
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
newNode->contact = contact;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 根据姓名判断插入到左子树还是右子树
if (strcmp(contact.name, root->contact.name) < 0) {
root->left = insertNode(root->left, contact);
} else {
root->right = insertNode(root->right, contact);
}
return root;
}
// 查找通讯录结点
TreeNode *searchNode(TreeNode *root, char *name) {
if (root == NULL) {
return NULL;
}
// 根据姓名比较查找
if (strcmp(name, root->contact.name) == 0) {
return root;
} else if (strcmp(name, root->contact.name) < 0) {
return searchNode(root->left, name);
} else {
return searchNode(root->right, name);
}
}
// 删除通讯录结点
TreeNode *deleteNode(TreeNode *root, char *name) {
if (root == NULL) {
return NULL;
}
// 根据姓名比较查找
if (strcmp(name, root->contact.name) == 0) {
// 如果该结点没有子结点,直接删除
if (root->left == NULL && root->right == NULL) {
free(root);
return NULL;
}
// 如果该结点只有左子树或只有右子树,直接将子结点替换掉该结点
if (root->left == NULL || root->right == NULL) {
TreeNode *child = root->left == NULL ? root->right : root->left;
free(root);
return child;
}
// 如果该结点有两个子结点,将其右子树中的最小值结点替换掉该结点
TreeNode *minNode = root->right;
while (minNode->left != NULL) {
minNode = minNode->left;
}
root->contact = minNode->contact;
root->right = deleteNode(root->right, minNode->contact.name);
} else if (strcmp(name, root->contact.name) < 0) {
root->left = deleteNode(root->left, name);
} else {
root->right = deleteNode(root->right, name);
}
return root;
}
// 遍历二叉排序树,按照中序遍历的顺序输出通讯录
void printTree(TreeNode *root) {
if (root == NULL) {
return;
}
printTree(root->left);
printf("%s %s\n", root->contact.name, root->contact.phone);
printTree(root->right);
}
int main() {
int choice;
char name[20];
Contact contact;
TreeNode *root = NULL;
while (1) {
printf("请选择操作:\n");
printf("1. 添加联系人\n");
printf("2. 查找联系人\n");
printf("3. 删除联系人\n");
printf("4. 显示通讯录\n");
printf("5. 退出程序\n");
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入联系人姓名:");
scanf("%s", contact.name);
printf("请输入联系人电话号码:");
scanf("%s", contact.phone);
root = insertNode(root, contact);
printf("联系人已添加到通讯录中!\n");
break;
case 2:
printf("请输入联系人姓名:");
scanf("%s", name);
TreeNode *node = searchNode(root, name);
if (node == NULL) {
printf("未找到该联系人!\n");
} else {
printf("%s %s\n", node->contact.name, node->contact.phone);
}
break;
case 3:
printf("请输入联系人姓名:");
scanf("%s", name);
root = deleteNode(root, name);
printf("联系人已从通讯录中删除!\n");
break;
case 4:
printf("通讯录如下:\n");
printTree(root);
break;
case 5:
printf("程序已退出!\n");
exit(0);
default:
printf("输入错误,请重新输入!\n");
break;
}
}
return 0;
}
```
这个代码实现了四个基本操作:插入联系人、查找联系人、删除联系人、显示通讯录。其中,通讯录使用了二叉排序树来存储,按照联系人的姓名进行排序。你可以根据自己的需要进行修改和扩展。