简体   繁体   English

二进制搜索树,使用堆栈非递归

[英]binary search tree, non recursive using stack

What should I know about having two separate classes in one .h file? 对于一个.h文件中有两个单独的类,我应该知道些什么?

I have a binary search tree class with all the members and public & private functions. 我有一个包含所有成员以及公共和私有功能的二进制搜索树类。

class BinarySearchTree
{
   struct Node {
      Node* left;
      Node* right;
      int val;
   };
};

and following that code I want to design a stack of pointers to that binary search tree node. 然后按照该代码,我要设计一个指向该二进制搜索树节点的指针堆栈。 Within the same.h file I have 在same.h文件中

class stack
{
  Node* array;
  //

};

Visual Studio doesn't show linkage and doesn't recognize Node* . Visual Studio不会显示链接,也无法识别Node* Is it ok to declare two separate classes in one .h file or is it better to implement the stack class nested inside the binary search tree class? 是否可以在一个.h文件中声明两个单独的类,还是更好地实现嵌套在二进制搜索树类中的堆栈类?

You've declared a struct called Node that is nested in the class BinarySearchTree , so if you want to refer to that struct outside of the class, you need to refer to it like this: 您已经声明了一个嵌套在类BinarySearchTree中的名为Nodestruct ,因此,如果要在类外部引用该struct ,则需要像这样引用它:

class stack
{
  BinarySearchTree::Node* array;
  //

};

Whether or not that's good design is a whole new question, so I would recommend getting a bit further with the implementation before asking more. 好的设计是否是一个全新的问题,所以我建议在提出更多要求之前,进一步介绍一下实现。

EDIT 编辑

Like you noticed, it's necessary to make the nested struct public if you want to use it outside of the class. 就像您注意到的那样,如果要在类之外使用嵌套结构,则必须将其public That, in itself, is not necessarily bad or wrong. 这本身并不一定是坏事或错误。 You're not exposing data, just a declaration. 您不会公开数据,而只是声明。

You've got two choices: 您有两种选择:

  1. Make the nested struct public. 将嵌套结构公开。
  2. Take the nested struct outside of the enclosing class. 将嵌套结构带到封闭类之外。

Personally, I'd go for the first option. 就个人而言,我会选择第一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM