How to build a multiway tree from lists

712 Views Asked by At

I have to build a multiway tree by merging multiple lists together. I am trying to find an efficient algorithm to do that.

  • There can be identical lists
  • List elements are not unique. The paths to these elements are
  • The tree starts with a null root element

The lists contains elements that are not unique. The path to each element is a key,

For instances, using lists :

  • A-B-C-D-E
  • A-B-C-B
  • F-B-C-C
  • A-E-F
  • A-C-D
  • A-B-C-D-E

The following tree can be built :

enter image description here

The first algorithm I implemented builds the tree by creating the root node and adding the lists to it one after the other :

  1. Create root node
  2. Select the first list
  3. If the first element of the list is already a children of the root node, select it. Otherwise, insert the element as a child.
  4. If the next element of the list is already a children the previously selected or inserted child, select it. Otherwise, insert the element as a child.
  5. Repeat step 4 until the list is empty
  6. Select the next array and go to step 3. If there is no other list, stop.

Most of the resource I have found to build or merge tree refers to binary trees. As such, I am wondering if there is a better algorithm to build a multiway tree in my case ?

0

There are 0 best solutions below