Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #include<string.h>
- struct tree{
- int id,balance,broj_dece,deca[100];
- float x,y;
- struct tree *left,*right;
- };
- #define novi(x) x=(struct tree*)malloc(sizeof(struct tree))
- int dubina(struct tree *p)
- {
- int dl=0,dr=0;
- if(p)
- {
- if(p->left)dubina(p->left);
- if(p->right)dubina(p->right);
- if(dl>dr) return ++dl;
- else return ++dr;
- }
- else return 0;
- }
- void lrotation(struct tree **t)
- {
- struct tree *poml,*pomr;
- poml=*t;
- pomr=poml->right;
- poml->right=pomr->left;
- pomr->left=poml;
- *t=pomr;
- poml->balance=dubina(poml->right)-dubina(poml->left);
- pomr->balance=dubina(pomr->right)-dubina(pomr->left);
- }
- void rrotation(struct tree **t)
- {
- struct tree *poml,*pomr;
- pomr=*t;
- poml=pomr->left;
- pomr->left=poml->right;
- poml->right=pomr;
- *t=poml;
- poml->balance=dubina(poml->right)-dubina(poml->left);
- pomr->balance=dubina(pomr->right)-dubina(pomr->left);
- }
- float rastojanje(float x,float y)
- {
- float r;
- r=sqrt(pow(x,2)+pow(y,2));
- return r;
- }
- int dodaj(struct tree **p,int id, int br_dece,int *deca,float x,float y)
- {
- struct tree *t=*p;
- int inc,res=0,i;
- if(!t)
- {
- novi(t);
- if(!t) printf("Greska\n");
- t->id=id;
- t->broj_dece=br_dece;
- for(i=0;i<br_dece;i++)
- t->deca[i]=deca[i];
- t->x=x;
- t->y=y;
- t->balance=0;
- t->left=t->right=0;
- res=1;
- }
- else
- {
- if((rastojanje(x,y)) > (rastojanje(t->x,t->y))) inc=dodaj(&(t->right),id,br_dece,deca,x,y);
- else inc=-dodaj(&(t->left),id,br_dece,deca,x,y);
- t->balance+=inc;
- if(inc!=0 && t->balance!=0){
- if(t->balance<-1){
- if(t->left->balance<0)rrotation(&t);
- else { lrotation(&(t->left)); rrotation(&t);}
- }
- else if(t->balance>1){
- if(t->right->balance>0) lrotation(&t);
- else { rrotation(&(t->right)); lrotation(&t);}
- }
- else res=1;
- }
- }
- *p=t;
- return res;
- }
- struct tree *form(int n)
- {
- struct tree *root=NULL;
- int id,broj_dece,*deca=(int*)malloc(sizeof(int)*100),i,j;
- float x,y;
- for(i=0;i<n;i++)
- {
- scanf(" %d %d",&id,&broj_dece);
- for(j=0;j<broj_dece;j++)
- scanf("%d",&deca[j]);
- scanf("%f %f",&x,&y);
- dodaj(&root,id,broj_dece,deca,x,y);
- }
- return root;
- }
- void ispis(struct tree *p)
- {
- int i;
- if(p)
- {
- if(p->left)ispis(p->left);
- printf("id_dm: %d\nbroj_dece: %d\n",p->id,p->broj_dece);
- printf("Deca: ");
- for(i=0;i<p->broj_dece;i++)
- printf("%3d",p->deca[i]);
- printf("\nx=%f y=%f\nrastojanje=%f\n",p->x,p->y,rastojanje(p->x,p->y));
- printf("--------------------------------------\n");
- }
- }
- int main()
- {
- struct tree *p;
- int n;
- scanf("%d",&n);
- p=form(n);
- ispis(p);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement