Forum grupy T2 I Roku EiT


#1 2008-05-12 20:47:09

arturfocus

UÂżytkownik

Zarejestrowany: 2007-10-19
Posty: 56
Punktów :   

Drzewo binarne.

Program zrobiony dzięki pomocy Mateusza Buczkowskiego. Wstawiam bo poprosił Paweł Braniek:)

#include <iostream>

using namespace std;

class node
{
      private:
              double data;
              node *wsk_right;
              node *wsk_left;
             
      public:
             node(double d, node *wskl = 0, node *wskp = 0)
             {
                         data = d;
                         wsk_left = wskl;
                         wsk_right = wskp;
             }
             
             ~node()
             {
                    if(wsk_right) delete wsk_right;
                    if(wsk_left) delete wsk_left;
             }
             
             friend class drzewo;
             
             
};


class drzewo
{
      private:
              node *head;
             
      public:
             drzewo()
             {
                     head = 0;
             }
             
             ~drzewo()
             {
                      delete head;
             }
             
      void add(double d)
      {
           if(head == 0)
           {
                   head = new node(d);
           }
           
           else
           {
                node *wsk_temp = head;
               
                while(1)
                {
                    if(wsk_temp->data > d)
                    {
                         if(wsk_temp->wsk_left == 0)
                         {
                         wsk_temp->wsk_left = new node(d);
                         return;
                         }
                         else
                         wsk_temp = wsk_temp->wsk_left;
                         
                    }
                   
                    else
                    {
                        if(wsk_temp->wsk_right == 0)
                        {
                        wsk_temp->wsk_right = new node(d);
                        return;
                        }
                        else
                        wsk_temp = wsk_temp->wsk_right;
                    }
                }
           }
           
      }
     
      void display(node *wsk = 0)
      {
           if(wsk == 0) wsk = head;
           
           if(wsk->wsk_left) display(wsk->wsk_left);
           cout<<wsk->data<<"\n";
           if(wsk->wsk_right) display(wsk->wsk_right);       
      }
             
             
};

int main ()
{
    drzewo *a = new drzewo;
   
    a->add(2);
    a->add(5);
    a->add(3.5);
    a->add(8);
    a->add(12);
    a->add(0);
    a->add(1);   
   
   
    a->display();
   
   
   
   
    cout<<"\n\n";
    system("pause");
};


Pimpin' ride ever!
http://img112.imageshack.us/img112/682/asdasdhz7.jpghttp://img.userbars.pl/80/15955.jpg
http://img.userbars.pl/92/18342.pnghttp://img.userbars.pl/90/17940.png

Offline

 

#2 2008-05-12 22:08:29

P.Brańka

UÂżytkownik

Zarejestrowany: 2007-10-04
Posty: 22
Punktów :   

Re: Drzewo binarne.

dzieki

Offline

 

Stopka forum

RSS
Powered by PunBB 1.2.23
© Copyright 2002–2008 PunBB
Polityka cookies - Wersja Lo-Fi


Darmowe Forum | Ciekawe Fora | Darmowe Fora
GotLink.plklej do luster