簡體   English   中英

在沒有節點指針作為參數的情況下遍歷二進制搜索樹

[英]In order traversal for binary search tree without node pointer as parameter

我必須在二進制搜索樹中為實驗室編寫函數以便順序遍歷。 我的問題是,我已經得到一個必須遵循的接口,並且在其中可以傳遞給遍歷函數的唯一參數是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

我假設BinaryNode具有方法

const BinaryNode* getLeft() const;
const BinaryNode* getRight() const;
const ItemType& getValue() const;

[由於以下原因而被編輯:“請注意,我們無法在課程中添加任何其他內容”]

您會看到,該方法是static -這意味着它不依賴於有關樹的特定瞬間的任何知識。

因此,它可以放置在任何地方。

例如,只需將其作為靜態函數寫入類的"BinarySearchTree.cpp"文件內即可。

另一個解決方案:inorderTraverse方法中實現它,作為lambda函數,如下所示:

  // as a method of this class, you **do** have access to the root node
  void inorderTraverse(void visit(const ItemType&)) const {
    // this is a lambda function
    auto inOrderRecurse=(
      const BinaryNode<ItemType>* node, 
      void visit(const ItemType&)
    ) {
         if(node) {
           auto n=node->getLeft();
           if(n) {
              this->inOrderRecurse(n, visit);
           }
           visit(node->getValue());
           n=node->getRight();
           if(n) {
             this->inOrderRecurse(n, visit);
           }
        }
      }
    ;
    inOrderRecurse(this->rootPtr);
  }

還有另一種解決方案 :如果不允許使用lambda,則仍可以在method內部聲明一個類/結構 因此,讓我們在inorderTraverse方法中聲明/使用一個。

  // as a method of this class, you **do** have access to the root node
  void inorderTraverse(void visit(const ItemType&)) const {
    struct recurser {
      static void inOrderRecurse(
        const BinaryNode<ItemType>* node, 
        void visit(const ItemType&)
      ) {
       // etc...
      }
    };
    recurser::inOrderRecurse(this->rootPtr);
  }

[原始答案]

這樣,可以將inorderTraverse實現為:

private:
  // "All problems in computer science can be solved by another level of indirection, 
  // except of course for the problem of too many indirections."
  // In the context, **this method** is another level of indirection
  static void inOrderRecurse(
      const BinaryNode<ItemType>* node, 
      void visit(const ItemType&)
  ) const {
   if(node) {
     auto n=node->getLeft();
     if(n) {
       this->inOrderRecurse(n, visit);
     }
     visit(node->getValue());
     n=node->getRight();
     if(n) {
       this->inOrderRecurse(n, visit);
     }
  }
public:

  // as a method of this class, you **do** have access to the root node
  void inorderTraverse(void visit(const ItemType&)) const {
    // note this `const` here  ---^ needed because of ^^^^ this one
    inOrderRecurse(this->rootPtr);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM