使用C语音写出 首先,从记事本中依次读取每一行通讯录内容,将读取的所有教师信息按照年龄构建成二叉排序树,如图8-2所示。然后,分别通过对二叉排序树的先序遍历显示所有教师信息,通过二叉排序树的后序遍历按照姓名查询某位教师通讯信息,通过二叉排序树后序遍历按照部门查询该部门所有教师的通讯信息;最后,通过二叉排序树的中序遍历输出青年教师(≤35岁)通讯信息,遍历过程中,如果某个结点的年龄大于35岁,那么不再需要遍历该结点的右子树。
时间: 2024-02-21 08:57:11 浏览: 62
下面是使用C语言实现题目要求的代码,其中假设通讯录文件名为"contacts.txt",每一行的格式为"姓名 年龄 部门 电话号码",各字段之间用空格隔开:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_DEPT_LEN 20
#define MAX_PHONE_LEN 20
typedef struct teacher {
char name[MAX_NAME_LEN];
int age;
char dept[MAX_DEPT_LEN];
char phone[MAX_PHONE_LEN];
} Teacher;
typedef struct bst_node {
Teacher data;
struct bst_node *left;
struct bst_node *right;
} BSTNode;
void insert_bst_node(BSTNode **root, Teacher data);
void preorder_traversal(BSTNode *root);
BSTNode *search_bst_node(BSTNode *root, char *name);
void postorder_traversal_by_dept(BSTNode *root, char *dept);
void inorder_traversal_young_teachers(BSTNode *root);
int main() {
FILE *fp;
char line[100];
BSTNode *root = NULL;
fp = fopen("contacts.txt", "r");
if (fp == NULL) {
perror("Error opening contacts file");
exit(EXIT_FAILURE);
}
while (fgets(line, sizeof(line), fp) != NULL) {
Teacher t;
sscanf(line, "%s %d %s %s", t.name, &t.age, t.dept, t.phone);
insert_bst_node(&root, t);
}
printf("Preorder traversal:\n");
preorder_traversal(root);
char name[MAX_NAME_LEN];
printf("Enter a name to search:\n");
scanf("%s", name);
BSTNode *node = search_bst_node(root, name);
if (node != NULL) {
printf("Found:\n%s %d %s %s\n", node->data.name, node->data.age, node->data.dept, node->data.phone);
} else {
printf("Not found\n");
}
char dept[MAX_DEPT_LEN];
printf("Enter a department to search:\n");
scanf("%s", dept);
postorder_traversal_by_dept(root, dept);
printf("Young teachers:\n");
inorder_traversal_young_teachers(root);
fclose(fp);
return 0;
}
void insert_bst_node(BSTNode **root, Teacher data) {
if (*root == NULL) {
*root = (BSTNode *)malloc(sizeof(BSTNode));
(*root)->data = data;
(*root)->left = NULL;
(*root)->right = NULL;
} else if (data.age < (*root)->data.age) {
insert_bst_node(&((*root)->left), data);
} else {
insert_bst_node(&((*root)->right), data);
}
}
void preorder_traversal(BSTNode *root) {
if (root != NULL) {
printf("%s %d %s %s\n", root->data.name, root->data.age, root->data.dept, root->data.phone);
preorder_traversal(root->left);
preorder_traversal(root->right);
}
}
BSTNode *search_bst_node(BSTNode *root, char *name) {
if (root == NULL) {
return NULL;
} else if (strcmp(name, root->data.name) == 0) {
return root;
} else if (strcmp(name, root->data.name) < 0) {
return search_bst_node(root->left, name);
} else {
return search_bst_node(root->right, name);
}
}
void postorder_traversal_by_dept(BSTNode *root, char *dept) {
if (root != NULL) {
postorder_traversal_by_dept(root->left, dept);
postorder_traversal_by_dept(root->right, dept);
if (strcmp(dept, root->data.dept) == 0) {
printf("%s %d %s %s\n", root->data.name, root->data.age, root->data.dept, root->data.phone);
}
}
}
void inorder_traversal_young_teachers(BSTNode *root) {
if (root != NULL) {
inorder_traversal_young_teachers(root->left);
if (root->data.age <= 35) {
printf("%s %d %s %s\n", root->data.name, root->data.age, root->data.dept, root->data.phone);
inorder_traversal_young_teachers(root->right);
}
}
}
```
注:本代码仅作参考,可能存在不足之处,欢迎指正。
阅读全文