The advantage of a binary search tree over a vector in C ++

What is the use of a binary search tree data structure if a vector (in sorted order) can support inserting, deleting, and searching in log (n) time (using binary search)?

+4
source share
3 answers

The main advantage of the tree is that the insertion and deletion in the vector is not O (log (n)) - it is O (n). (They take log (n) comparisons, but n moves.)

The advantage of the vector is that the constant factor can be extremely in their favor (because they are usually much more cache friendly, and misses in the cache can cost you 100% performance).

,

  • .
  • , .

,

  • .
  • .

... , O (1) + ( O (n) , , ).

+6

BST, /. , BST. , , .

, /, , , .

+2

O (log (n)). BST vs vector, :

BST "node", node . , ( ) . , , , , BST , .

0

Source: https://habr.com/ru/post/1620266/


All Articles