是否有树结构或算法可以在树中的各个级别之间进行洗牌?
我有一个我认为很有趣的问题。
基本上,我有一个项目列表,其中每个项目都有一组固定的元数据,具有不同的值。
例如:
- 项目 1:{Type = "Text",作者 = "User A",编辑日期 = "03/03/2003"}
- 项目 2:{Type = "Table",作者 = "User A",编辑日期= "04/05/2006"}
- 项目 3: {类型 = "图像", 作者 = "用户 B", 编辑日期 = "05/05/2005"}
- 项目 4: {类型 = “Text”,作者=“User B”,编辑日期=“05/07/2007”}
现在,按照目前的情况,该项目列表已被展平并呈现在表格中。
然而,我们希望找到一种方法,允许用户在树中浏览它,但增加了灵活性,他们可以“旋转”每个元数据标签在树中出现的顺序。
因此,最初它可能看起来像:
Items
+ Table
+ User A
+ 04/05/2006
-> Item 2
-> Item 2
-> Item 2
+ Text
+ User A
+ 03/03/2003
-> Item 1
-> Item 1
+ User B
+ 05/07/2007
-> Item 4
-> Item 4
-> Item 1
-> Item 4
+ Image
..
但是,假设用户想要翻转它并查看与特定用户相关的所有项目:
Items
+ User A
+ Text
+ Table
-> Item 1
-> Item 2
+ User B
+ Image
+ Text
-> Item 3
-> Item 4
等等。
我希望这是有道理的。
因此,我想知道是否有最佳实践方法可以以低成本实现这一目标?每个“翻转/洗牌/旋转”的结果都很好地表示在树中,因此显然第一个想法是当用户请求更改表示时,可以根据需要生成项目列表的新树。然而,我希望也许有更好的方法,简单地旋转一棵树等。
此外,如果后端只是简单地返回一个简单的列表,那么这是否可以在用户浏览器上的 JavaScript 中以低成本计算完成项目?
非常感谢&亲切的问候,
杰米
I have what I think is an interesting problem.
Basically, I have a list of Items, where each Item has a fixed set of meta-data, of different values.
For example:
- Item 1: {Type = "Text", Author = "User A", Edited Date = "03/03/2003"}
- Item 2: {Type = "Table", Author = "User A", Edited Date = "04/05/2006"}
- Item 3: {Type = "Image", Author = "User B", Edited Date = "05/05/2005"}
- Item 4: {Type = "Text", Author = "User B", Edited Date = "05/07/2007"}
Now, as it stands, that list of items is flattened and presented in a table.
However, we would like to find a way to allow the users to browse it in a tree, but with the added flexibility that they can 'pivot' the order each of the meta-data tags appears in the tree.
So, initially it might look like:
Items
+ Table
+ User A
+ 04/05/2006
-> Item 2
-> Item 2
-> Item 2
+ Text
+ User A
+ 03/03/2003
-> Item 1
-> Item 1
+ User B
+ 05/07/2007
-> Item 4
-> Item 4
-> Item 1
-> Item 4
+ Image
..
However, suppose instead, a user wants to flip it round and view all items related to a particular user:
Items
+ User A
+ Text
+ Table
-> Item 1
-> Item 2
+ User B
+ Image
+ Text
-> Item 3
-> Item 4
And so on.
I hope that makes sense.
So, what I'm wondering therefore, is if there is a best practice approach to achieving this at low cost? The result of each 'flip/shuffle/pivot' is nicely represented in a tree, so obviously the first thought is that when a user requests to change the representation, a new tree could be generated of the list of items as required. However, I was hoping perhaps there may be a better way, simply rotating a single tree etc.
Also, is this something that could be done computationally cheaply in JavaScript on the user's browser, if the backend were to just simply return a flat list of items?
Many thanks & kind regards,
Jamie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要以树结构呈现元素,但具有可变的树深度和变化的树分支:我怀疑树结构实际上是您想要的。
我认为你应该考虑世界是平的(就像你的表中一样)。 javascript 数据库可以提供帮助(有http://taffydb.com/)
仍然考虑到世界是平的,你可以还创建一个返回字符串的签名函数
,然后使用此签名作为键将对象存储在一个简单的字典中。
然后,您可以对键执行正则表达式匹配来获取您想要的对象。
首先,编辑签名函数,如果相应的项属性未定义,则返回“([^separator]+)”。
我不知道这是否比浏览所有元素更快。
You want to present elements in a tree structure, but with a variable tree depth and changing tree branching: I doubt a tree structure is actualy what you want.
I think you should consider instead that the world is flat (like in your table). A javascript database could help (there is http://taffydb.com/)
Still considering the world is flat, you could also create a signature function that returns a string
Then you store your objects in a simple dictionary using this signature as the key.
And then, you can perform a regexp match on the keys to get the objects you want.
First, edit the signature function to return "([^separator]+)" if the corresponding item property is undefined.
I have no idea whether this is faster than browsing all elements though.
我解决这个问题的方法是定义一个看起来像这样的列表项:
这是 C# 代码,但这个想法应该适用于任何支持对象的语言。现在你的列表只需要支持一种类型的列表,那就是
Item
列表,所以你需要做的就是找到一种方法将你的数据转换为这样的列表。They way I would go around to solve this is to define a list item that lookgs something like this:
This is c#-code, but the idea should be applicable in any language that supports objects. Now your list only need to support one type of list, and that is a list of
Item
so all you need to do is find a way to transform your data to such a list.