私は、トラバーサルのためにバイナリ検索ツリー内のラボ用の関数をコーディングする必要があります。私の問題は、私は私が従わなければならないインターフェースを与えてきたし、その中で私がトラバーサル関数に渡すことができる唯一のパラメータがvoid戻り値の型の別の関数である:ノードポインタを持たないバイナリ検索ツリーのトラバーサルをパラメータとして
void BinarySearchTree<ItemType, KeyType>::inorderTraverse(void visit(ItemType&)) const
訪問関数は、基本的機能でありますツリーの特定のユースケースのために定義したいと思うように、ツリーを昇順で印刷したい場合、inorderTraverse
関数に渡す関数は印刷関数になります。ノードポインタをパラメータとして使用せずにツリー全体をどのようにトラバースするかを理解することはできません。私はコード全体を求めているわけではなく、ちょうど正しい方向に私を向けることができるアドバイスです!ここBinarySearchTree.h
です:
template<typename ItemType, typename KeyType>
class BinarySearchTree
{
private:
BinaryNode<ItemType>* rootPtr;
// Recursively deletes all nodes from the tree.
void destroyTree(BinaryNode<ItemType>* subTreePtr);
// Recursively finds where the given node should be placed and
// inserts it in a leaf at that point.
BinaryNode<ItemType>* insertInorder(BinaryNode<ItemType>* subTreePtr,
BinaryNode<ItemType>* newNode);
// Returns a pointer to the node containing the given value,
// or nullptr if not found.
BinaryNode<ItemType>* findNode(BinaryNode<ItemType>* treePtr,
const KeyType& target) const;
public:
//------------------------------------------------------------
// Constructor and Destructor Section.
//------------------------------------------------------------
BinarySearchTree();
virtual ~BinarySearchTree();
//------------------------------------------------------------
// Public Methods Section.
//------------------------------------------------------------
bool add(const ItemType& newEntry);
ItemType getEntry(const KeyType& aKey) const throw(NotFoundException);
bool contains(const KeyType& aKey) const;
//------------------------------------------------------------
// Public Traversals Section.
//------------------------------------------------------------
void inorderTraverse(void visit(ItemType&)) const;
}; // end BinarySearchTree
#include "BinarySearchTree.cpp"
#endif
:http://stackoverflow.com/questions/ 18413705/c-binary-tree-traverse-and-function-pointer-parameter?rq = 1 –
いいえ、それはまさに私の問題です。私はノードポインタを関数に渡すことができません。再帰的にトラバースする – pyro97
再帰を使うはずですか? –