algorithm - Build tree structure from the records -


I query the database for the records in the structure

ID | Words | ParentID

I have the following classes in the C # code

  public square tree {public string id {get; Set; } Public string term {get; Set; } Public string ParentId {get; Set; } Public int level {get; Set; } Public IList & lt; Tree & gt; Receive hair items { Set;   

The query returns 5 000 000 records.

I need to make a tree tree tree and populate it.

First of all, I select all those items where ParentID is zero, and then for each element search parent (if parent does not exist then I parents I am creating a parent) and can build a tree using the resuscitation.

I am not happy with my algorithm because it takes more than 5 minutes.

Please give me some advice on how to do it, what to use it, and so on.

The code has now been implemented as follows:

  Private string handle 2 (list & gt; tree & gt; basic theory) {IList & lt; Tree & gt; NewTree = new list & lt; Tree Test. Models. Tree & gt; (); IList & LT; Tree & gt; Treeless Parents = Basic Tree is (x = & gt; String.en.ParentID). Orderbay (x = & gt; x.Term). Ollist (); Exotic (tree without tree items without trees) {tree new itam = new tree {id = item. Id, period = item. Mercury, PainterID = Item. Parent id, level = 0}; newTree.Add (newItem); InsertChilds (New Itam, Basic Tree, 0); } Return "Output"; } Private Zero Enter Baby (tree item, list & gt; tree & gt; origin, integer level) {++ level; IList & LT; Tree & gt; ChildItems = origTree.Where (x => x.ParentID == item.Id) .Oolist (); OrigTree.RemoveAll (x = & gt; x.ParentID == item.id); Exotic hair (in the baby in the tree) {Origin. Away (i); } Foreach (tree titanium in Bali) {if (item.ChildTree == zero) {item.ChildTree = new list & lt; TreeTest.Models.Tree & gt; (); } Tree ItemToad = New Tree {ID = TITIM.ID, Term = TITIM. TRM, PRIID = TITIM. Parent id, level = level}; It. Instructs (itemautoud, Genesis, level); Item.ChildTree.Add (itemToAdd); ID} ( string , although I'm curious that is why it is not  int ) to node its tree nodes ( tree ) to do.  

This will allow you to obtain the node with the expected O (1) complexity instead of your current O (n) complexity.

Besides, I suggest that you reconsider your view a bit - try the code, in which you only have to go through input data, just use a single dictionary - If the parent is not yet in existence, then you can create a stuff for parents only, whose members are only populated when you buy that item.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -