如何将数据以增加它的时间排序,不晚? [英] how to sort data at the time of adding it, not later?

查看:133
本文介绍了如何将数据以增加它的时间排序,不晚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的算法,所以请原谅我,如果这听起来很基本的或愚蠢的。

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 sort on the list, is there a method (data structure+algorithm) that lets me sort the data at the time of adding itself, or to put it another way, inserts the data in its proper place?

例如:如果我想加上'3'{1,5,6},而不是将它添加在开始或结束,然后排序名单,我想'3'追打'1'直接

eg: if I want to add '3' to {1,5,6}, instead of adding it at the start or end and then sorting the list, I want '3' to go after '1' "directly".

感谢

推荐答案

如果您使用的是二叉搜索树,而不是一个数组,排序会自动发生,因为它已经完成了节点的插入方法。因此,一个二叉树总是排序,很容易移动。唯一的问题是,当你已经(或多或少)排序的数据,树变成inbalanced(这是红黑树和其他变化发挥作用)。

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).

这篇关于如何将数据以增加它的时间排序,不晚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆