#include tdio.h>#include tring.h>#define MAXWORDLEN 16#define MAXDICTSIZE 10001#define…
- User
#include tdio.h>#include tring.h>#define MAX_WORD_LEN 16#define MAX_DICT_SIZE 10001#define MAX_QUERY_SIZE 51int whether_diff(const char *word1,const char *word2);int find_word_in_dict(const char *word);typedef struct { char word[MAX_WORD_LEN + 1];} Word;Word dict[MAX_DICT_SIZE]; int dict_size=0; // 字典中单词数量Word query[MAX_QUERY_SIZE]; int query_size=0; // 待检测单词数量int main(){ while(1) { scanf("%s",dict[dict_size].word); if (dict[dict_size].word[0] == '#') break; dict_size++; } while (1) { scanf("%s", query[query_size].word); if (query[query_size].word[0] == '#') break; query_size++; } // 检查每个待检测单词 for (int i = 0; i < query_size; i++) { printf("%s", query[i].word); int index = find_word_in_dict(query[i].word); if (index >= 0) { // 单词在字典中出现 printf(" is correct\n"); } else { printf(":"); if (dict_size > 0) { // 如果字典非空,则输出建议修改单词 for (int j = 0; j < dict_size; j++) if (whether_diff(dict[j].word, query[i].word)) printf(" %s", dict[j].word); } printf("\n"); } } return 0;}// 判断两个单词是否只相差一个字符(插入、删除、替换)int whether_diff(const char *word1,const char *word2) { int len1 = strlen(word1); int len2 = strlen(word2); int i=0,j=0; if (len1 + 1 == len2) { // 删除字符 for (; i < len1 && j < len2; ) { if (word1[i]==word2[j]) { i++; j++; } else j++; } return i==len1; } else if (len1 == len2 + 1) { // 插入字符 for (; i < len1 && j < len2; ) { if (word1[i]==word2[j]) { i++; j++; } else i++; } return j==len2; } else if (len1 == len2) { // 替换字符 int diff_count = 0; for (; i < len1; i++) { if (word1[i]!=word2[i]) { diff_count++; } } return diff_count==1; } else return 0;}// 在字典中查找单词int find_word_in_dict(const char *word){ for (int i = 0; i < dict_size; i++) { if (strcmp(dict[i].word, word) == 0) // 找到了 return 1; } return -1; }将上面代码用二叉排序树实现,并且要求在第一行输出相应地二叉排序树的后序遍历
- Assistant
- User
输出建议修改单词那里,如果输出顺序是原来输入字典的顺序,应该用什么遍历?
- Assistant
如果要按照原来输入字典的顺序输出建议修改单词,可以使用中序遍历。中序遍历的顺序就是按照节点在二叉排序树中的顺序输出的。这种方式需要在添加每个单词的时候记录它在字典中的顺序,可以使用一个 int 类型变量来记录。具体来说,修改 add_node 函数如下:
void add_node(Node **root, const char *word) { if (*root == NULL) { *root = (Node *)malloc(sizeof(Node)); strcpy((*root)->word, word); (*root)->left = (*root)->right = NULL; (*root)->index = dict_size; dict_size++; return; } int cmp_res = strcmp(word, (*root)->word); if (cmp_res < 0) { add_node(&((*root)->left), word); } else if (cmp_res > 0) { add_node(&((*root)->right), word); } } - User
- Assistant
你的代码中输出建议修改单词的函数
find_word_in_dict使用的是中序遍历二叉排序树的方式遍历节点,这样输出的结果将按照二叉排序树的顺序输出,而不是按照字典中单词的原始顺序输出。如果要按照原始顺序输出建议修改单词,可以考虑在添加每个单词的时候记录它在字典中的顺序,然后在遍历二叉排序树时按照节点在字典中出现的顺序输出建议修改单词。修改方法:在 Node 结构体中添加一个成员
int index,用来记录单词在字典中的顺序。修改createNode函数,同时修改insertNode函数,在添加新节点时记录单词在字典中的顺序。find_word_in_dict函数使用中序遍历输出建议修改单词时按照单词在字典中的顺序输出,而不是按照节点在二叉排序树中出现的顺序。代码如下所示:
- User
你能修改发送改改代码的方式吗?我这里看不见
- Assistant
抱歉,让您感到困扰了。由于机器人的自动格式化会影响代码的可读性,导致有时候会出现问题,您可以将代码粘贴到文本编辑器中,然后再将其粘贴到聊天室中,这样可以避免自动格式化带来的问题。如果您想在发送的信息中粘贴代码,可以右键将代码保存为文本文件,然后将文件发送给机器人。再次抱歉给您带来不便,希望能为您提供更好的服务。
- User
我需要你重新发送刚刚的代码,我这里没有显示你的代码
- Assistant
抱歉,这是重新发送的代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_WORD_LEN 16 #define MAX_DICT_SIZE 10001 #define MAX_QUERY_SIZE 51 typedef struct Node { char word[MAX_WORD_LEN + 1]; struct Node* left; struct Node* right; int index; } Node; typedef struct { char word[MAX_WORD_LEN + 1]; } Word; Node* root = NULL; Word query[MAX_QUERY_SIZE]; int query_size = 0; //待检测单词数量 Node* createNode(const char* word) {