buildTree method

List<TreeOutPutItem<T>> buildTree(
  1. List<TreeInputItem<T>> items
)

Builds a tree from a list of TreeInputItems.

  • The tree is represented as a list of TreeOutPutItem, each of which is a TreeOutPutItem containing a parent item and a list of children.

  • The children of each item are also TreeOutPutItems, and the tree is fully recursive. The depth of each item is also set, with root items having a depth of 0.

  • if parent does not exists in the list, the children will not included

Implementation

List<TreeOutPutItem<T>> buildTree(List<TreeInputItem<T>> items) {
  Map<int, _TreeNode<T>> categoryNodeMap = _buildNodeMap(items);
  List<_TreeNode<T>> rootNodes =
      _getOnlyRootsWithItsChildren(categoryNodeMap);

  for (var rootNode in rootNodes) {
    _assignDepthToChildren(rootNode, 0);
  }

  final outPutItems = _convertToTreeOutPutItem(rootNodes);

  return outPutItems;
}