/ Published in: C++
Implementacija općenitog stabla pomoću polja.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct elem{ char label; int firstC, nextS; }; struct tr{ elem elements[10000]; int first; }; tr *InitT(int x, tr *T){ T = new tr; for(int i=0; i<10000; i++){ T->elements[i].label = '0'; T->elements[i].firstC = -1; T->elements[i].nextS = -1; } T->elements[x].label = 'A'; T->first = x; return T; } int RootT(tr *T){ return T->first; } int ParentT(int n, tr *T){ if(RootT(T) == n) return -1; for(int i=0; i<10000; i++){ if(T->elements[i].firstC == n) return i; } } int FirstChildT(int n, tr *T){ return T->elements[n].firstC; } int NextSiblingT(int n, tr *T){ return T->elements[n].nextS; } char LabelT(int n, tr *T){ return T->elements[n].label; } void ChangeLabelT(char x, int n, tr *T){ T->elements[n].label = x; } void CreateT(int x, int n, tr *T){ if(LabelT(x, T) != '0'){ cout << "Vrijednost vec postoji!\n\n"; return; } if(LabelT(n, T) == '0'){ cout << "Cvor ne postoji!\n\n"; return; } if(FirstChildT(n, T) == -1) T->elements[n].firstC = x; else if(NextSiblingT(FirstChildT(n, T), T) == -1) T->elements[FirstChildT(n, T)].nextS = x; else{ n = FirstChildT(n, T); while(NextSiblingT(n, T) != -1) n = NextSiblingT(n, T); T->elements[n].nextS = x; } T->elements[x].firstC = -1; T->elements[x].nextS = -1; T->elements[x].label = T->elements[n].label+1; } void DeleteT(int n, tr *T){ if(FirstChildT(n, T) != -1) DeleteT(FirstChildT(n, T), T); if(NextSiblingT(n, T) != -1) DeleteT(NextSiblingT(n, T), T); T->elements[n].label = '0'; T->elements[n].firstC = -1; T->elements[n].nextS = -1; if(NextSiblingT(ParentT(n, T), T) != -1) T->elements[ParentT(n, T)].firstC = NextSiblingT(ParentT(n, T), T); else T->elements[ParentT(n, T)].firstC = -1; }