Red-Black Tree Height using Recursion

喜欢而已 提交于 2019-12-13 05:09:11

问题


I have these following methods to get the height of a red black tree and this works (I send the root). Now my question is, how is this working? I have drawn a tree and have tried following this step by step for each recursion call but I can't pull it off. I know the general idea of what the code is doing, which is going through all the leaves and comparing them but can anyone give a clear explanation on this?

int RedBlackTree::heightHelper(Node * n) const{
        if ( n == NULL ){
            return -1;
        }
        else{
            return max(heightHelper(n->left), heightHelper(n->right)) + 1;
        }
    }

int RedBlackTree::max(int x, int y) const{
    if (x >= y){
        return x;
    }
    else{
        return y;
    }
}

回答1:


Well, the general algorithm to find the height of any binary tree (whether a BST,AVL tree, Red Black,etc) is as follows

For the current node:
 if(node is NULL) return -1
 else
    h1=Height of your left child//A Recursive call
    h2=Height of your right child//A Recursive call
    Add 1 to max(h1,h2) to account for the current node
    return this value to parent.

An illustration to the above algorithm is as follows:

(Image courtesy Wikipedia.org)




回答2:


This code will return the height of any binary tree, not just a red-black tree. It works recursively.

I found this problem difficult to think about in the past, but if we imagine we have a function which returns the height of a sub-tree, we could easily use that to compute the height of a full tree. We do this by computing the height of each side, taking the max, and adding one.

The height of the tree either goes through the left or right branch, so we can take the max of those. Then we add 1 for the root.

Handle the base case of no tree (-1), and we're done.




回答3:


This is a basic recursion algorithm.

Start at the base case, if the root itself is null the height of tree is -1 as the tree does not exist.

Now imagine at any node what will be the height of the tree if this node were its root?

It would be simply the maximum of the height of left subtree or the right subtree (since you are trying to find the maximum possible height, so you have to take the greater of the 2) and add a 1 to it to incorporate the node itself.

That's it, once you follow this, you're done!




回答4:


As a recursive function, this computes the height of each child node, using that result to compute the height of the current node by adding + 1 to it. The height of any node is always the maximum height of the two children + 1. A single-node case is probably the easiest to understand, since it has a height of zero (0).

    A

Here the call stack looks like this:

height(A) = 
   max(height(A->left), height(A->right)) + 1

Since both left and right are null, both return (-1), and therefore this reduces to

height(A) = max (-1, -1) + 1;
height(A) = -1 + 1;
height(A) = 0

A slightly more complicated version

         A
    B         C
  D   E

The recursive calls we care about are:

height(A) = 
    max(height(B), height(C)) + 1

height(B) = 
    max(height(D), height(E)) + 1

The single nodes D, E, and C we already know from our first example have a height of zero (they have no children). therefore all of the above reduces to

height(A) = max( (max(0, 0) + 1), 0) + 1
height(A) = max(1, 0) + 1
height(A) = 1 + 1
height(A) = 2

I hope that makes at least a dent in the learning curve for you. Draw them out on paper with some sample trees to understand better if you still have doubts.



来源:https://stackoverflow.com/questions/20171402/red-black-tree-height-using-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!