You are on page 1of 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Tree t = new Tree(null, 0);
t.insere('C');
t.insere('A');
t.insere('B');
t.insere('D');
t.insere('G');
t.insere('F');
t.insere('E');
t.preOrdem();
Console.ReadKey();
}
}

public class Tree
{
private TreeNode raiz;
private int soma;
public Tree(TreeNode raiz, int aux)
{
raiz = null;
}

public bool estahVazia()
{
return raiz == null;
}
public void insere(char valor)
{
if(estahVazia())
raiz = new TreeNode(valor,-1);
else
raiz.insereNo(valor);
}
public void preOrdem(TreeNode no)
{
if(no!=null)
{
soma += no.total;
Console.WriteLine("Valor: " + ((Char) no.info) + "\tNvel: " + no.
nivel + "\tDescendente: " + no.desc + "\tTotal " + no.total);
preOrdem(no.esq);
preOrdem(no.dir);
}
}
public void preOrdem()
{
soma = 0;
preOrdem(raiz);
Console.WriteLine("\tSoma " + soma);
}

}
public class TreeNode
{
public int info;
public TreeNode esq;
public TreeNode dir;
public int nivel;
public int desc;
public int total;
public TreeNode(char valor, int nivelPai)
{
info = valor;
esq = dir = null;
nivel = nivelPai + 1;
total = valor - 64;
}
public void insereNo(char valor)
{
if(valor < info)
{
if (esq == null)
{
desc += 1;
esq = new TreeNode(valor, nivel);
}
else
{
esq.insereNo(valor);
desc += 1;
}
}
else
{
if(dir==null)
{
desc += 1;
dir = new TreeNode(valor, nivel);
}
else
{
dir.insereNo(valor);
desc += 1;
}
}
}
}
}

You might also like