简体   繁体   English

二叉树中的成本搜索操作?

[英]Cost search operation in a Binary tree?

How much does it cost the search operation in a Binary tree? 在二叉树中进行搜索操作需要花费多少? Is it O(n)? 是O(n)吗?

        Average     Worst case
Space   O(n)        O(n)
Search  O(log n)    O(n)
Insert  O(log n)    O(n)
Delete  O(log n)    O(n)

Yes, it is O(n), since it is Binary Tree and NOT binary search tree. 是的,它是O(n),因为它是二叉树而不是二叉搜索树。

Since it is not possible to judge to which way (Left or Right) to branch in a "Binary tree", we have to search the entire tree in the worst case. 由于无法判断要在“二叉树”中分支的方式(左或右),因此在最坏的情况下,我们必须搜索整个树。

Average Case for searching an element: O(log n) 搜索元素的平均大小写:O(log n)

Worst Case: O(n) 最坏的情况:O(n)

You can check out for balanced trees (AVL, Red Black) if you need better (logarithmic) worst case running complexities. 如果您需要更好的(对数)最坏情况下的运行复杂性,则可以检查平衡树(AVL,红黑)。

是,它将是O(n),因为在该树中没有像二进制搜索树那样的排序条件,因此您必须遍历整个树,就像数组一样

根据Robert Sedgewick的《算法分析入门》一书,如果此二叉树是通过大小为N的随机排列构造的,则平均成功搜索为2H_N − 3 + 2H_N / N = 2ln(N)+ O( 1),搜索失败的平均值为2H_ {N + 1}-2 = 2ln(N)+ O(1)。

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

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