Binary Tree with Pointers

G

Guest

I´m doing a paper about Binary Tree with pointers, and a want to reproduce them in C# but I must use pointers .I Tried to do but failed . My piece of code (with pointers)
not Working(I can´t use class , anyone know why?):
I did it without pointers , but i can´t use it for my paper.

Thank you for any help

namespace Arvores
{
public unsafe struct ArvorePonteiro
{
private int valor;
private unsafe ArvorePonteiro* noEsquerdo;
private unsafe ArvorePonteiro* noDireito;
private bool vazio;

public unsafe ArvorePonteiro(int cheio)
{
vazio=true;
valor=0;
this.noDireito=null;
this.noEsquerdo=null;
}

public unsafe void Inserir(ref ArvorePonteiro* novoNo,ref int valor)
{
if (novoNo->vazio==true)
{
novoNo->valor=valor;
vazio=false;
}
else
{
if (valor < novoNo->valor)
{
if (novoNo->noEsquerdo==null)
{
ArvorePonteiro obj = new ArvorePonteiro(1);
novoNo->noEsquerdo =&obj;
Inserir(ref novoNo->noEsquerdo,ref valor);
}
else
{
Inserir(ref novoNo->noEsquerdo,ref valor);
}
}
else
{
if (novoNo->noDireito==null)
{
ArvorePonteiro teste = new ArvorePonteiro(1);
novoNo->noDireito=&teste;
Inserir(ref novoNo->noDireito,ref valor);
}
else
{
Inserir(ref novoNo->noDireito,ref valor);
}
}
}

}
public unsafe void PreOrdem(ArvorePonteiro* novoNo)
{
if (novoNo->vazio==false)
{
Console.WriteLine(novoNo->valor);
PreOrdem(novoNo->noEsquerdo);
PreOrdem(novoNo->noDireito);
}
}
}
}
 
J

Jon Skeet [C# MVP]

Tiago said:
I?m doing a paper about Binary Tree with pointers, and a want to
reproduce them in C# but I must use pointers .I Tried to do but
failed . My piece of code (with pointers)
not Working(I can?t use class , anyone know why?):
I did it without pointers , but i can?t use it for my paper.

What do you mean by "I can't use class"?

Using a reference type often accomplishes what you might use pointers
for in C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top