For convenience, the comma replaced the hyphen.

At the entrance, a set of numeric arrays. Example:

2-1-2-0-6-7 2-1-2-0-7-0 2-1-2-0-7-1-2-1-3-1-6 2-1-2-0-7-1-2-1-3-2-8 2-1-2-0-7-1-3-4 2-1-2-0-7-1-3-5 2-1-2-0-7-1-4 2-1-2-0-7-1-4-6 2-1-2-0-8 2-1-2-2 

The output should be a tree whose vertices are the initial subarrays of the arrays subordinate to them. New arrays can be created, but if new arrays are created, each vertex should subordinate at least two arrays to itself, that is, if only one class abcde is in class ab, then it is not necessary to allocate class ab (unless ab is specified at the input) by inserting to class a directly abcde.

From the example given by me, the following should be obtained, with preservation of the hierarchical structure, it is expressed by spaces here:

 2-1-2 (создаётся класс) 2-1-2-0 (создаётся класс) 2-1-2-0-6-7 2-1-2-0-7 (создаётся класс) 2-1-2-0-7-0 2-1-2-0-7-1 (создаётся класс) 2-1-2-0-7-1-2-1-3 (создаётся класс) 2-1-2-0-7-1-2-1-3-1-6 2-1-2-0-7-1-2-1-3-2-8 2-1-2-0-7-1-3 (создаётся класс) 2-1-2-0-7-1-3-4 2-1-2-0-7-1-3-5 2-1-2-0-7-1-4 2-1-2-0-7-1-4-6 2-1-2-0-8 2-1-2-2 

It is important that when the processor encounters an array of 2-1-2-0-7-1-3-4, it must create a class of 2-1-2-0-7-1, and lay it between class 2-1-2 -0-7 and 2-1-2-0-7-1-2-1-3 (the latter is obtained during the initial comparison of the two previous arrays), since in this class the array 2-1-2-0- will be placed later 7-1-3-4 (in this case, indirectly), not being a representative of class 2-1-2-0-7-1-2-1-3, but belonging to class 2-1-2-0-7 -1, which is created.

The programming language is java whenever possible, but not necessarily - the sorting principle is important.

  • Have you ever tried to solve your problem? - tym32167
  • You want to convert an array of arrays into a tree. Well ... there are several tree concepts, you need to choose one of them, and work with it. If you just want to sort - the tree (or graph) is not necessary to build. Some tasks are solved through a tree (graph), and some just sorting. It is not clear from the example which task you are trying to solve. - nick_n_a
  • @nick_n_a and what are these concepts called, that is, what should I google? it would be good if there was a standard method not to reinvent the wheel. I googled, but there basically sorting inside arrays or merging, here you need to leave small arrays intact - Pavel Sumarokov
  • Let's say there is xml. There are links to everything, but he is slow. Usually ... declare a structure (class), and properties are assigned to the object. For some tasks, a reference to the parent is needed, for others it is not needed. If the number of "children" is limited, then a third implementation is possible. In its simplest form, two fields are enough. Value, and reference to parent. value and parent. mssql we admit, is able to build xml a tree from the similar table. I won't tell you about the libraries. - nick_n_a
  • So what's the problem with the obvious frontal algorithm: just insert the vertices into the tree one at a time and "create a class" at every opportunity (that is, when two chains appear with a common prefix of their own)? - AnT

1 answer 1

I ended up creating a class that sorts arrays into a tree

 public static class Folder { Folder() { depth = 0; code = 0; folders = new ArrayList<>(); } public void add(int[] codeArray, TextView view) { if (codeArray.length == depth) { this.view = view; return; } if (folders == null) folders = new ArrayList<>(); else for (Folder folder : folders) if (folder.code == codeArray[depth]) { folder.add(codeArray, view); return; } folders.add(new Folder(this, codeArray, view)); } private int depth; private int code; private int[] fullCode; private TextView view; private ArrayList<Folder> folders; private Folder(Folder parent, int[] codeArray, TextView view) { depth = parent.depth + 1; code = codeArray[depth - 1]; fullCode = C.fragment(codeArray, 0, depth); if (depth == codeArray.length) this.view = view; else { folders = new ArrayList<>(); folders.add(new Folder(this, codeArray, view)); } } } 

And then, when displayed on the screen in the class ItemFolder, which is a custom Layout, added a method

 void apply(Folder folder, String caption) { if (folder.folders == null) folderItself.addView(folder.view); //Эта строка схлапывает при отображении фрагменты дерева, представляющие собой последовательно подчиненные единственные классы else if (folder.folders.size() == 1 && folder.view == null) apply(folder.folders.get(0), getName(caption, folder)); else folderItself.addView(new ItemFolder(getContext(), folder, caption)); } 

And finally, to fill the tree with an array of arrays:

 for (int i = 0; i < variants.length; i++) folder.add(codeArrays[i], createNewButton(i)); 

Everything turned out exactly as intended. Thank you all for the comments that led me to the right decision)