Trees and Inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #1

    Trees and Inheritance

    Hey guys,

    I'm learning C++ for the first time and have found it quite similar to Java in many ways. However, Inheritance differed a little bit, and I seem to have trouble with it, especially when it comes to trees.

    The book I'm studying from defines Binary Trees as two seperate classes - an abstract binaryTree, which holds basic functions such as nodeCount, leavesCount, inorderTraversa l, etc. etc. This class also holds a protected variable of type treeNode - a struct defined in the header file holding a variable of some type, a pointer to the left child, and a pointer to the right child (each child is of type treeNode). The protected variable is entitled root - it is the first node inserted into the tree, and it is how the original variable keeps track of the tree.

    The second class is called orderedBinaryTr ee. It is a subclass of binaryTree, inheriting its members publicly:

    Code:
    template <class elem>
    class orderedBinaryTree : public binaryTree {
       ...
       ...
       ...
    }
    As a subclass of binaryTree, it should have access to the protected variable root - yet, when compiling a short test-program, my orderedBinaryTr ee variable does not recognize root - it obviously cannot see root in binaryTree.

    I have basically copied the code for both classes straight out of the book, changing only a few things (such as indenting style and naming style) to my preference, yet it doesn't work. My teacher actually downloaded the official copy of these classes and received the same error.

    Any ideas as to why orderedBinaryTr ee cannot access root?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think I would want to see more of the code to answer this.

    Presumably root was protected and not private?

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Following is the class definitions of treeNode, binaryTree, and orBinTree, along with a simple test program. Below the test program is the list of errors I am recieving.

      Code:
      // The following was saved as binaryTree.h
      
      #include <iostream>
      
      using namespace std;
      
      template <class elem>
      struct treeNode {
             elem info;
             treeNode<elem> *left;
             treeNode<elem> *right;
      };
      
      template <class elem>
      class binaryTree {
            public:
                   const binaryTree<elem>& operator=(const binaryTree<elem>& other);
                   bool isEmpty() const;
                   void inorderTraversal(void (*visit) (elem& item)) const;
                   void preorderTraversal() const;
                   void postorderTraversal() const;
                   int treeHeight() const;
                   int treeNodeCount() const;
                   int treeLeavesCount() const;
                   void destroyTree();
                   virtual bool search(const elem& searchItem) const = 0;
                   virtual void insert(const elem& insertItem) = 0;
                   virtual void deleteNode(const elem& deleteItem) = 0;
                   binaryTree(const binaryTree<elem>& other);
                   binaryTree();
                   ~binaryTree();
                   
            protected:
                      treeNode<elem> *root;
                      
            private:
                    void copyTree(treeNode<elem>* &copiedTreeRoot, treeNode<elem>* otherTreeRoot);
                    void destroy(treeNode<elem>* &p);
                    void inorder(treeNode<elem> *p, void (*visit) (elem& item)) const;
                    void preorder(treeNode<elem> *p) const;
                    void postorder(treeNode<elem> *p) const;
                    int height(treeNode<elem> *p) const;
                    int max(int x, int y) const;
                    int nodeCount(treeNode<elem> *p) const;
                    int leavesCount(treeNode<elem> *p) const;
      };
      
      // The following was saved as orBinTree.h
      
      #include <iostream>
      #include "binaryTree.h"
      
      using namespace std;
      
      template <class elem>
      class orBinTree: public binaryTree<elem> {
            public:
                   bool search(const elem& searchItem) const;
                   void insert(const elem& insertItem);
                   void deleteNode(const elem& deleteItem);
            
            private:
                    void deleteFromTree(treeNode<elem>* &p);
      };
      
      // The following is my test program, saved as binTreeTest.cpp
      
      #include <iostream>
      #include "orBinTree.h"
      using namespace std;
      
      void print(int& x);
      
      int main() {
          orBinTree<int> tree;
          int num;
          cout << "Enter 10 numbers: ";
          for (int i = 0; i < 10; i++) {
              cin >> num;
              tree.insert(num);
          }
          
          cout << endl << "Tree nodes printed inorder: ";
          tree.inorderTraversal(print);
          cout << endl;
          cout << "Tree Height: " << tree.treeHeight() << endl;
          cout << "Tree Nodes: " << tree.treeNodeCount() << endl;
          cout << "Tree Leaves: " << tree.treeLeavesCount() << endl;
          
          system("PAUSE");
          return 0;
      }
      
      void print(int& x) {
           cout << x << " ";
      }
      
      /* Errors:
      
         H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h In member function `bool orBinTree<elem>::search(const elem&) const': 
      23 H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h `root' undeclared (first use this function) 
         H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h In member function `void orBinTree<elem>::insert(const elem&)': 
      47 H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h `root' undeclared (first use this function) 
         H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h In member function `void orBinTree<elem>::deleteNode(const elem&)':
      69 H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h `root' undeclared (first use this function) 
      
      */
      As you can see, root is a pointer of type treeNode - it is a protected member of binaryTree. Thus, orBinTree, a subclass of binaryTree, should be able to access and manipulate it, yet the errors indicate that no member named root exists.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Does anyone have any idea of the problem?

        I have considered simply adding a public function to the binaryTree class called getRoot:

        Code:
        treeNode* getRoot() { return root; }
        This would allow orBinTree to access root by declaring a treeNode pointer and setting it to getRoot - but it still does not make sense that orBinTree has no access to the protected variable root.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You have not posted all of orBinTree.h, I can tell this because you have posted 15 lines of code for orBinTree.h but the errors you are getting occur in orBinTree.h starting at line 23.

          In generally when getting compile errors and asking for help it is always a good idea to post the lines of code you are actually getting the errors in.

          Also notice the error

          `root' undeclared (first use this function)

          It's not saying that you can't access a member that actually exists, it's saying that it can find any variable or member with that name

          You may wish to try accessing it as binaryTree::roo t but it's probably some other error.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            OK, I've tried posting the code, and the forum keeps telling me:

            "The following errors occurred when this message was submitted:
            You have included too many images in your signature or in your previous post. Please go back and correct the problem and then continue again.

            Images include use of smilies, the vB code [img] tag and HTML <img> tags. The use of these is all subject to them being enabled by the administrator."

            Is there some other way I can send you a copy of the code?

            Also, I tried replacing each instance of 'root' in orBinTree.h with 'binaryTree::ro ot', but it didn't work - same errors in the same place.

            Comment

            Working...