2013-05-03 39 views
0

我有.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; 
}; 

这是我第一次使用二叉树,而这混淆了我最多的就是类里面的类的东西。我只需要知道如何去添加字符串到树中。

+0

做了一些改动,修正了一些错误。 – FJam 2013-05-03 22:48:32

+0

你需要写主要做什么?我也猜测你必须实现类中声明的所有功能...... – 2013-05-03 22:49:40

+1

这是一项家庭作业吗? – TemplateRex 2013-05-03 22:49:45

回答

1

一些示例用法:

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; 
}