0

So I'm writing this height function of an avl tree class :

int avlTree::height(avlNode* tree) {
if (!tree) {
    throw invalidInput;
}
if (!tree->left && !tree->right) {
    return 0;
}
return max(height(tree->right), height(tree->left)) + 1;
}

But It feels like something is wrong, I thought of implementing it in such a way that in the beginning the tree will be checked for NULL and if it is, a 0 should be returned. But then I lose the possibiilty to check if the tree was a void pointer in the first place. Or is it fine that it'll return 0 even if it's a void pointer? what's more accepted?

thanks!

wannabe programmer
  • 653
  • 1
  • 9
  • 23
  • `return 0` looks OK in this case. Height of empty tree is 0. You may write another function which throws exception on NULL pointer, and calls recursive function. BTW, this may give you opportunity to write code that can be optimized to tail recursion. – Alex F Nov 25 '14 at 06:58
  • From [Wikipedia](http://en.wikipedia.org/wiki/Tree_%28data_structure%29): "Conventionally, an empty tree (tree with no nodes, if such are allowed) has depth and height −1." – Amadan Nov 25 '14 at 06:59
  • @AlexFarber, can you provide an example? – wannabe programmer Nov 25 '14 at 07:05
  • @Amadan, it feels like if I set it to return -1, it won't work correctly, from the recursion aspect. – wannabe programmer Nov 25 '14 at 07:06
  • Sure it will. The `-1` case does not recurse - it only happens if you pass in a rootless tree. Your base recursion case remains `0`. You will need to fix the conditions though; only recurse into branches that exist. – Amadan Nov 25 '14 at 07:18
  • Two functions is simple: one function tests for NULL and throws exception (or returns -1) and calls recursive function. Recursive function returns 0 on NULL pointer. Regarding tail recursion: read this http://stackoverflow.com/questions/2693683/tail-recursion-in-c The answers from nategoose and Potatoswatter – Alex F Nov 25 '14 at 07:51

1 Answers1

1

This is the code presented as I'm writing this answer:

int avlTree::height(avlNode* tree) {
if (!tree) {
    throw invalidInput;
}
if (!tree->left && !tree->right) {
    return 0;
}
return max(height(tree->right), height(tree->left)) + 1;
}

This will attempt to dereference a nullpointer when called with a pointer to a node whose left or right, but not both, are nullpointers.

So, the logic is ungood in the first place.

You might think about "or" versus "and", and such.


To check for valid arguments,

  • write a non-checking version, and
  • write a checking version that calls the first one.

Tip: you can use the free AStyle program to fix the indentation. Many editors also provide automatic formatting.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Can you provide an example that will check the legality of a tree without causing such problems? Didn't get the comment about identation and it's relevance to the question. – wannabe programmer Nov 25 '14 at 07:07
  • @wannabeprogrammer: regarding indentation, sloppiness in one area carries over to sloppiness in other areas, easily. c++ source code is not about communicating orders to the computer. it is about communicating intent to human readers. improper, unsystematic indentation is not commensurate with that main goal. you might as well program in assembly language. – Cheers and hth. - Alf Nov 25 '14 at 07:09
  • I took an assembly course (PDP 11 computer), hated it. I can't agree more about the code being readable! The reason it wasn't indented is the way I copied it to stack overflow (I'm a noob in this (amazing) website), rest assured, my (actual) code is fine from the indentation aspect. – wannabe programmer Nov 25 '14 at 07:27
  • @wannabeprogrammer: Oh, that brings back memories. Memory mapped registers. @ everywhere. I don't remember what for, any longer. Anyway, the PDP-11 was all the rage in its day. Later on they made smallish versions, named something L something 11, that could actually be carried around, like suitcases. Almost. So, I believe you about the code. Tip 1: it's a good idea to set editor to translate tabs to spaces, if you haven't already done so. Tip 2: in Visual Studio there's a command to translate tabs to spaces in selected text, but it must be manually installed in the menus. – Cheers and hth. - Alf Nov 25 '14 at 07:39
  • Thanks. Why is it preferred to space instead of use tab? Anyway I'll get the Astyle... Thanks for the help, can you add en example of what you suggested to the height func. problem? Thanks a bunch again – wannabe programmer Nov 25 '14 at 07:53
  • Default tab size in Windows programmer's editors is 4. Especially used by Microsoft. Default size in Unix-land is 8. Some printers of old defaulted to 10. Can really mess up the formatting when cooperating with others. E.g. Boost library code uses spaces exclusively (for this reason). I think re suggested solution, it's something you can do and learn by doing. The two bullet points. – Cheers and hth. - Alf Nov 25 '14 at 08:07