V
V
Vignore2017-09-19 11:23:10
Programming
Vignore, 2017-09-19 11:23:10

Which algorithm to use?

At the input I have a json array of data. Each element has id(string, not null), folder(bool), parentId(string)
Based on these data, I have to make a tree. The analogy is the file system in windows explorer (I think, like in most operating systems).
Can you suggest how to implement this or what algorithm, or where to look at what has already been done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2017-09-19
@Mercury13

Very important question. Can the contents of a folder get caught before the folder itself? For the sake of simplicity, we'll assume no, and that you've found a library for JSON.
We need two objects: a tree (self-made) and an ID dictionary (map: id → tree fork).
For each element ...
1. By parent id through the dictionary, we find where to shove it. If not found, display an error.
2. If folder: create a fork, assign an ID, enter it into the ID dictionary.
3. If not folder: create a sheet, assign an ID and enter the ID into the dictionary (if necessary for some other reason).
I don't know C sharp very well, so please correct me.

class Node {
  List<Node> children;
  List<Leaf> leaves;
}

class Leaf {
  // в нём может быть что угодно
}

class Tree {
  Node root;
}

Tree tree;
Dictionary<string, Node> idDic;  // считается, что словарь нужен только на разбор JSON,
   // и он временный

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question