2013-05-03 44 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; 
}