You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++从文本文件导单词入BST实现拼写检查技术求助

解决你的BST拼写检查程序问题

看起来你已经搞定了文件读取的部分,但在把单词导入二叉搜索树(BST)的环节踩了几个关键坑。我会帮你梳理现有代码的问题,再给你一个能正常运行的修正版本。

现有代码的核心问题

  • 变量作用域不对:main里的数组a是局部变量,create和insert函数根本访问不到,自然没法把读取到的单词传入BST。
  • BST插入逻辑搞反了:strcmp的比较方向错了——BST的规则是:新单词比当前节点小(strcmp(new_word, current_word) < 0)就插左子树,大的插右子树,你的代码刚好写反,会导致树的结构完全混乱。
  • create函数完全无效:这个函数没用到你从文件读的内容,反而凭空创建了一个空节点,起不到初始化BST的作用。
  • 未定义变量满天飞:比如create里的i、a,insert里的a[i+1]都没声明或传递,编译直接报错。
  • 文件读取的小bug:用!inFile.eof()当循环条件会导致最后一个单词被读两次,换成while(inFile >> x)才是正确的姿势。

修正后的完整代码

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;

// 修正结构体定义,补上末尾的分号
struct node {
    struct node *left;
    struct node *right;
    char word[100];
};

struct node *root = NULL;

// 通用的BST插入函数,接受要插入的单词
void insertNode(const char* word) {
    struct node *newnode = (struct node *)malloc(sizeof(struct node));
    strcpy(newnode->word, word);
    newnode->left = NULL;
    newnode->right = NULL;

    if (root == NULL) {
        // 树为空时,直接把新节点作为根
        root = newnode;
        return;
    }

    struct node *temp = root;
    struct node *parent = NULL;

    // 遍历找到合适的插入位置
    while (temp != NULL) {
        parent = temp;
        int cmpResult = strcmp(word, temp->word);
        if (cmpResult < 0) {
            // 新单词更小,往左子树走
            temp = temp->left;
        } else {
            // 新单词更大或相等,往右子树走
            temp = temp->right;
        }
    }

    // 把新节点插入到父节点的左/右子树
    int cmpResult = strcmp(word, parent->word);
    if (cmpResult < 0) {
        parent->left = newnode;
    } else {
        parent->right = newnode;
    }
}

// 拼写检查函数:查找单词是否在BST中
bool checkSpelling(const char* word) {
    struct node *temp = root;
    while (temp != NULL) {
        int cmpResult = strcmp(word, temp->word);
        if (cmpResult == 0) {
            // 找到匹配的单词
            return true;
        } else if (cmpResult < 0) {
            temp = temp->left;
        } else {
            temp = temp->right;
        }
    }
    // 遍历完没找到,返回false
    return false;
}

int main() {
    ifstream inFile;
    inFile.open("test.txt");
    if (inFile.fail()) {
        cerr << "Error opening file" << endl;
        exit(1);
    }

    string x;
    // 读取文件里的每个单词,逐个插入BST
    while (inFile >> x) {
        insertNode(x.c_str());
    }
    inFile.close();

    // 测试拼写检查功能
    string inputWord;
    cout << "Enter a word to check: ";
    cin >> inputWord;

    if (checkSpelling(inputWord.c_str())) {
        cout << "Correct" << endl;
    } else {
        cout << "Incorrect" << endl;
    }

    return 0;
}

关键修改说明

  1. 通用插入函数:insertNode接受C风格字符串,不管是从文件读的还是手动输入的单词,都能直接调用这个函数插入BST。
  2. 修复BST插入逻辑:通过strcmp(word, temp->word)的结果判断方向,确保小单词左插、大单词右插,符合BST的核心规则。
  3. 移除无效的create函数:直接在insertNode里处理根节点为空的情况,不需要单独的初始化步骤。
  4. 正确读取文件并插入:用while(inFile >> x)读取每个单词,再转成C风格字符串传入BST(因为结构体里用的是char[])。
  5. 添加拼写检查函数:checkSpelling遍历BST查找目标单词,返回布尔值,最后根据结果输出对应的提示。

使用提示

  • 确保你的test.txt文件每行一个单词(哪怕不按字母顺序排列也能正常工作,只是排序后的插入会让树更平衡,查找效率更高)。
  • 编译运行后,输入任意单词,程序就会返回拼写是否正确。

内容的提问来源于stack exchange,提问作者mohinish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:23:28