【问题标题】:Time analysis of binary search tree operations二叉搜索树操作的时间分析
【发布时间】: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


【解决方案1】:

简短的回答:完整(或完美)二叉树中的层数x 是log2(n+1),其中n 是节点数(或者,n = 2^(x-1))。具有x 级别的树的高度为x-1。从根到任何节点的最长路径包含x = log2(n+1) 节点(和x-1 边)。

现在因为 n+1 是 2 的幂,所以我们有 log2(n+1) = 1 + floor(log2(n))。换句话说,1 + log2(n) 是一个正确的上界,但绝不是整数。

我不清楚您计算中的x 是指高度还是层数。

【讨论】:

  • @philippie ...在我的计算中,我从根开始作为具有 2^0 个节点的第一级.....所以我的 (x+1) 级有 2^x 个节点。 ..x 是第 (x+1) 层的总高度...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2012-04-09
  • 1970-01-01
相关资源
最近更新 更多