Skip to content

Instantly share code, notes, and snippets.

@asssis
Created November 19, 2020 16:17
Show Gist options
  • Save asssis/8719c82e366a77137ed4f7d9971603cb to your computer and use it in GitHub Desktop.
Save asssis/8719c82e366a77137ed4f7d9971603cb to your computer and use it in GitHub Desktop.
codigo_arvore c#
using System.Collections.Generic;
using System;
using System.Linq;
public class Program
{
public static void Main()
{
Arvore obj = new Arvore();
obj.add("a","b");
obj.add("a","c");
obj.add("c","e");
obj.add("c","f");
obj.add("b","g");
obj.add("b","h");
obj.visualizar(obj.arvore[0]);
}
}
public class Arvore{
string pai {get; set;}
List<string> filhos {get; set;}
public List<Arvore> arvore = new List<Arvore>();
public void add(string pai, string filho){
foreach(Arvore obj in this.arvore){
if(obj.pai == pai){
obj.filhos.Add(filho);
return;
}
}
Arvore arvore = new Arvore();
arvore.pai = pai;
arvore.filhos = new List<string>();
arvore.filhos.Add(filho);
this.arvore.Add(arvore);
}
public void visualizar(Arvore obj){
Console.Write(obj.pai);
Console.Write("[");
List<string> filhos = new List<string>();
foreach(string filho in obj.filhos){
Arvore v = arvore.FirstOrDefault(x=>x.pai == filho);
if(v != null){
visualizar(v);
}
else{
filhos.Add(filho);
}
}
Console.Write(string.Join(",",filhos));
Console.Write("]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment