简体   繁体   中英

Where to implement a stack class (to be used in a non recursive binary search function)

First of all, this sure is my homework but I will not be asking for code.

Inside a BST.h file, I implemented all the private members, functions and public functions. However, I am finding trouble where to implement a stack (a stack of pointers to BSTNode ).

So would it be

#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H

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

#endif

so do I implement a stack class inside the BinarySearchTree class or outside the class in the same .h file?

The professor is not asking for 2 different .h files so I assume it should be either inside or outside. And if it is inside the binarysearchtree class, what about constructors for the stack class?

You probably want to implement the stack outside of the BST class. Typically, nested classes are used when the nested class is exclusively used with the parent class. For example, Node is nested because you wouldn't use a Node without the BST .

A stack isn't really related to the BST even though you're going to use it for the BST . If you design a good stack , that would allow you reuse it without needing to pull it out of BST .

It isn't incorrect to include multiple class declarations in a header file as long as they are small, and logically connected. But that comes down to style and project requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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