-3

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.

neuromancer
  • 53,769
  • 78
  • 166
  • 223
  • 1
    I would think it would be important to show what the public member functions are. If everything is private, then no, it can't be done. – Phil Feb 08 '10 at 02:39
  • You say "I've made BSTs before" - so why don't do compare this to your previous solutions? – Mitch Wheat Feb 08 '10 at 02:41

3 Answers3

3

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.
  • 58,739
  • 8
  • 81
  • 86
  • I was going to downvote and rant about how unprofessional this answer was... Then I saw he said "without modifications". – Platinum Azure Feb 08 '10 at 03:06
  • @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 Feb 08 '10 at 03:10
  • 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. Feb 08 '10 at 03:28
2

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
  • 74,869
  • 16
  • 134
  • 187
  • Yeah, I see what you're saying. In order to access the child pointers I need to make class functions that do so. – neuromancer Feb 08 '10 at 03:23
0

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
  • 40,341
  • 47
  • 125
  • 198