开发者

Can I make a binary search tree with this?

开发者 https://www.devze.com 2022-12-19 05:51 出处:网络
I\'ve made BSTs before. Can I use this to make a BST without modifications? template <class Item>

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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消