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

请求协助完成基于指定关键字构建C++二叉搜索树的作业

二叉搜索树实现正确性分析

我被布置了一项作业,要求使用try、throw、catch、cin、cout、static、friend这些关键字构建一棵二叉搜索树(Binary Search Tree)。我是C++新手,查了很多资料还是做不出来,以下是我写的代码,请问这样实现是否正确?

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

class BST {
    
    struct node {
        string data;
        node* left;
        node* right;
    };

    node* root;

    node* insert(string x, node* t)
    {
        if(t == NULL)
        {
            t = new node;
            t->data = x;
            t->left = t->right = NULL;
        }
        else if(x < t->data)
            t->left = insert(x, t->left);
        else if(x > t->data)
            t->right = insert(x, t->right);
        return t;
    }

    
    void inorder(node* t) {
        if(t == NULL)
            return;
        inorder(t->left);
        cout << t->data << " ";
        inorder(t->right);
    }

public:
    BST() {
        root = NULL;
    }

    void insert(string x) {
        root = insert(x, root);
    }

    void display() {
        inorder(root);
        cout << endl;
    }
};

int main() {
    BST t;
    t.insert("Try");
    t.insert("throw");
    t.insert("catch");
    t.insert("cin");
    t.insert("cout");
    t.insert("static");
    t.insert("friend");
    t.display();
    return 0; 
}

代码运行输出:

Try catch cin cout friend static throw


基础逻辑正确,但未满足作业全部要求

你的代码实现了二叉搜索树的核心插入逻辑和中序遍历,字符串比较遵循C++字典序,中序遍历输出升序结果,这部分是正确的。但没有用到作业要求的try、throw、catch、static、friend关键字,不符合作业的完整要求。

补充关键字的修改建议

1. 加入try/throw/catch:处理重复插入异常

当插入已存在的元素时抛出异常,在main函数中捕获:

// 在BST类的insert私有方法中添加重复判断
node* insert(string x, node* t)
{
    if(t == NULL)
    {
        t = new node;
        t->data = x;
        t->left = t->right = NULL;
    }
    else if(x < t->data)
        t->left = insert(x, t->left);
    else if(x > t->data)
        t->right = insert(x, t->right);
    else
        throw runtime_error("元素已存在"); // 抛出异常
    return t;
}

// main函数中用try-catch捕获
int main() {
    BST t;
    try {
        t.insert("Try");
        t.insert("throw");
        t.insert("catch");
        t.insert("cin");
        t.insert("cout");
        t.insert("static");
        t.insert("friend");
        t.insert("catch"); // 尝试插入重复元素
        t.display();
    } catch(const exception& e) {
        cout << "错误:" << e.what() << endl;
    }
    return 0; 
}

2. 加入static:统计节点总数

给BST类添加静态成员变量,统计所有BST实例的总节点数:

class BST {
private:
    struct node { /* ... */ };
    node* root;
    static int totalNodes; // 静态成员变量

    node* insert(string x, node* t)
    {
        if(t == NULL)
        {
            t = new node;
            t->data = x;
            t->left = t->right = NULL;
            totalNodes++; // 节点数+1
        }
        // ... 其余逻辑
    }

public:
    BST() { root = NULL; }
    static int getTotalNodes() { return totalNodes; } // 静态方法
    // ... 其余公共方法
};

// 静态成员变量初始化
int BST::totalNodes = 0;

// main中调用
cout << "总节点数:" << BST::getTotalNodes() << endl;

3. 加入friend:友元函数访问私有节点

声明一个友元函数,直接访问BST的私有root节点进行遍历:

#include<functional> // 需要包含这个头文件

class BST {
private:
    struct node { /* ... */ };
    node* root;
    // ... 其余私有成员

    friend void printBST(const BST& tree); // 声明友元函数

public:
    // ... 公共方法
};

// 友元函数实现,直接访问root
void printBST(const BST& tree) {
    function<void(node*)> inorder = [&](node* t) {
        if(!t) return;
        inorder(t->left);
        cout << t->data << " ";
        inorder(t->right);
    };
    inorder(tree.root);
    cout << endl;
}

// main中调用
printBST(t);

4. 加入cin:支持手动输入插入元素

在main函数中添加cin逻辑,让用户手动输入元素插入:

int main() {
    BST t;
    string input;
    cout << "请输入要插入的元素(输入quit结束):" << endl;
    while(cin >> input && input != "quit") {
        try {
            t.insert(input);
        } catch(const exception& e) {
            cout << e.what() << endl;
        }
    }
    printBST(t);
    cout << "总节点数:" << BST::getTotalNodes() << endl;
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 20:41:07