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

j2e*_*nue 5 java tree data-structures

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

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

              ]
            },

  ]
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

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

有内置的数据结构吗?我在用Java编码。我想我还需要按某种顺序排序的节点,也许是AZ。

到目前为止,我有这个:

class Node{
 String label;
 List<Node> children;
}
Run Code Online (Sandbox Code Playgroud)

但是那怎么搜索呢?还有更好的数据结构吗?我不想在搜索过程中遍历所有节点,有没有办法对树进行排序,所以我不必这样做?我现在怎么拥有它,我必须遍历所有孩子。有没有一种方法可以按字母顺序排序,或者可以使查找更快的某种排序?

Bri*_*isk 4

为此你需要两件事:

在您的 Node 对象中,还有对父节点的引用:

class Node{
    String label;
    List<Node> children;
    Node parent;
}
Run Code Online (Sandbox Code Playgroud)

创建一个将标签映射到节点的 HashMap:

HashMap<String, Node> labelsToNodes;
Run Code Online (Sandbox Code Playgroud)

然后通过HashMap中的get()方法进行查找。您可以通过重复获取父节点来获取类别列表。如果您想要此代码,请告诉我,我会添加它(我现在时间不够)。

  • 正如我在回答中提到的,如果您必须多次处理相同的标签,您可能需要在 HashMap 中使用“List&lt;Node&gt;”。 (3认同)