c++ - 如何使用这个二叉树类?

标签 c++ binary-tree

我有 .h 文件和 .cpp 文件的代码。我只是不知道如何主要使用它。这是给定的,我需要写主要内容。

.h文件:

class binary_tree {
public:
    class node;
    binary_tree();
    void addRoot(const std::string &data);
    void addLeft(node *nd, const std::string &data);
    void addRight(node *nd, const std::string &data);

    node *getRoot();

    std::string get(node *node);
    bool isEmpty();

private:
    node *root;
};

struct binary_tree::node {
    node(const std::string &data);
    std::string data;
    node *left, *right;
};

这是我第一次使用二叉树,最让我困惑的是类中的类。我只需要知道如何将字符串添加到树中。

最佳答案

一些示例用法:

int main()
{
    // construct the tree
    binary_tree tree;

    // add a root
    tree.addRoot("this is the root");

    // add children to the root
    tree.addRight(tree.getRoot(), "left child");
    tree.addRight(tree.getRoot(), "right child");

    // get the data with either of these:
    std::cout << tree.getRoot()->left->data;
    std::cout << tree.get(tree.getRoot()->left);
    return 0;
}

关于c++ - 如何使用这个二叉树类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16368526/

相关文章:

c++ - 打包一个 C++ 项目以发布一些依赖项,如 pthread boost curl 等

c++ - SFINAE 启用/禁用功能和模板别名

c++ - 是否可以通过访问 USB 设备为三星智能电视编写 C++ 应用程序

java - 从遍历构建二叉树

C++ 继承 vector 问题(无限循环+在其他类中使用 vector 的问题)

c++ - C 从 GetSaveFileName() 返回的字符包含空字符

c++ - 二叉树和特殊节点打印

C二叉树结构

java - 中序二叉树方法的返回值

将中缀表达式转换为后缀表达式导致出现奇怪的符号