简体   繁体   中英

Cost search operation in a Binary tree?

How much does it cost the search operation in a Binary tree? Is it 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.

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)

Worst Case: O(n)

You can check out for balanced trees (AVL, Red Black) if you need better (logarithmic) worst case running complexities.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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