2014-04-29 80 views
0

我正在爲二叉搜索樹創建非遞歸遍歷。但是,我遇到了一些非常奇怪的錯誤。迭代二叉樹橫向問題

這裏是我的橫向函數的代碼:

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 

感謝所有幫助!

+0

你的函數是'nrInOrederTraversal',但你叫它'nrInOrderTraversal'。 – imreal

+0

需要您發佈更多的代碼,例如顯示器的定義 –

+0

顯示已添加到問題的最底部。 – Neko

回答

0

typoo在這一行。

tree4Ptr-> nrInOrederTraversal(display); 
+0

名稱應該有它的一個錯字(它的一個笑話),我們確實改變了其他線路,以避免問題的混淆,但我想我們忘了那一個。只是忽略函數名稱。 – Neko

+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 – computerfreaker

+0

@computerfreak和Bhargav克里希納:我的歉意。當我將代碼轉移到stackoverflow時,我試圖修復顯示nrInOrederTraversal的所有實例。在實際的代碼中,所有這些實例都匹配。 (即它們都具有完全相同的拼寫錯誤),我只是更改了上面的代碼中的行,以避免更進一步的混淆。 – Neko

1

標題顯示:rootPtr是一個對象而不是函數/方法。全部引用rootPtr後刪除()。

E.g.而不是 rootPtr() != 0rootPtr != 0

Btw。如果您可以使用C++ 11,則應該使用nullptr而不是0指針。即寫rootPtr != nullptr

1

rootPtr是數據成員,而不是一個方法,因此它不應該被作爲函數調用。即每次在代碼中提及rootPtr後,刪除()

+0

他們被發現並提前取出,但代碼仍然給我們錯誤 – Neko