Difference between binary search and binary search tree?

前端 未结 4 1204
攒了一身酷
攒了一身酷 2021-01-30 07:24

What is the difference between binary search and binary search tree?

Are they the same? Reading the internet it seems the second is only for trees (up to 2 children node

4条回答
  •  失恋的感觉
    2021-01-30 07:47

    For those who came here to quickly check which one to use. In addition to the answers,posted above, I would like to add complexities with respect to the operations for both of these techniques.

    Binary search Tree:

    Search: θ(log(n)), Worst case (O(n)) for unbalanced BST,

    Insert of node: θ(log(n)) , Worst case (O(n)) for unbalanced BST

    Deletion of node: θ(log(n)), , Worst case (O(n)) for unbalanced BST

    Balanced Binary search Tree:

    Search: log(n),

    Insert of node: O(log(n))

    Deletion of node: O(log(n))

    Binary Search on sorted array:

    Search: O(log(n)) But,

    Insertion of node: Not possible if array is statically allocated and already full. Otherwise O(n) ( O(n) for moving larger items of array to their adjacent right)

    Deletion of node: O(log(n)) + O(n). (So it would be O(log(n)) for finding position of deletion + O(n) for moving larger items of array to their adjacent left)

    So based on your requirements, you can choose whether you need quick inserts and deletes. If you don't need these, then keeping things in sorted array will work for you, as array will take less memory compared to tree

提交回复
热议问题