1

I am currently implementing a Binary Search Tree. My teaching materials state that the depth of a tree is equal to the depth of the deepest leaf. The depth of a node itself is defined as the amount of edges from root to that node. However when I wanted to check if my implementation is correct on the internet, I found that many implementations are like this:

int Tree::Depth()
{
    if (!this)
    {
        return 0;
    }

    return max(this->left.Depth(), this->right.Depth()) + 1;
}

But using return 0 gives the amount of nodes instead of the amount of edges (amount of nodes = amount of edges + 1). Shouldn't it be return -1? Robert Sedgewick also seems to use -1:

/**
 * Returns the height of the BST (for debugging).
 *
 * @return the height of the BST (a 1-node tree has height 0)
 */
public int height() {
    return height(root);
}
private int height(Node x) {
    if (x == null) return -1;
    return 1 + Math.max(height(x.left), height(x.right));
}

Definition. The size of a tree is its number of nodes. The depth of a node in a tree is the number of links on the path from it to the root. The height of a tree is the maximum depth among its nodes.

~ Algorithms (4th Edition) by Robert Sedgewick and Kevin Wayne

An example of root having depth 0 is visible at page 490 in "Introduction to Algorithms" 3rd. edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein.

Can someone help me to clear the confusion?

Community
  • 1
  • 1
MartenBE
  • 744
  • 1
  • 5
  • 20
  • 1
    I guess it is just a matter of definition. – Henry Oct 27 '16 at 08:56
  • Yes, the height of an empty tree is usually defined as -1. – Ry- Oct 27 '16 at 08:57
  • 2
    The difference in definitions is unimportant. In any case, the only point to be made is that the depth "grows logarithmically" (or something similar), so +-1 means very little. – Ami Tavory Oct 27 '16 at 08:58
  • The C++ snippet relies on undefined behavior (dereferences null pointer), uses incorrect terminology (it calculates the height rather than depth), and is off-by-one (leaves have 'depth' of 1)... so don't take it seriously. Just as most of the code there 'in the wild'. – Yakov Galka Oct 27 '16 at 10:47
  • It does not dereference a null pointer because of the if(!this) function guard. The height of the tree is defined equal as the amount of edges between the root and the deepest node (or in other words, the depth of the tree). @ Ami I want to know which definition is correct @Ryan, yes it is odd (hence the confusion), but R. Sedgewick implements it this way. This is solvable by an extra if (!root) statement in the height() function. – MartenBE Oct 27 '16 at 11:49

1 Answers1

1

Sedgewick's implementation is the correct one.

The height of a node is the length of the longest downward path to a leaf from that node. The height of the root is the height of the tree.

So the height of a tree is all about the length of a path (measured in edges, not nodes). A tree that only contains a root node has a height of 0, and a tree that doesn't even have a root node (if that's allowed) is defined to have a height of -1.

Reference: Tree (data structure)

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880