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

C++二叉树zodiac_sign字段搜索函数问题:误输出不存在提示

修复二叉树Zodiac Sign搜索函数的错误提示问题

原代码的核心问题是每遇到一个不匹配的节点就输出一次「元素不存在」,递归遍历整棵树时,哪怕已经找到目标,后续不匹配的节点仍会触发错误提示;就算真的没找到,也会输出N次提示(N为不匹配节点数),完全不符合需求。

以下是两种修复方案:

方案一:用引用变量追踪匹配状态(支持输出所有匹配节点)

拆分出递归辅助函数,通过引用传递的标志变量记录是否找到目标,遍历完成后统一判断是否输出提示:

#include <iostream>
#include <cstring>
using namespace std;

// 补充原代码中未给出的结构体定义
struct Info {
    int id;
    char last_name[20];
    char first_name[20];
    char zodiac_sign[10];
    int date_of_birth[3];
};

struct tree {
    Info inf;
    tree* left;
    tree* right;
};

// 递归辅助函数:遍历树并标记是否找到匹配节点
void findZodiacSignHelper(tree* top, const char* zodiac_sign, bool& found) {
    if (top == nullptr) {
        return;
    }

    int comparisonResult = strcmp(top->inf.zodiac_sign, zodiac_sign);
    if (comparisonResult == 0) {
        // 找到匹配节点,输出信息并更新标志
        printf("| %10d | %7s | %5s | %10s | %d/%d/%d |\n",
               top->inf.id, top->inf.last_name, top->inf.first_name,
               top->inf.zodiac_sign, top->inf.date_of_birth[0],
               top->inf.date_of_birth[1], top->inf.date_of_birth[2]);
        found = true;
    }

    // 继续遍历左右子树(支持多个匹配节点的情况)
    findZodiacSignHelper(top->left, zodiac_sign, found);
    findZodiacSignHelper(top->right, zodiac_sign, found);
}

// 对外调用接口:初始化标志,遍历后统一输出提示
void findZodiacSign(tree* top, const char* zodiac_sign) {
    bool found = false;
    findZodiacSignHelper(top, zodiac_sign, found);

    if (!found) {
        cout << "This element does not exist" << endl;
    }
}

修改说明

  • 移除原代码中else分支的错误提示,避免每个不匹配节点都输出冗余信息
  • 用引用变量found全程追踪是否找到匹配节点
  • 仅在整棵树遍历完成后,根据found的值决定是否输出一次「元素不存在」提示

方案二:让递归函数返回匹配状态(适合快速判断是否存在)

如果只需要确认是否存在匹配节点,或找到第一个匹配项后可提前终止,可让递归函数返回bool值:

#include <iostream>
#include <cstring>
using namespace std;

// 补充结构体定义(同方案一)
struct Info { ... };
struct tree { ... };

bool findZodiacSign(tree* top, const char* zodiac_sign) {
    if (top == nullptr) {
        return false;
    }

    int comparisonResult = strcmp(top->inf.zodiac_sign, zodiac_sign);
    bool foundCurrent = false;

    if (comparisonResult == 0) {
        printf("| %10d | %7s | %5s | %10s | %d/%d/%d |\n",
               top->inf.id, top->inf.last_name, top->inf.first_name,
               top->inf.zodiac_sign, top->inf.date_of_birth[0],
               top->inf.date_of_birth[1], top->inf.date_of_birth[2]);
        foundCurrent = true;
    }

    // 遍历左右子树,只要有一个分支找到就返回true
    bool foundLeft = findZodiacSign(top->left, zodiac_sign);
    bool foundRight = findZodiacSign(top->right, zodiac_sign);

    return foundCurrent || foundLeft || foundRight;
}

// 使用示例
int main() {
    tree* root = ...; // 你的二叉树根节点
    const char* target = "Aries";
    bool found = findZodiacSign(root, target);
    if (!found) {
        cout << "This element does not exist" << endl;
    }
    return 0;
}

修改说明

  • 递归函数返回当前子树是否存在匹配节点
  • 主函数根据返回值,仅输出一次提示
  • 若不需要输出所有匹配项,可在找到当前节点后直接返回true,提前终止遍历

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 11:35:56