一个用于存储具有不同搜索参数对象的单一数据结构。

huangapple go评论75阅读模式
英文:

A single data structure to store object that has different search parameters

问题

所以我有一个对象列表,比如一个包含诸如 usernameroll numberstandarddivision 等变量的学生列表。

我正在寻找一种能够存储学生对象并且还允许在上述任何变量中进行搜索的数据结构。时间复杂度是关键部分,因为数据上将会进行成千上万次的搜索。

例如:如果我使用二叉树,我可以获取任何变量(比如 username)的哈希码,然后从中获取一个索引,并将学生对象存储在二叉树中。然而,这有一个限制,就是如果我想要根据 standard 搜索学生,这是不可能的,因为在二叉树中存储学生的索引是从 username 生成的。

是否有任何数据结构可以允许一次存储学生,并且还允许根据不同的变量搜索学生。或者是否有任何混合型数据结构可以满足这个需求。

我希望在最好的情况下实现 O(1) 的时间复杂度,否则是 O(log n)

英文:

So I have a list of objects, say a list of Students with variables like username, roll number, standard, division, etc.

I am looking out for a data structure that can store student objects and also allows search on any of the above variables. The time complexity is the key part here as there will be thousands of the searches done on the data.

Ex: If I use Binary Tree, then I can take a hashcode of the any variable say, username and then take an index out of it and store the student object in the Binary Tree. However, this has a limitation, as say if I want to search the student on the standard then it is not possible as the index at which the Student is stored in the Binary Tree was generated from username.

Is there any data structure, that can allow storing student once and also allows searching student on the different variables. OR Any hybrid data structure that can be created to fulfill the requirement.

I'd like to achieve a time complexity of O(1) at best else O(log n).

答案1

得分: 1

如果您想查找具有特定“学号”或特定“标准”的所有学生,则可以维护多个学生哈希映射:

HashMap<标准, 学生列表>
HashMap<学号, 学生列表>
HashMap<用户名, 学生>(假设用户名是唯一的)

对于一些仅为整数的字段(例如学号、标准),您可以使用数组而不是HashMap。

但是,如果您想支持查询,例如“查找所有标准为5的学生,其学号在5和10之间”,那么您将不得不在两个哈希映射中进行查找并交集值。

如果您希望这些操作快速执行,[k-d树](https://en.wikipedia.org/wiki/K-d_tree)可能会有所帮助。
英文:

If you want to find all students that have a particular roll number or particular standard, then you could maintain a many hashmaps to students:

HashMap<Standard, List[Student]>
HashMap<RollNumber, List[Student]>
HashMap<Username, Student>        (assuming usernames are unique)

For some fields (e.g. roll number, standard) that are just integers, you could use an array instead of a HashMap.

However, if you want to find to support queries like "find all students in a standard 5 whose roll numbers are between 5 and 10", then you'd have to do a lookup in two hashmaps and intersect the values.

If you want these operations to be quick, k-d trees might help.

答案2

得分: 0

如果我要做这个,我会创建一个Student数组,其中每个Student的索引都将是它的ID。

然后,我会创建一个HashMap<String, Integer>,将username映射到一个ID,另一个HashMap<Integer, Integer>roll number映射到一个ID,依此类推。

英文:

If I were to do this, I would create an array of Students, where the index of each Student would be its ID.

Then, I would create a HashMap&lt;String, Integer&gt; to map the username to an ID, another HashMap&lt;Integer, Integer&gt; to map roll number to an ID, etc.

答案3

得分: 0

考虑到用户名,每个学生和班级的学号都是唯一的,而班级可以由多名学生共享。

您可以按照以下方式来解决,以实现O(1)/O(log(N))/O(N)的时间复杂度,具体取决于情况。

首先定义类结构:

class Student {

  // 考虑所有变量为字符串。您可以根据您的用例进行更改。

  String username;

  String rollNumber;

  String standard;

  String division;

}

让我们创建一些映射来存储这些学生:

Map<String, Student> usernameMap; // 用户名到对象的映射

Map<String, Student> rollNumberMap; // 学号到对象的映射

每当创建任何学生对象时,都将其添加到这些映射中。

如果您想要查找特定标准/班级中的所有学生,

您还可以为标准和班级保持另一个映射。

类似于:

Map<String, Set<String>> standardMap; // 标准到学生用户名/学号集合的映射。

您可以类似地为班级创建映射。

英文:

Considering username, roll numbers are unique for every student and standard, divisions can be shared by multiple students.

You can work out a solution in fallowing manner to achieve the O(1)/O(log(N))/O(N) depending upon the cases.

Lets define the class structure first :

class Student{

  // Considering all the variables to be string. You can change them according to your use cases.

  String username;

  String rollNumber;

  String standard;

  String division;

}

Lets create some maps to store these students

Map&lt;String, Student&gt; usernameMap; // username to object map

Map&lt;String, Student&gt; rollNumberMap; // roll number to object map

Whenever any Student objectis created you add those respective Maps.

Incase you want to find out all the Students in any given standard/division,

you can keep the another mapping for standards and divisions also.

Something like ->

Map&lt;String, Set&lt;String&gt;&gt; standardMap; // standard to set of student&#39;s username/rollnumber.

you can create similarly map for divisions.

huangapple
  • 本文由 发表于 2020年7月27日 01:01:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63103175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定