简体   繁体   English

证明平衡二叉搜索树的高度为 log(n)

[英]Proof that the height of a balanced binary-search tree is log(n)

The binary-search algorithm takes log(n) time, because of the fact that the height of the tree (with n nodes) would be log(n).二进制搜索算法需要 log(n) 时间,因为树的高度(具有 n 个节点)将是 log(n)。

How would you prove this?你将如何证明这一点?

Now here I am not giving mathematical proof.现在在这里我不给出数学证明。 Try to understand the problem using log to the base 2. Log 2 is the normal meaning of log in computer science.尝试使用 log to base 2 来理解问题。 Log 2是计算机科学中 log 的正常含义。

First, understand it is binary logarithm (log 2 n) (logarithm to the base 2).首先,了解它是二进制对数(log 2 n)(以 2 为底的对数)。 For example,例如,

  • the binary logarithm of 1 is 0 1的二进制对数为0
  • the binary logarithm of 2 is 1 2的二进制对数是1
  • the binary logarithm of 3 is 1 3的二进制对数是1
  • the binary logarithm of 4 is 2 4的二进制对数是2
  • the binary logarithm of 5, 6, 7 is 2 5、6、7的二进制对数是2
  • the binary logarithm of 8-15 is 3 8-15的二进制对数是3
  • the binary logarithm of 16-31 is 4 and so on. 16-31的二进制对数为4,以此类推。

For each height the number of nodes in a fully balanced tree are对于每个高度,完全平衡树中的节点数为

Height  Nodes  Log calculation
      0        1      log21 = 0
      1        3      log23 = 1
      2        7      log27 = 2
      3       15      log215 = 3

Consider a balanced tree with between 8 and 15 nodes (any number, let's say 10).考虑一个具有 8 到 15 个节点(任意数量,比如说 10)的平衡树。 It is always going to be height 3 because log 2 of any number from 8 to 15 is 3.它总是高度 3,因为从 8 到 15 的任何数字的 log 2都是 3。

In a balanced binary tree the size of the problem to be solved is halved with every iteration.在平衡二叉树中,每次迭代要解决的问题的大小减半。 Thus roughly log 2 n iterations are needed to obtain a problem of size 1.因此,大约需要 log 2 n 次迭代才能获得大小为 1 的问题。

I hope this helps.我希望这有帮助。

Let's assume at first that the tree is complete - it has 2^N leaf nodes.让我们首先假设树是完整的——它有 2^N 个叶子节点。 We try to prove that you need N recursive steps for a binary search.我们试图证明你需要 N 个递归步骤来进行二分搜索。

With each recursion step you cut the number of candidate leaf nodes exactly by half (because our tree is complete).在每个递归步骤中,您将候选叶节点的数量精确地减少了一半(因为我们的树是完整的)。 This means that after N halving operations there is exactly one candidate node left.这意味着在 N 次减半操作之后,只剩下一个候选节点。

As each recursion step in our binary search algorithm corresponds to exactly one height level the height is exactly N.由于我们的二进制搜索算法中的每个递归步骤都对应于一个高度级别,因此高度正好是 N。

Generalization to all balanced binary trees: If the tree has less nodes than 2^N we for sure don't need more halvings.推广到所有平衡二叉树:如果树的节点少于 2^N,我们肯定不需要更多的减半。 We might need less or the same amount but never more.我们可能需要更少或相同的数量,但永远不会更多。

Assuming that we have a complete tree to work with, we can say that at depth k, there are 2 k nodes.假设我们有一个完整的树可以使用,我们可以说在深度k处有 2k 个节点。 You can prove this using simple induction, based on the intuition that adding an extra level to the tree will increase the number of nodes in the entire tree by the number of nodes that were in the previous level times two.您可以使用简单的归纳来证明这一点,基于这样的直觉,即向树添加额外的级别将使整个树中的节点数增加前一层中的节点数乘以 2。

The height k of the tree is log(N), where N is the number of nodes.树的高度 k 是 log(N),其中 N 是节点数。 This can be stated as这可以表述为

log 2 (N) = k,日志2 (N) = k,

and it is equivalent to它相当于

N = 2 k N = 2

To understand this, here's an example:为了理解这一点,这里有一个例子:

16 = 2 4 => log 2 (16) = 4 16 = 2 4 => 对数2 (16) = 4

The height of the tree and the number of nodes are related exponentially .树的高度和节点的数量呈指数关系。 Taking the log of the number of nodes just allows you to work backwards to find the height.记录节点数量的日志只允许您向后工作以找到高度。

Just look up the rigorous proof in Knuth, Volume 3 - Searching and Sorting Algorithms ... He does it far more rigorously than anyone else I can think of.只需查看 Knuth, Volume 3 - Searching and Sorting Algorithms 中的严格证明……他比我能想到的任何其他人都严格得多。

http://en.wikipedia.org/wiki/The_Art_of_Computer_Programminghttp://en.wikipedia.org/wiki/The_Art_of_Computer_Programming

You can find it in any good Computer Science library and on the bookshelves of many (very) old geeks.你可以在任何好的计算机科学图书馆和许多(非常)老极客的书架上找到它。

Why is the height of a balanced binary tree equal to ceil(log 2 N) for N nodes?为什么平衡二叉树的高度等于 N 个节点的 ceil(log 2 N)?

w = width of base (maximum number of leaves) w = 基部宽度(最大叶数)

h = height of tree (maximum number of edges from root to leaf) h = 树的高度(从根到叶的最大边数)

Divide w by 2 (h times) to get to 1, which counts the single root node at top.将 w 除以 2(h 次)得到 1,它计算顶部的单个根节点。

N = w + w/2 + ... + 1 N = w + w/2 + ... + 1

N = 2 h + ... + 2 1 + 2 0 N = 2小时+ ... + 2 1 + 2 0

= (1-2 h+1 ) / (1-2) = 2 h+1 -1 = (1-2 h+1 ) / (1-2) = 2 h+1 -1

log 2 (N+1) = h+1日志2 (N+1) = h+1

Check: if N=1, h=0.检查:如果 N=1,h=0。 If h=1, N=3.如果 h=1,N=3。

This formula is for if the bottom level is full.此公式适用于底层是否已满。 N will not always be so great, but would still have the same height, h. N 并不总是那么大,但仍具有相同的高度 h。 So we must take the log's ceiling.所以我们必须采取日志的天花板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM