Data_Structure/作業/unit7/try.cpp

204 lines
4.8 KiB
C++
Raw Normal View History

2025-01-20 21:30:53 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
/* <20>w<EFBFBD>q<EFBFBD>޽u<DEBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD>𵲺c */
typedef struct student {
char name[20]; /* <20>ǥͩm<CDA9>W */
int score; /* <20>ǥͦ<C7A5><CDA6>Z */
struct student *left; /* <20><><EFBFBD>l<EFBFBD><6C><EFBFBD><EFBFBD> */
struct student *right; /* <20>k<EFBFBD>l<EFBFBD><6C><EFBFBD><EFBFBD> */
int leftThread; /* <20><><EFBFBD>u<EFBFBD><75><EFBFBD>аO */
int rightThread; /* <20>k<EFBFBD>u<EFBFBD><75><EFBFBD>аO */
} student;
/* <20><><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD> */
student *root = NULL;
student *head = NULL;
/* <20><><EFBFBD>ƭ쫬 */
void insert(char [], int);
void remove_node(char []);
void show_tree();
student *search(char []);
student *find_inorder_successor(student *);
student *find_inorder_predecessor(student *);
void free_tree(student *);
/* <20><><EFBFBD>M<EFBFBD><4D><EFBFBD>ǫ<EFBFBD><C7AB>~<7E>`<60>I */
student *find_inorder_successor(student *node) {
if (node->rightThread)
return node->right;
node = node->right;
while (!node->leftThread)
node = node->left;
return node;
}
/* <20><><EFBFBD>M<EFBFBD><4D><EFBFBD>ǫe<C7AB>X<EFBFBD>`<60>I */
student *find_inorder_predecessor(student *node) {
if (node->leftThread)
return node->left;
node = node->left;
while (!node->rightThread)
node = node->right;
return node;
}
/* <20><><EFBFBD>J<EFBFBD>`<60>I */
void insert(char name[], int score) {
student *new_node, *current, *parent;
/* <20>ˬd<CBAC>`<60>I<EFBFBD>O<EFBFBD>_<EFBFBD>w<EFBFBD>s<EFBFBD>b */
if (search(name) != NULL) {
printf("%s <20>w<EFBFBD>s<EFBFBD>b!\n", name);
return;
}
new_node = (student *)malloc(sizeof(student));
strcpy(new_node->name, name);
new_node->score = score;
new_node->leftThread = new_node->rightThread = 1;
if (root == NULL) {
/* <20>إߪ<D8A5><DFAA>Y<EFBFBD>`<60>I */
head = (student *)malloc(sizeof(student));
head->leftThread = head->rightThread = 0;
head->right = head;
head->left = new_node;
/* <20>Ĥ@<40>Ӹ`<60>I */
new_node->left = head;
new_node->right = head;
root = new_node;
return;
}
current = root;
while (1) {
parent = current;
if (strcmp(name, current->name) < 0) {
if (current->leftThread)
break;
current = current->left;
} else {
if (current->rightThread)
break;
current = current->right;
}
}
if (strcmp(name, parent->name) < 0) {
/* <20><><EFBFBD>l<EFBFBD>`<60>I */
new_node->left = parent->left;
new_node->right = parent;
parent->left = new_node;
parent->leftThread = 0;
} else {
/* <20>k<EFBFBD>l<EFBFBD>`<60>I */
new_node->right = parent->right;
new_node->left = parent;
parent->right = new_node;
parent->rightThread = 0;
}
}
/* <20><><EFBFBD>ǹM<C7B9><4D> */
void show_tree() {
student *current;
if (root == NULL) {
printf("<EFBFBD><EFBFBD><EFBFBD>O<EFBFBD>Ū<EFBFBD>!\n");
return;
}
current = root;
while (!current->leftThread)
current = current->left;
printf("<EFBFBD>r<EFBFBD><EFBFBD><EFBFBD>ܼƤ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>x<EFBFBD>s<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(key<65>Gvalue)<29>p<EFBFBD>U\n");
while (current != head) {
printf("%s<>G%d\n", current->name, current->score);
current = find_inorder_successor(current);
}
}
/* <20>j<EFBFBD>M<EFBFBD>`<60>I */
student *search(char target[]) {
student *current = root;
while (current != NULL) {
if (strcmp(target, current->name) == 0)
return current;
if (strcmp(target, current->name) < 0) {
if (current->leftThread)
break;
current = current->left;
} else {
if (current->rightThread)
break;
current = current->right;
}
}
return NULL;
}
/* <20>R<EFBFBD><52><EFBFBD>`<60>I */
void remove_node(char name[]) {
student *node = search(name);
if (node == NULL) {
printf("%s <20><><EFBFBD>b<EFBFBD><62><EFBFBD><EFBFBD>!\n", name);
return;
}
// <20>R<EFBFBD><52><EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>o<EFBFBD>̷|<7C><><EFBFBD><EFBFBD><EFBFBD>h<EFBFBD>Ӹ`<60>ݭn<DDAD>B<EFBFBD>z
printf("<EFBFBD>R<EFBFBD><EFBFBD><EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>@<40>b<EFBFBD>޽u<DEBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD>𤤸<EFBFBD><F0A4A4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>ݭn<DDAD>S<EFBFBD>O<EFBFBD>B<EFBFBD>z<EFBFBD>u<EFBFBD><75><EFBFBD>C\n");
}
/* <20><><EFBFBD><EFBFBD><EFBFBD>𪺰O<F0AABAB0><4F><EFBFBD><EFBFBD> */
void free_tree(student *node) {
if (node == NULL) return;
if (!node->leftThread)
free_tree(node->left);
if (!node->rightThread)
free_tree(node->right);
printf("<EFBFBD>R<EFBFBD><EFBFBD>'w[%s]=%d'<27>`<60>I\n", node->name, node->score);
free(node);
}
int main() {
char line[128], key[20];
int value;
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>s<EFBFBD><EFBFBD><EFBFBD>r<EFBFBD><EFBFBD><EFBFBD><EFBFBD>z<EFBFBD>A<EFBFBD><EFBFBD><Enter><3E><><EFBFBD><EFBFBD><EFBFBD>{<7B><>\n");
while (1) {
printf("\n<EFBFBD><EFBFBD><EFBFBD>O => ");
fgets(line, 128, stdin);
if (line[0] == '\n') break;
if (sscanf(line, "w[%[^]]] = %d", key, &value) == 2) {
insert(key, value);
printf("<EFBFBD>s<EFBFBD>Ww[%s]=%d\n", key, value);
} else if (sscanf(line, "w[%[^]]]?", key) == 1) {
student *found = search(key);
if (found)
printf("<EFBFBD>r<EFBFBD><EFBFBD><EFBFBD>w[%s]=%d\n", key, found->score);
else
printf("<EFBFBD><EFBFBD><EFBFBD>~! %s <20><><EFBFBD>b<EFBFBD><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!\n", key);
} else if (line[0] == 'w' && line[1] == '?') {
show_tree();
}
}
free_tree(root);
free(head);
return 0;
}