#include <iostream>

#ifdef __BORLANDC__
#else
  using namespace std;
#endif

class Drzewo
  {
   protected:
   struct Element
     {
      double Dane;
      Element *Lewy;
      Element *Prawy;
     };
   Element *Korzen;
   virtual void _dodaj(double x);
   void _wstaw(Element *&e,Element *nowy);
   void _drukuj(ostream &s,Element *e)const;
   void _zwolnij(Element *e);
   public:
   Drzewo():Korzen(0) {}
   virtual ~Drzewo();
   Drzewo &operator<<(double x) { _dodaj(x); return(*this); }
   friend ostream &operator<<(ostream &s,const Drzewo &D);
  };

void Drzewo::_zwolnij(Element *e)
  {
   if(e)
     {
      _zwolnij(e->Lewy);
      _zwolnij(e->Prawy);
      delete e;
     }
  }

Drzewo::~Drzewo()
  {
   _zwolnij(Korzen);
  }

void Drzewo::_drukuj(ostream &s,Element *e)const
  {
   if(e)
     {
      _drukuj(s,e->Prawy);
      s<<e->Dane<<endl;
      _drukuj(s,e->Lewy);
     }
  }

ostream &operator<<(ostream &s,const Drzewo &D)
  {
   D._drukuj(s,D.Korzen);
   return(s);
  }


void Drzewo::_wstaw(Element *&e,Element *nowy)
  {
   if(e)
     {
      if(e->Dane<nowy->Dane) _wstaw(e->Lewy,nowy);
      else                   _wstaw(e->Prawy,nowy);
     }
   else e=nowy;
  }

void Drzewo::_dodaj(double x)
  {
   Element *Nowy=new Element;
   Nowy->Dane=x;
   Nowy->Lewy=0;
   Nowy->Prawy=0;
   _wstaw(Korzen,Nowy);
  }

int main()
  {
   Drzewo D;

   D<<3<<1<<7<<2<<5<<6<<4;
   cout<<D<<endl;

   return(0);
  }
