/ Published in: C++
Header datoteka za implementaciju stabla prvo dijete sljedeci brat
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <iostream> using namespace std; struct elem{ char labela; int dijete,brat; }; struct tree{ elem P[1000]; int root; }; tree *InitT(int k,tree *T){ T=new tree; for(int i=0;i<1000;i++){ T->P[i].labela='0'; T->P[i].dijete=T->P[i].brat=-1; } T->P[k].labela='A'; T->root=k; return T; } void ChangeLabelT(char lab,int n,tree *T ){ T->P[n].labela=lab; } int RootT(tree *T){ return T->root; } char LabelT(int n,tree *T){ return T->P[n].labela; } void CreateT(int x,int n,tree *T){ if(T->P[n].labela=='0'){ cout << "Ne postoji cvor: " << n << endl; return; } if(T->P[n].dijete==-1)T->P[n].dijete=x; else if(T->P[T->P[n].dijete].brat==-1)T->P[T->P[n].dijete].brat=x; else{ n=T->P[n].dijete; while(T->P[n].brat!=-1) n = T->P[n].brat; T->P[n].brat = x; } T->P[x].labela = T->P[n].labela+1; T->P[x].dijete = T->P[x].brat = -1; } int FirstChildT(int n,tree *T){ return T->P[n].dijete; } int NextSiblingT(int n,tree *T){ return T->P[n].brat; } int ParentT(int n,tree *T){ for(int i=0;i<1000;i++){ if(T->P[i].dijete==n)return i; if(T->P[i].brat==n)return ParentT(i,T); } } void DeleteT(int n,tree *T){ if(T->P[n].dijete!=-1) DeleteT(T->P[n].dijete,T); if(T->P[n].brat!=-1) DeleteT(T->P[n].brat,T); T->P[n].dijete=T->P[n].brat=-1; T->P[n].labela='0'; if(T->P[ParentT(n,T)].brat!=-1) T->P[ParentT(n,T)].dijete = T->P[ParentT(n,T)].brat; else T->P[ParentT(n,T)].dijete = -1; }