【发布时间】:2011-09-26 17:26:06
【问题描述】:
我读过二叉搜索树,如果它是具有 n 个节点的完整树(除叶节点之外的所有节点都有两个子节点),那么任何路径都不能有超过 1+log n 个节点。
这是我做的计算...你能告诉我我哪里出错了...
the first level of bst has only one node(i.e. the root)-->2^0
the second level have 2 nodes(the children of root)---->2^1
the third level has 2^3=8 nodes
.
.
the (x+1)th level has 2^x nodes
so the total number of nodes =n = 2^0 +2^1 +2^2 +...+2^x = 2^(x+1)-1
so, x=log(n+1)-1
now as it is a 'complete' tree...the longest path(which has most no of nodes)=x
and so the nodes experienced in this path is x+1= log(n+1)
那么1+log n这个数字是怎么来的……?
【问题讨论】:
-
你对路径的定义是什么?您将树视为有向(边仅从父节点到子节点)还是无向(边“双向”)图?
-
@Philippe...这是一个无向图
-
OK... log(n+1) - 1 bound 似乎对应于从 root 到任何节点的路径的最大长度。跨度>
标签: data-structures binary-tree binary-search-tree