#include <stdio.h>
#include <conio.h>
#include <alloc.h>
# define tree struct tree
   tree{ int inf;
         tree *left;
         tree *right;
         int balans;
       };
    tree *top;
void ris(tree *a,int l,int r,int y)
{
   gotoxy((l+r)/2,y);cprintf("%d",a->inf);
   if(a->left!=NULL) ris(a->left,l,(l+r)/2,y+4);
   if(a->right!=NULL) ris(a->right,(l+r)/2,r,y+4);
}
void poisk(tree **a,int x,int *h)
{  tree *a1;
   if((*a)==NULL) { a1=(tree*)malloc(sizeof(tree));
                    a1->left=a1->right=NULL;
                    a1->inf=x;top->balans=0;
                    (*a)=a1;*h=1;
                  }
   if(x<(*a)->inf) if((*a)->left!=NULL) poisk(&((*a)->left),x,h);
                   {
                         switch ((*a)->balans)
                         { case 1:*h=0;(*a)->balans=0;break;
                           case 0:*h=0;(*a)->balans=-1;break;
                           case -1: //balans
                                   { (*a)->left=a1->right;
                                     (*a)->balans=0;
                                     a1->right=(*a);
                                     (*a)=a1;
                                   }
                         }
                       }
   else           if((*a)->right!=NULL) poisk(&((*a)->right),x,h);
                   {    switch ((*a)->balans)
                         { case -1:*h=0;(*a)->balans=0;break;
                           case 0:*h=0;(*a)->balans=1;break;
                           case 1:; //balans
                         }

                       }
}
main()
{  int x,h;
   do{ gotoxy(1,20);cprintf("Input x - ");scanf("%d",&x);
       poisk(&top,x,&h);clrscr();ris(top,1,79,1);
     } while(x!=0);


}
