0

I am working on creating a non recursive traversal for a binary search tree. I am, however, running into a couple of very odd errors.

here is the code for my transverse function:

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;
        }

    }
}

and the code for traversal part of my main:

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); 

the particular errors I am getting are:

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

some people were asking for the header file concerning rootPtr so here it is

/** 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
Cœur
  • 37,241
  • 25
  • 195
  • 267
Neko
  • 51
  • 1
  • 1
  • 9
  • Your function is `nrInOrederTraversal`, but you are calling it `nrInOrderTraversal`. – imreal Apr 29 '14 at 04:02
  • Need you to post more code, like the definition of display – Kevin Le - Khnle Apr 29 '14 at 04:07
  • Display has been added at the bottom of the question. – Neko Apr 29 '14 at 04:32
  • @Nick That isn't the problem, but thanks for pointing it out. When I copied the code over to stackoverflow, I accidentally forgot to fix all of the instances of the name of it. – Neko Apr 29 '14 at 04:35
  • @Neko Are you sure you pasted the full error message? It seems like there should be more. What you posted shows an error *location*, but I'm not seeing an actual error *message*. – cf- Apr 29 '14 at 04:43
  • @computerfreaker You're right. Apparently the full message didn't copy/paste correctly. Thanks for pointing it out. – Neko Apr 29 '14 at 04:50
  • Try changing "void visit(ItemType&)" for "void (*visit)(ItemType&)" – imreal Apr 29 '14 at 05:18

3 Answers3

1

[edit] The header shows: rootPtr is an object rather than a function/method. Remove the () after all references to rootPtr.

E.g. instead of rootPtr() != 0 write rootPtr != 0.

Btw. if you can use C++11 you should use nullptr instead of 0 for pointers. i.e. write rootPtr != nullptr

Benjamin Mesing
  • 4,075
  • 1
  • 18
  • 22
1

rootPtr is a data member, not a method, so it should not be called as a function. I.e. delete the () after every mention of rootPtr in your code.

happydave
  • 7,127
  • 1
  • 26
  • 25
0

typoo in this line.

tree4Ptr-> nrInOrederTraversal(display); 
Bhargav Krishna
  • 227
  • 4
  • 12
  • the name is supposed to have a typo in it (its a joke) we did change the other lines to avoid confusion in the question but i guess we forgot that one. just ignore the function name. – Neko Apr 29 '14 at 04:09
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – cf- Apr 29 '14 at 04:29
  • @computerfreak and Bhargav Krishna: My apologies. When I was transferring the code over to stackoverflow, I tried to fix all instances where nrInOrederTraversal was shown. In the actual code, all of those instances match. (i.e. they all have the exact same misspelling) I just changed the line in the code above to avoid further confusion. – Neko Apr 29 '14 at 04:39
  • @Neko No worries, it happens. My comment was directed at Bhargav Krishna, actually, since his answer would be more appropriate as a comment. – cf- Apr 29 '14 at 04:42