views:

126

answers:

4

I've made BSTs before. Can I use this to make a BST without modifications?

template <class Item>
class binary_tree_node
{
  public:

  private:
    Item data_field;
    binary_tree_node *left_ptr;
    binary_tree_node *right_ptr;
};

I tried making a BST with this but ran into some problems. For one thing, when I create the root node, I can't access the pointers to its child nodes.

+3  A: 

No, you won't be able to make a BST with a class that says "place public member functions here".

It won't even compile without some pretty hacky typedefs and macros.

Anon.
I was going to downvote and rant about how unprofessional this answer was... Then I saw he said "without modifications".
Platinum Azure
Wait, was that edited in by Robert C. Cartaino?
Platinum Azure
@Platinum: No, he only edited the tags and fixed the indentation - click on the *edited x mins ago* to see the edit history.
Georg Fritzsche
The OP has now removed that line from the question. The appropriate answer now would probably be "No, you won't be able to make a BST with a class that has absolutely no public fields or methods".
Anon.
A: 

Yes you can make a BST with that but its going to take some work to get there. :)

Brian Ensink
+2  A: 

Without modifications, no.

But that line 'place public member functions here' is screaming out that you should be modifying it.

Since you talk about permission problem, it means you are trying to use free functions. But since the pointers are private, you won't have access to them.

What you should be doing is creating member functions. For example:

class binary_tree_node
{
  public:
    binary_tree_node()
    {
    }

    bool is_item_in_tree(const Item &item)
    {
    }

    ...
};

Anyway, I'd recommend reviewing your C++ basics around visibility and OOP.

R Samuel Klatchko
Yeah, I see what you're saying. In order to access the child pointers I need to make class functions that do so.
Phenom
A: 

Normally,you should provide the comparation interface for the new Item class,becuase in the insert and remove opeartion,the comparation are needed.

The concrete information was not given,so I do not know whether you use < and > etc relation operators or not.But If you use them.You should make sure the new Item class support these operators.

I'd advice you to add one generic comparation class name Comp to provide the compration interface for the Item class.

Jichao