getDepth of a subtree issue

I am trying to get the depth of a node in a subtree; however, I am getting a lot of errors with my code I was wondering what I did wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   int getDepth(const T& value, AVLNode* node) const {
 55                 // CODE HERE
 56                 int depth = 0;
 57                 if(node == nullptr)
 58                         return -1;
 59                 T* temp = new T;
 60                 temp = node->data;
 61
 62                 if(temp->compare(value)){ 
                     getDepth(node->leftChild->data,* node);
 63                         depth++;
 64                 }
 65                 else{
 66                         getDepth( node->rightChild->data,* node);
 67                         depth++;
 68                 }
 69
 70                 return depth; // PLACEHOLDER
 71         }
What are the errors that you're getting? Are they compiler errors or is it that you're not getting expected output?
Think of what you should pass to the recursive calls. The first parameter should stay the same the whole time right? It is the second one that should change. Also you should make use of the returned integer value. I disagree with returning -1 for an empty tree; I would return 0. After all an empty tree is still a tree just empty :)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
AVLTree.h:54:6: note: int AVLTree<T>::getDepth(const T&, AVLTree<T>::AVLNode*) const [with T = DoubleData]
  int getDepth(const T& value, AVLNode* node) const {
      ^
AVLTree.h:54:6: note:   candidate expects 2 arguments, 1 provided
AVLTree.h:275:6: note: int AVLTree<T>::getDepth() const [with T = DoubleData]
  int getDepth() const {
      ^
AVLTree.h:275:6: note:   candidate expects 0 arguments, 1 provided
AVLTree.h:285:6: note: int AVLTree<T>::getDepth(T&) const [with T = DoubleData]
  int getDepth(T& value) const {
      ^
AVLTree.h:285:6: note:   no known conversion for argument 1 from ‘DoubleData*’ to ‘DoubleData&’
AVLTree.h:292:6: note: int AVLTree<T>::getDepth(T&&) const [with T = DoubleData]
  int getDepth(T&& value) const {
      ^
AVLTree.h:292:6: note:   no known conversion for argument 1 from ‘DoubleData*’ to ‘DoubleData&&’
AVLTree.h:66:36: error: no matching function for call to ‘AVLTree<DoubleData>::getDepth(DoubleData*&) const’
    getDepth( node->rightChild->data);
                                    ^
AVLTree.h:66:36: note: candidates are:
AVLTree.h:54:6: note: int AVLTree<T>::getDepth(const T&, AVLTree<T>::AVLNode*) const [with T = DoubleData]
  int getDepth(const T& value, AVLNode* node) const {
      ^
AVLTree.h:54:6: note:   candidate expects 2 arguments, 1 provided
AVLTree.h:275:6: note: int AVLTree<T>::getDepth() const [with T = DoubleData]
  int getDepth() const {
      ^
AVLTree.h:275:6: note:   candidate expects 0 arguments, 1 provided
AVLTree.h:285:6: note: int AVLTree<T>::getDepth(T&) const [with T = DoubleData]
  int getDepth(T& value) const {
      ^
AVLTree.h:285:6: note:   no known conversion for argument 1 from ‘DoubleData*’ to ‘DoubleData&’
AVLTree.h:292:6: note: int AVLTree<T>::getDepth(T&&) const [with T = DoubleData]
  int getDepth(T&& value) const {
keene I think that worked.
I think you are missing AVLTree<T>:: in front of getDepth
You don't need that if it is defined inside the AVLTree class.

There are errors near lines 272, 285, 292 with how you are calling getDepth ne555 is right

There are still logic errors that I mentioned above.
Last edited on
> T* temp = new T;
http://www.cplusplus.com/forum/general/138037/

> I am getting a lot of errors
I'm not going to bother my mind-reading cat.

¿compile errors? Your snip is too small and references things that we don't know about (an AVLNode class, by instance), so you must post the compiler messages.
¿runtime error? (crash) again, we cannot compile that snip, much less run it. Get a debugger and identify the line where is crashing. You may also use tools like valgrind to discover invalid memory access.
¿wrong output? show expected and erroneous output, and also and example input.

Provide a testcase http://www.eelis.net/iso-c++/testcase.xhtml


Looking at your code, if the node is invalid you return -1.
If not, you set a `depth' variable to 0 and then increment it once (any path will do that) and return it. So you're simply returning 1.

Every you call the `getDepth()' function another `depth' variable is created, so it doesn't matter what you do to one of them, the others are not affected. Yes, they have the same name but they are not the same variable.
Well, I should have refreshed the page.

> There are errors near lines 272, 285, 292 with how you are calling getDepth
No, the only error is on line 66
1
2
AVLTree.h:66:36: error: no matching function for call to ‘AVLTree<DoubleData>::getDepth(DoubleData*&) const’
    getDepth( node->rightChild->data);
But your code in the first post has getDepth( node->rightChild->data,* node);
so you are compiling another piece of code (perhaps project misconfiguration or forgot to save the file)


The other are notes, they show the available member function that the compiler think that you may want to have used (because it couldn't find the one that you intend to call)
Topic archived. No new replies allowed.