ProgrammingVery hard

A list of records must be kept sorted while new records arrive one at a time. Which structure gives the best total cost for 10^5 inserts and 10^5 searches?

  1. A. A linked list with a linear search per insertion, about 10^10 steps
  2. B. A full re-sort after every insert, about 10^10 steps
  3. C. A balanced binary search tree, with O(log n) insert and search
  4. D. An unsorted array with a full scan per search, about 10^10 steps

Correct answer: C. A balanced binary search tree, with O(log n) insert and search

A balanced tree gives O(log n) for both operations. The other options cost about 10^10 steps at this size.

More C Programming questions

Exams that ask this topic