What is the formula for the minimum number of nodes in a red-black tree of height h?

守給你的承諾、 提交于 2021-02-16 20:11:28

问题


I have read that it is log(n+1) <= h <= 2*log(n+1) where log is in base 2. However, when trying this out on a few known minimum heights, it does not always work out.

So far I know that:

For for h = 1, minimum # of nodes = 2.

For h = 2, minimum nodes = 4.

For h = 3, minimum nodes = 10.

However these were done purely by tracing it out using the rules for red-black trees.

Should I be taking note of the black-height when trying to find this or is my approach/calculations just completely wrong?


回答1:


We can find the minimal node count recursively.
count_minimum_node will return the number of nodes to achieve height h.

int count_node(int h) 
{
    int sum = h;
    for(int i=1; i<=h-2; i++) sum += count_node(i);
    return sum;
}

int count_minimum_node(int h) { return count_node(h+1); }


来源:https://stackoverflow.com/questions/42132204/what-is-the-formula-for-the-minimum-number-of-nodes-in-a-red-black-tree-of-heigh

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