how to sort data at the time of adding it, not later?

后端 未结 4 1883
醉话见心
醉话见心 2021-01-21 18:29

I am new to algorithms so please forgive me if this sounds basic or stupid.

I want to know this : instead of adding data into some kind of list and then performing a sor

4条回答
  •  终归单人心
    2021-01-21 18:46

    If you use a binary search tree instead of an array, the sorting would happen "automatically", because it's already done by the insert method of the nodes. So a binary tree is always sorted, and it's easy to traverse. The only problem is that when you have already (more or less) sorted data, the tree becomes inbalanced (which is where red-black-trees and other variations come into play).

提交回复
热议问题