/ Published in: C++
zadatak 4, opcenito stablo
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct elemOS { char v[15]; int d, b; }; struct OS { elemOS el[10000]; int korijen; }; typedef OS* stablo; typedef int onode; onode FirstChildT(onode n, stablo t) { return t->el[n].d; } onode NextSiblingT(onode n, stablo t) { return t->el[n].b; } char* LabelT(onode n, stablo t) { return t->el[n].v; } void ChangeLabelT(char* x, onode n, stablo t) { strcpy(t->el[n].v, x); } onode RootT(stablo t) { return t->korijen; } onode ParentT(onode n, stablo t) { for(int m=0; m<10000; m++) { if(n == t->el[m].d) return m; if(n == t->el[m].b) return ParentT(m, t); } return -1; } void CreateT(onode x, onode n, stablo t) { if(strcmp(t->el[x].v, "")!=0) return; t->el[x].d = -1; t->el[x].b = -1; strcpy(t->el[x].v, ""); if(t->el[n].d==-1) { t->el[n].d = x; return; } n = FirstChildT(n, t); while(NextSiblingT(n, t)!=-1) n=NextSiblingT(n, t); t->el[n].b = x; } stablo InitT(onode x, stablo t) { t = new OS; for(int i=0; i<10000; i++) { t->el[i].d = -1; t->el[i].b = -1; strcpy(t->el[i].v, ""); } t->korijen = x; return t; } void DeleteT(onode n, stablo t, bool m=1) { if(m) { for(int i=0; i<10000; i++) { if(t->el[i].d==n) { t->el[i].d = NextSiblingT(n, t); t->el[n].b = -1; break; } else if(t->el[i].b==n) { t->el[i].b = NextSiblingT(n, t); t->el[n].b = -1; break; } } DeleteT(n, t, 0); return; } if(FirstChildT(n, t)!=-1) DeleteT(FirstChildT(n, t), t, 0); for(onode n2=NextSiblingT(n, t); n2!=-1; n2=NextSiblingT(n2, t) ) DeleteT(n2, t, 0); strcpy(t->el[n].v, ""); t->el[n].b = -1; t->el[n].d = -1; }