Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

graficar un arbol binario

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: graficar un arbol binario Responder citando

nece Confused Confused Confused sito untutorial o informacion sobre la canvas en español Confused
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Claro con gusto Responder citando

Escribeme un email y te conste con un ejemplo en builder para pintar los arboles binarios Smile
_________________

Trigger Wink
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Arboles Responder citando

Hola Trigger

me podrias orientar con lo de arboles, te lo agradecere mucho.

Ruby
Volver arriba
Mensaje    Asunto: Responder citando

Aca te paso uno que hice, pero es bastante precario, a ver si te sirve de ayuda...

Código:

const int ANCHO=60;
const int ALTO=40;
const int TAMTEXTO=12;

struct Dato
{
   int indice;
   int x,y;
   char col;
};

struct nodo
{
   struct nodo *izq;
   struct Dato dato;
   struct nodo *der;
};

int tempx, tempy;
int CursorY=36;
struct nodo *raiz=NULL;
struct Dato dato;

Graphics::TBitmap *Fondo;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormActivate(TObject *Sender)
{
    static bool PrimeraVez=true;

    if(PrimeraVez)
    {
        Fondo = new Graphics::TBitmap;
        Fondo->Width = Form1->Width;
        Fondo->Height = Form1->Height;
        Fondo->Canvas->Font->Color = clWhite;
        Fondo->Canvas->Font->Size = TAMTEXTO;
        Fondo->Canvas->Brush->Color = clBlack;
        Fondo->Canvas->FillRect(ClientRect);
        PrimeraVez=false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
    if(Key == VK_RETURN)
    {
        if(Edit1->Text != "")
        {
        if(Edit1->Text.ToInt() == 0) Application->Terminate();
           dato.indice = Edit1->Text.ToInt();
            raiz = creaarbol(raiz,dato);
            if(raiz!=NULL)
            {
                Fondo->Canvas->Brush->Color = clBlack;
                Fondo->Canvas->FillRect(ClientRect);
                enorden(raiz);
            }
        }
        Edit1->Text = "";
    }
}
//---------------------------------------------------------------------------

struct nodo * __fastcall TForm1::nuevonodo(void)
{
   struct nodo *p=NULL;
   p=(struct nodo*)malloc(sizeof(struct nodo));
   if(p==NULL)
   {
        ShowMessage("No hay memoria suficiente.");
        Application->Terminate();
        return NULL;
   }
   else
      return p;
}
//---------------------------------------------------------------------------

struct nodo * __fastcall TForm1::creaarbol(struct nodo *r, struct Dato dat)
{
   struct nodo *p, *q=r, *ant=NULL;
   int sen;
   int xant,yant;
   p = nuevonodo();
   p->izq = NULL;
   p->der = NULL;
   p->dato = dat;
   if(r==NULL)
   {
      r = p;
      r->dato.x=(Form1->Width/2)-(ANCHO/2);
      r->dato.y=20;
      r->dato.col=15;
   }
   else
   {
      xant = r->dato.x;
      yant = r->dato.y;
      while(q)
      {
         ant = q;
         if(dat.indice > q->dato.indice)
         {
            q=q->der;
            sen=1;
            xant+=ANCHO+20;
            yant+=ALTO+20;
         }
         else if(dat.indice < q->dato.indice)
         {
            q=q->izq;
            sen=2;
            xant-=ANCHO+20;
            yant+=ALTO+20;
         }
            else
                return r;
      }
      if(sen==2)
         ant->izq=p;
      else
         ant->der=p;
      p->dato.x=xant;
      p->dato.y=yant;
      p->dato.col=15;
   }
   return r;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::enorden(struct nodo *r)
{
   if(r)
   {
      dibujar_nodo(r, tempx, tempy);
      tempx = r->dato.x;
      tempy = r->dato.y;
      enorden(r->izq);
      tempx = r->dato.x;
      tempy = r->dato.y;
      enorden(r->der);
      tempx = r->dato.x;
      tempy = r->dato.y;
   }
};
//---------------------------------------------------------------------------

void __fastcall TForm1::dibujar_nodo(struct nodo *n, int X, int Y)
{
   static bool primvez=true;

    AnsiString tt = "";
    tt = AnsiString((int)Canvas->Pixels[n->dato.x+1][n->dato.y]);

    Fondo->Canvas->Brush->Color = clWhite;
   Fondo->Canvas->FrameRect(Rect(n->dato.x, n->dato.y, n->dato.x+ANCHO, n->dato.y+ALTO));
   if(!primvez)
   {
      if(X<n->dato.x)
        {
            Fondo->Canvas->Pen->Color = clBlue;
            Fondo->Canvas->Pen->Width = 2;
         Fondo->Canvas->PenPos = Point(X+ANCHO,Y+ALTO);
            Fondo->Canvas->LineTo(n->dato.x, n->dato.y);
        }
      else if(X>n->dato.x)
        {
            Fondo->Canvas->Pen->Color = clRed;
            Fondo->Canvas->Pen->Width = 2;
         Fondo->Canvas->PenPos = Point(X, Y+ALTO);
            Fondo->Canvas->LineTo(n->dato.x+ANCHO, n->dato.y);
        }
   }
   primvez=false;
    Fondo->Canvas->Brush->Color = clBlack;
    TextoCentrado(Fondo->Canvas, n->dato.x+(ANCHO/2), n->dato.y+(ALTO/2)-TAMTEXTO+TAMTEXTO/4, AnsiString(n->dato.indice));
    Canvas->Draw(0,0,Fondo);
    Label1->Repaint();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormResize(TObject *Sender)
{
        Fondo->Width = Form1->Width;
        Fondo->Height = Form1->Height;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormPaint(TObject *Sender)
{
    Canvas->Draw(0,0,Fondo);
    Label1->Repaint();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TextoCentrado(TCanvas *Destino, int x, int y, AnsiString texto)
{
    x -= (Destino->TextWidth(texto)/2);
    Destino->TextOut(x,y,texto);
}

_________________
Il Lupo
Volver arriba
Ver perfil de usuario Enviar mensaje privado MSN Messenger
Mensaje    Asunto: Responder citando

Gracias Lupo Wink
Volver arriba
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.