Java-具有多个节点的树数据结构-如何有效搜索

j2emanue:

我正在为某些类别和子类别搜索实现/数据结构。我正在考虑使用搜索树,但不确定如何开始实施。

让我告诉您数据的样子。它从后端以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": ""
                },

              ]
            },

  ]

因此,如果我必须将其绘制出来,它将看起来像这样:

在此处输入图片说明

因此,我希望能够找到任何东西。因此,例如,如果我要搜索项链,那么我也希望获得一条项链以获取以下路径:类别/配件/珠宝/项链

Is there a built in data structure for this ? i am coding in java. I suppose i also need the nodes sorted in some kind of order maybe A-Z.

so far i have this:

class Node{
 String label;
 List<Node> children;
}

but then how to search ? and is there a better data structure ? I do not want to iterate over all the nodes during a search, is there a way to sort the tree so i dont have to do that ? How i have it now i'd have to iterate over all the children. is there a way to maybe sort alphaetically perhaps or some kind of sort that would make the lookup faster ?

Brian Risk :

You need two things for this:

In your Node object, have a reference to the parent node as well:

class Node{
    String label;
    List<Node> children;
    Node parent;
}

Create a HashMap that maps labels to the nodes:

HashMap<String, Node> labelsToNodes;

然后使用HashMap中的get()方法完成搜索。您可以通过重复获取父节点来获得类别列表。让我知道您是否想要此代码,然后我将其添加(我现在时间很紧)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

每个项目具有多个键的树数据结构

Java如何有效地在jar文件中搜索类?

如何遵循有效的Java的建议?

用Java搜索键值对的最快,最有效的方法?

哪种数据结构最适合Java,我如何有效地实现它?

如何进行有效的搜索

使数据结构成为线程安全(Java)的最有效方法

最有效的数据结构来表示Java中的线程注释?

使用操作的Java中的数据结构删除一个节点之后的所有节点

如何使树具有多种类型的节点,并且每个节点在Java中可以具有多个子节点

除二进制搜索树外,是否有任何有效的数据结构来表示Set

打印具有多个子节点的节点的数据[Java]

这个有效的Java代码如何?(混淆的Java)

如何创建自己的有效数据结构?

在树中搜索具有特定属性的节点并分配树的属性的有效方法

如何在具有3个表的记录的Java中创建数据结构?

REST:如何发送YAML有效负载(Java)

如何检查的Java日期类型的有效性?

如何有效处理java.net.ConnectException

如何在Java中有效使用嵌套循环?

如何显示Java中的有效日期数?

如何在Java中检查有效的URL?

如何在Java中获取有效的子列表?

如何用Java编写有效的Web服务

Java:如何有效检查空指针

如何在Java中删除(有效)最终数组

如何有效地编程我的Java Swing?

如何删除Java中的最低有效位?

如何确保我在Java中的矩形是有效的矩形