【发布时间】:2017-07-09 07:41:35
【问题描述】:
我正在为我拥有的某些类别和子类别寻找实现/数据结构。我正在考虑使用搜索树,但不确定如何开始实施。
让我向您展示数据的样子。它实际上是作为后端的 json 结构出现的,但它看起来像这样:
[
{
"id": 94,
"category_name": "New In", //this is the category category_name
"description": "",
"children": [ //this is the category children which can also be a sub-category
{
"id": 322,
"category_name": "New Studio",
"description": "Chic, sophisticated and polished with a classic edge."
},
{
"id": 365,
"category_name": "New Soho",
"description": "Fresh, eclectic, and trendy. Oozes effortless cool."
},
{
"id": 809,
"category_name": "Summer Collection",
"description": "Your ultimate summer look"
}
]
},
{
"id": 12,
"category_name": "Clothes",
"description": "",
"children": [
{
"id": 22,
"category_name": "All Clothes",
"description": ""
},
{
"id": 63,
"category_name": "Tops",
"description": "",
"children": [
{
"id": 5,
"category_name": "All Tops",
"description": ""
}
]
},
{
"id": 641,
"category_name": "Accessories",
"description": "",
"children": [
{
"id": 61,
"category_name": "All Accessories",
"description": ""
},
{
"id": 622,
"category_name": "Jewelry",
"description": "",
"children": [ // here is an example of a child that is a sub-category also
{
"id": 52,
"category_name": "All Jewelry",
"description": ""
},
{
"id": 68,
"name": "Necklaces",
"description": ""
},
{
"id": 69,
"name": "Bracelets",
"description": ""
},
]
},
]
所以如果我必须把它画出来,它会看起来像这样:
所以我希望能够找到任何东西的路径。因此,例如,如果我想搜索项链,那么我除了想要一条项链来获取路径:类别/配饰/珠宝/项链
是否有内置的数据结构?我正在用java编码。我想我还需要按某种顺序排序的节点,也许是 A-Z。
到目前为止我有这个:
class Node{
String label;
List<Node> children;
}
那么如何搜索呢?有没有更好的数据结构?我不想在搜索过程中遍历所有节点,有没有办法对树进行排序,所以我不必这样做?我现在是如何拥有它的,我必须遍历所有的孩子。有没有一种方法可以按字母顺序排序,或者某种可以加快查找速度的排序?
【问题讨论】:
-
你可以使用这个API:sourceforge.net/p/treeds4j.
标签: java data-structures tree