Depth and height of the subtree

What is the difference between the depth and height of a subtree? Could you give an example of an algorithm for finding the depth and height of a subtree?

Author: Nicolas Chabanovsky, 2010-12-12

2 answers

Well actually:

  • Subtree depth - the distance from the root of the tree to the root of the subtree

  • The height of the subtree is the length of the longest branch in this subtree; where the length of the branch of the subtree is the distance from the root of the subtree to the leaf of this branch; the leaf is respectively a node of the tree that has no descendants)

 5
Author: a_s, 2010-12-18 19:46:55

The height search algorithm.

int HeightOfTree(BinaryTreeNode * node)
{
    if(node == 0)
        return 0;
    int left, right;
    if (node->leftChild != NULL) {
        left = HeightOfTree(node->leftChild);
    }else 
        left = -1;
    if (node->rightChild != NULL) {
        right = HeightOfTree(node->rightChild);
    }else 
        right = -1;
    int max = left > right ? left : right;
    return max+1;

}

The depth-finding algorithm.

int DepthOfTree(BinaryTreeNode * node)
{
    if(node->parent == NULL)
        return 0;
    return 1+DepthOfTree(node->parent);       
}
 4
Author: Nicolas Chabanovsky, 2010-12-18 21:03:11