【问题标题】:An iterative binary tree traversal in C++C ++中的迭代二叉树遍历
【发布时间】:2014-04-29 04:00:20
【问题描述】:

我正在为二叉搜索树创建非递归遍历。但是,我遇到了一些非常奇怪的错误。

这是我的横向函数的代码:

void BinarySearchTree<ItemType>::nrInOrderTraversal(void visit(ItemType&)) const
{
    stack <int> nodeStack;
    int *curPtr;

    bool done = false;

    while (!done)
    {
        if (rootPtr() != 0)
        {
            //Place pointer to node on stack before traversing the node's left subtree
            nodeStack.push(rootPtr());

            //Traverse the left subtree
            rootPtr() = rootPtr()->getLeftChildPtr();
        }
        else //Backtrack from the empty subtree and visit the node at the top of the stack;
            //however if the stack is empty, you are done.
        {
            if(!nodeStack.empty())
            {
                nodeStack.top(rootPtr());
                visit(rootPtr()->getItem());
                nodeStack.pop();

                //Traverse the right subtree of the node just visited
                rootPtr() = rootPtr()->getRightChildPtr();
            }

               else
                done = true;
        }

    }
}

以及我的主要遍历部分的代码:

BinarySearchTree<string>* tree4Ptr = new BinarySearchTree<string>();

   tree4Ptr->add("10");
   tree4Ptr->add("20");
   tree4Ptr->add("30");
   tree4Ptr->add("40");
   tree4Ptr->add("50");
   tree4Ptr->add("60");
   tree4Ptr->add("70");
   tree4Ptr->add("80");
   tree4Ptr->add("90");
   tree4Ptr->add("100");
   tree4Ptr->add("110");
   tree4Ptr->add("120");
   tree4Ptr->add("130");
   tree4Ptr->add("140");
   tree4Ptr->add("150");
   tree4Ptr->add("160");


   cout<<"Tree 4 nrInOrderTraversal: "<<endl;
   tree4Ptr-> nrInOrderTraversal(display); 

我遇到的具体错误是:

C:\Users\Maura\Documents\CSC2014\ATJprog6\BinarySearchTree.cpp|328|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|

C:\Users\Maura\Documents\CSC2014\ATJprog6\BinarySearchTree.cpp|331|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|

C:\Users\Maura\Documents\CSC2014\ATJprog6\BinarySearchTree.cpp|334|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|

C:\Users\Maura\Documents\CSC2014\ATJprog6\BinarySearchTree.cpp|334|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|

C:\Users\Maura\Documents\CSC2014\ATJprog6\BinarySearchTree.cpp|341|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|

C:\Users\Maura\Documents\CSC2014\ATJprog6\BinarySearchTree.cpp|342|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|

C:\Users\Maura\Documents\CSC2014\ATJprog6\BinarySearchTree.cpp|346|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|

C:\Users\Maura\Documents\CSC2014\ATJprog6\BinarySearchTree.cpp|346|error: '((const BinarySearchTree<std::basic_string<char> >*)this)->BinarySearchTree<std::basic_string<char> >::rootPtr' cannot be used as a function|



void display(string& anItem)
{
   cout << "Displaying item - " << anItem << endl;
}  // end display

有些人要求提供有关 rootPtr 的头文件,所以这里是

/** Link-based implementation of the ADT binary search tree.
 @file BinarySearchTree.h */

#ifndef _BINARY_SEARCH_TREE
#define _BINARY_SEARCH_TREE

#include "BinaryTreeInterface.h"
#include "BinaryNode.h"
#include "BinaryNodeTree.h"
#include "NotFoundException.h"
#include "PrecondViolatedExcep.h"

template<class ItemType>
class BinarySearchTree : public BinaryNodeTree<ItemType>
{
private:
   BinaryNode<ItemType>* rootPtr;

protected:
   //------------------------------------------------------------
   // Protected Utility Methods Section:
   // Recursive helper methods for the public methods.
   //------------------------------------------------------------
   // 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);

   // Removes the given target value from the tree while maintaining a
   // binary search tree.
   BinaryNode<ItemType>* removeValue(BinaryNode<ItemType>* subTreePtr,
                                     const ItemType target,
                                     bool& success);

   // Removes a given node from a tree while maintaining a
   // binary search tree.
   BinaryNode<ItemType>* removeNode(BinaryNode<ItemType>* nodePtr);

   // Removes the leftmost node in the left subtree of the node
   // pointed to by nodePtr.
   // Sets inorderSuccessor to the value in this node.
   // Returns a pointer to the revised subtree.
   BinaryNode<ItemType>* removeLeftmostNode(BinaryNode<ItemType>* subTreePtr,
                                            ItemType& inorderSuccessor);

   // Returns a pointer to the node containing the given value,
   // or 0 if not found.
   BinaryNode<ItemType>* findNode(BinaryNode<ItemType>* treePtr,
                                  const ItemType& target) const;

public:
   //------------------------------------------------------------
   // Constructor and Destructor Section.
   //------------------------------------------------------------
   BinarySearchTree();
   BinarySearchTree(const ItemType& rootItem);
   BinarySearchTree(const BinarySearchTree<ItemType>& tree);
   virtual ~BinarySearchTree();

   //------------------------------------------------------------
   // Public Methods Section.
   //------------------------------------------------------------
   bool isEmpty() const;
   int getHeight() const;
   int getNumberOfNodes() const;
   ItemType getRootData() const throw(PrecondViolatedExcep);
   void setRootData(const ItemType& newData) const throw(PrecondViolatedExcep);
   bool add(const ItemType& newEntry);
   bool remove(const ItemType& anEntry);
   void clear();
   ItemType getEntry(const ItemType& anEntry) const throw(NotFoundException);
   bool contains(const ItemType& anEntry) const;

   //------------------------------------------------------------
   // Public Traversals Section.
   //------------------------------------------------------------
   void preorderTraverse(void visit(ItemType&)) const;
   void inorderTraverse(void visit(ItemType&)) const;
   void postorderTraverse(void visit(ItemType&)) const;
   void nrInOrederTraversal(void visit (ItemType&)) const;
   //------------------------------------------------------------
   // Overloaded Operator Section.
   //------------------------------------------------------------
   BinarySearchTree<ItemType>& operator=(const BinarySearchTree<ItemType>& rightHandSide);
}; // end BinarySearchTree

#include "BinarySearchTree.cpp"

#endif

【问题讨论】:

  • 你的函数是nrInOrederTraversal,但你称它为nrInOrderTraversal
  • 需要你多贴代码,比如display的定义
  • 已在问题底部添加显示。
  • @Nick 这不是问题,但感谢您指出。当我将代码复制到 stackoverflow 时,我不小心忘记修复它名称的所有实例。
  • @Neko 你确定你粘贴了完整的错误信息吗?似乎应该还有更多。您发布的内容显示错误 location,但我没有看到实际错误 message

标签: c++ binary-search-tree non-recursive


【解决方案1】:

rootPtr 是数据成员,而不是方法,因此不应将其作为函数调用。 IE。在您的代码中每次提及 rootPtr 后删除 ()

【讨论】:

  • 他们早先被发现并取出,但代码仍然给我们错误
【解决方案2】:

[编辑] 标题显示:rootPtr 是一个对象,而不是一个函数/方法。在所有对 rootPtr 的引用之后删除 ()。

例如代替 rootPtr() != 0rootPtr != 0

顺便说一句。如果你可以使用 C++11,你应该使用 nullptr 而不是 0 作为指针。即写rootPtr != nullptr

【讨论】:

    【解决方案3】:

    这行有错别字。

    tree4Ptr-> nrInOrederTraversal(display); 
    

    【讨论】:

    • 该名称应该有错字(这是一个笑话)我们确实更改了其他行以避免问题混淆,但我想我们忘记了那个。忽略函数名。
    • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • @computerfreak 和 Bhargav Krishna:我很抱歉。当我将代码转移到 stackoverflow 时,我尝试修复所有显示 nrInOrederTraversal 的实例。在实际代码中,所有这些实例都匹配。 (即它们都有完全相同的拼写错误)我只是更改了上面代码中的行以避免进一步混淆。
    • @Neko 不用担心,它会发生。实际上,我的评论是针对 Bhargav Krishna 的,因为他的回答更适合作为评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多