Home  >  Article  >  Backend Development  >  How can you efficiently transform a list of path strings into a hierarchical tree structure without relying heavily on pointer manipulation and recursion?

How can you efficiently transform a list of path strings into a hierarchical tree structure without relying heavily on pointer manipulation and recursion?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-27 05:06:02282browse

How can you efficiently transform a list of path strings into a hierarchical tree structure without relying heavily on pointer manipulation and recursion?

Transforming Path Strings into Hierarchical Tree Structures

When presented with an array of path-like structures, constructing a corresponding hierarchical tree structure can be a challenging task, especially with limitations in pointer manipulation and recursion. To address this, an efficient solution is to utilize a method that iteratively adds nodes to the tree while searching for existing ones.

SOLUTION

The proposed solution employs a recursive function named AddToTree that takes a list of nodes (root) and a list of path components to be added (names). It iterates through the root nodes, checking if the first component of names (names[0]) already exists as a child. If it does not, a new node with names[0] as the name is appended to the root.

Next, the function recursively calls AddToTree on the children of the node that matches names[0]. This process continues until all components of names have been processed, resulting in a fully constructed tree structure.

CODE EXAMPLE

Below is the Go implementation of the AddToTree function:

<code class="go">func AddToTree(root []Node, names []string) []Node {
    if len(names) > 0 {
        var i int
        for i = 0; i < len(root); i++ {
            if root[i].Name == names[0] { //already in tree
                break
            }
        }
        if i == len(root) {
            root = append(root, Node{Name: names[0]})
        }
        root[i].Children = AddToTree(root[i].Children, names[1:])
    }
    return root
}</code>

EXAMPLE OUTPUT

Using this solution, the input data provided can be transformed into the desired tree structure:

[{
    "name": "a",
    "children": [{
        "name": "b",
        "children": [{
            "name": "c"
        }, {
            "name": "g"
        }]
    }, {
        "name": "d"
    }]
}]

KEY DIFFERENCES

Compared to the provided code, the proposed solution offers several key advantages:

  • It operates on a list of nodes, allowing for multiple root nodes.
  • It creates new nodes instead of modifying the input node, avoiding data duplication.
  • It actively searches the tree to prevent duplicate nodes.

The above is the detailed content of How can you efficiently transform a list of path strings into a hierarchical tree structure without relying heavily on pointer manipulation and recursion?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn