473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trees and Inheritance

Ganon11
3,652 Expert 2GB
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, inorderTraversal, 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 orderedBinaryTree. It is a subclass of binaryTree, inheriting its members publicly:

Expand|Select|Wrap|Line Numbers
  1. template <class elem>
  2. class orderedBinaryTree : public binaryTree {
  3.    ...
  4.    ...
  5.    ...
  6. }
As a subclass of binaryTree, it should have access to the protected variable root - yet, when compiling a short test-program, my orderedBinaryTree 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 orderedBinaryTree cannot access root?
Oct 25 '06 #1
5 3906
Banfa
9,065 Expert Mod 8TB
I think I would want to see more of the code to answer this.

Presumably root was protected and not private?
Oct 26 '06 #2
Ganon11
3,652 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. // The following was saved as binaryTree.h
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. template <class elem>
  8. struct treeNode {
  9.        elem info;
  10.        treeNode<elem> *left;
  11.        treeNode<elem> *right;
  12. };
  13.  
  14. template <class elem>
  15. class binaryTree {
  16.       public:
  17.              const binaryTree<elem>& operator=(const binaryTree<elem>& other);
  18.              bool isEmpty() const;
  19.              void inorderTraversal(void (*visit) (elem& item)) const;
  20.              void preorderTraversal() const;
  21.              void postorderTraversal() const;
  22.              int treeHeight() const;
  23.              int treeNodeCount() const;
  24.              int treeLeavesCount() const;
  25.              void destroyTree();
  26.              virtual bool search(const elem& searchItem) const = 0;
  27.              virtual void insert(const elem& insertItem) = 0;
  28.              virtual void deleteNode(const elem& deleteItem) = 0;
  29.              binaryTree(const binaryTree<elem>& other);
  30.              binaryTree();
  31.              ~binaryTree();
  32.  
  33.       protected:
  34.                 treeNode<elem> *root;
  35.  
  36.       private:
  37.               void copyTree(treeNode<elem>* &copiedTreeRoot, treeNode<elem>* otherTreeRoot);
  38.               void destroy(treeNode<elem>* &p);
  39.               void inorder(treeNode<elem> *p, void (*visit) (elem& item)) const;
  40.               void preorder(treeNode<elem> *p) const;
  41.               void postorder(treeNode<elem> *p) const;
  42.               int height(treeNode<elem> *p) const;
  43.               int max(int x, int y) const;
  44.               int nodeCount(treeNode<elem> *p) const;
  45.               int leavesCount(treeNode<elem> *p) const;
  46. };
  47.  
  48. // The following was saved as orBinTree.h
  49.  
  50. #include <iostream>
  51. #include "binaryTree.h"
  52.  
  53. using namespace std;
  54.  
  55. template <class elem>
  56. class orBinTree: public binaryTree<elem> {
  57.       public:
  58.              bool search(const elem& searchItem) const;
  59.              void insert(const elem& insertItem);
  60.              void deleteNode(const elem& deleteItem);
  61.  
  62.       private:
  63.               void deleteFromTree(treeNode<elem>* &p);
  64. };
  65.  
  66. // The following is my test program, saved as binTreeTest.cpp
  67.  
  68. #include <iostream>
  69. #include "orBinTree.h"
  70. using namespace std;
  71.  
  72. void print(int& x);
  73.  
  74. int main() {
  75.     orBinTree<int> tree;
  76.     int num;
  77.     cout << "Enter 10 numbers: ";
  78.     for (int i = 0; i < 10; i++) {
  79.         cin >> num;
  80.         tree.insert(num);
  81.     }
  82.  
  83.     cout << endl << "Tree nodes printed inorder: ";
  84.     tree.inorderTraversal(print);
  85.     cout << endl;
  86.     cout << "Tree Height: " << tree.treeHeight() << endl;
  87.     cout << "Tree Nodes: " << tree.treeNodeCount() << endl;
  88.     cout << "Tree Leaves: " << tree.treeLeavesCount() << endl;
  89.  
  90.     system("PAUSE");
  91.     return 0;
  92. }
  93.  
  94. void print(int& x) {
  95.      cout << x << " ";
  96. }
  97.  
  98. /* Errors:
  99.  
  100.    H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h In member function `bool orBinTree<elem>::search(const elem&) const': 
  101. 23 H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h `root' undeclared (first use this function) 
  102.    H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h In member function `void orBinTree<elem>::insert(const elem&)': 
  103. 47 H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h `root' undeclared (first use this function) 
  104.    H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h In member function `void orBinTree<elem>::deleteNode(const elem&)':
  105. 69 H:\Independent Study\Random\Data Structures\Binary Tree\orBinTree.h `root' undeclared (first use this function) 
  106.  
  107. */
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.
Oct 26 '06 #3
Ganon11
3,652 Expert 2GB
Does anyone have any idea of the problem?

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

Expand|Select|Wrap|Line Numbers
  1. 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.
Oct 27 '06 #4
Banfa
9,065 Expert Mod 8TB
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::root but it's probably some other error.
Oct 27 '06 #5
Ganon11
3,652 Expert 2GB
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::root', but it didn't work - same errors in the same place.
Oct 30 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level...
6
by: C++ Shark | last post by:
Hi, which stl class is good for creating search trees? I am looking for something flexible, that allows me to search for a required state (of matrices, graphs, etc.) quickly. thanks in...
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
3
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
8
by: Mike - EMAIL IGNORED | last post by:
I have a class that may or may not be inherited. Its constructor calls a function that should be called only if the class is not inherited. Is there a way to tell, other than simply passing a...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
6
by: wojtas | last post by:
Hello I'm trying to create application with as much as possible methods derived from base class. I use two base classes "Document" and "DocumentItem", each document has collection of...
2
by: parasuram | last post by:
Hi friends ............. this is a question regarding the data structures trees Pleas post it if possible with in 2 days I will thankful if some body could help doing this. Operating...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.