Skip to content

What is Tree-Conver?

Tree-Conver is a tool that can convert between flat node arrays and tree arrays.

Exports two methods: arrayToTree and treeToArray.

Performance

Array to Tree

This method has time and space complexity of O(n), where n is the length of the input array. The method traverses the array twice, once to create a mapping from node ID to node, and once to add nodes to their parent's children array. It ultimately stores all nodes and child nodes.

Tree to Array

This method uses stack-based iteration, optimizing memory and call stack. The time complexity of this method depends on the depth and number of nodes in the tree. In the worst case, it is O(nlogn), but in the case of a shallow tree, it can maintain O(n) even with multiple child nodes.

The space complexity is O(n), where the maximum length of the node array in the stack is equal to the depth of the tree.

Released under the MIT License.