[英]Why is add in unbalanced Binary Search Tree O(n)?
This is the implementation of add in Binary Search Tree from BST Add 这是BST Add中Binary Search Tree中add的实现
private IntTreeNode add(IntTreeNode root, int value) {
if (root == null) {
root = new IntTreeNode(value);
} else if (value <= root.data) {
root.left = add(root.left, value);
} else {
root.right = add(root.right, value);
}
return root;
}
I understand why this runs in O(log n). 我知道为什么它在O(log n)中运行。 Here's how I analyze it.
这就是我的分析方式。 We have a tree size of n.
我们的树大小为n。 How many cuts of 2, or half cut, will reduce this tree down to a size of 1. So we have the expression n(1/2)^x = 1 where the 1/2 represents each half cut.
多少个2的切口或半切口将把这棵树缩小到1的大小。因此,我们有n(1/2)^ x = 1的表达式,其中1/2表示每个半切口。 Solving this for x, we have log2(x) so the logn comes from search.
为x解决这个问题,我们有log2(x),所以logn来自搜索。
Here is a lecture slide from Heap that discusses runtime for an unbalanced binary search. 这是Heap的演讲幻灯片,讨论了不平衡二进制搜索的运行时。
My question is even if the binary search tree is unbalanced, wouldn't the same strategy work for analyzing the runtime of add? 我的问题是,即使二进制搜索树不平衡,使用相同的策略来分析添加的运行时间也行不通吗? How many cuts you have to make.
您必须进行多少次切割。 Wouldn't the runtime still be O(log n), not O(n)?
运行时是否仍为O(log n),而不是O(n)? If so, can someone show the math of why it would be O(n)?
如果是这样,有人可以说明为什么它是O(n)的数学方法吗?
With an unbalanced tree: 对于不平衡的树:
1
\
2
\
3
\
4
\
5
\
...
Your intuition of cutting the tree in half with each operation no longer applies. 您每次操作将树切成两半的直觉不再适用。 This unbalanced tree is the worst case of an unbalanced binary search tree.
该不平衡树是不平衡二进制搜索树的最坏情况。 To search for
10
at the bottom of the list, you must make 10
operations, one for each element in the tree. 要在列表底部搜索
10
,必须执行10
操作,每个操作针对树中的每个元素。 That is why a search operation for an unbalanced binary search tree is O (n) - this unbalanced binary search tree is equivalent to a linked list. 这就是为什么不平衡的二进制搜索树的搜索操作为O (n)的原因-该不平衡的二进制搜索树等效于链接列表。 Each operation doesn't cut off half the tree -- just the one node you've already visited.
每个操作都不会砍掉一半的树-只是您已经访问过的一个节点。
That is why specialized versions of binary search trees, such as red-black trees and AVL trees are important: they maintain trees that are balanced well enough so that all operations - search, insert, delete -- are still O (log n). 这就是为什么二进制搜索树(例如红黑树和AVL树)的特殊版本很重要的原因:它们维护的树要平衡得足够好,以便所有操作(搜索,插入,删除)仍为O (log n)。
The O(n)
situation in a BST happens when you have either the minimum or the maximum at the top, effectively turning your BST into a linked list. BST的
O(n)
情况发生在顶部有最小值或最大值时,实际上将BST变成了链表。 Suppose you added elements as: 1, 2, 3, 4, 5
, generating your BST, which will be a linked list due to every element having only a right child
. 假设您添加的元素为:
1, 2, 3, 4, 5
生成您的BST,由于每个元素只有一个right child
,所以它将成为一个链表。 Adding 6 would have to go down right on every single node, going through all the elements, hence making the asymptotic complexity of add O(n)
加法6必须在每个节点上向下遍历所有元素,因此使加法
O(n)
的渐近复杂度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.