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 :
The first algorithm I implemented builds the tree by creating the root node and adding the lists to it one after the other :
- Create root node
- Select the first list
- If the first element of the list is already a children of the root node, select it. Otherwise, insert the element as a child.
- 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.
- Repeat step 4 until the list is empty
- 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 ?
