Created
December 1, 2016 02:35
-
-
Save drmcarvalho/7a073a8edd661962875399c9c2a9929b to your computer and use it in GitHub Desktop.
Arvore Binaria
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ArvoreBinariaExemplo | |
{ | |
public class ArvoreBinaria | |
{ | |
private No raiz; | |
public ArvoreBinaria() | |
{ | |
raiz = null; | |
} | |
public void TranverseCentral() | |
{ | |
if (raiz != null) | |
raiz.TraverseCentral(raiz); | |
} | |
public void TraversePosOrder() | |
{ | |
if (raiz != null) | |
raiz.TraversePosOrder(raiz); | |
} | |
public void TraversePreOrder() | |
{ | |
if (raiz != null) | |
raiz.TraversePreOrder(raiz); | |
} | |
public void Add(int valor) | |
{ | |
if (raiz != null) | |
raiz.Add(valor); | |
else | |
raiz = new No(valor); | |
} | |
public int TraverseSumAll() | |
{ | |
return raiz != null ? raiz.TraverseSumAll(raiz, 0) : 0; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using static System.Console; | |
namespace ArvoreBinariaExemplo | |
{ | |
public class No | |
{ | |
private int valor; | |
public No esquerdo { get; set; } | |
public No direito { get; set; } | |
public No(int valor) | |
{ | |
this.valor = valor; | |
esquerdo = null; | |
direito = null; | |
} | |
public void TraverseCentral(No no) | |
{ | |
if (no.esquerdo != null) | |
TraverseCentral(no.esquerdo); | |
Write(no.valor + " "); | |
if (no.direito != null) | |
TraverseCentral(no.direito); | |
} | |
public void TraversePosOrder(No no) | |
{ | |
if (no.esquerdo != null) | |
TraversePosOrder(no.esquerdo); | |
if (no.direito != null) | |
TraversePosOrder(no.direito); | |
Write(no.valor + " "); | |
} | |
public void TraversePreOrder(No no) | |
{ | |
Write(no.valor + " "); | |
if (no.esquerdo != null) | |
TraversePreOrder(no.esquerdo); | |
if (no.direito != null) | |
TraversePreOrder(no.direito); | |
} | |
public void Add(int valor) | |
{ | |
if (valor > this.valor) | |
{ | |
if (direito != null) | |
{ | |
direito.Add(valor); | |
} | |
else | |
{ | |
direito = new No(valor); | |
} | |
} | |
else | |
{ | |
if (esquerdo != null) | |
{ | |
esquerdo.Add(valor); | |
} | |
else | |
{ | |
esquerdo = new No(valor); | |
} | |
} | |
} | |
public int TraverseSumAll(No no, int inicioSoma) | |
{ | |
int noEsquerdo = 0; | |
int noDireito = 0; | |
if (no.esquerdo != null) | |
noEsquerdo = no.esquerdo.TraverseSumAll(no.esquerdo, inicioSoma); | |
no.valor = Math.Max(no.valor, inicioSoma + noDireito); | |
if (no.direito != null) | |
noDireito = no.direito.TraverseSumAll(no.direito, inicioSoma); | |
return noEsquerdo + no.valor + noDireito; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using static System.Console; | |
namespace ArvoreBinariaExemplo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ArvoreBinaria arvore = new ArvoreBinaria(); | |
arvore.Add(4); | |
arvore.Add(1); | |
arvore.Add(3); | |
arvore.TranverseCentral(); | |
WriteLine("\n"); | |
arvore.TraversePreOrder(); | |
WriteLine("\n"); | |
arvore.TraversePosOrder(); | |
int sumAllValueTree = arvore.TraverseSumAll(); | |
WriteLine("\n"); | |
WriteLine($"\nSomatorio: {sumAllValueTree}"); | |
ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment