/ Published in: C++
Implementacija opcenitog stabla „prvo dijete - sljedeci brat“ pomocu polja
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <iostream> using namespace std; struct element{ int name; int firstchild, nextsibling; bool root; }; struct tree{ element Element[10000]; int first; }; int ParentT(int node, tree *Stablo){ for(int i=0; i<10000; i++){ if (Stablo->Element[i].firstchild==node) return i; else if (Stablo->Element[i].nextsibling==node) ParentT(i,Stablo); } return -1; } int FirstChildT(int node, tree *Stablo){ return Stablo->Element[node].firstchild; } int NextSiblingT(int node, tree *Stablo){ return Stablo->Element[node].nextsibling; } int LabelT(int node, tree *Stablo){ if (Stablo->Element[node].root==0) return -1; else return Stablo->Element[node].name; } int RootT(tree *Stablo){ return Stablo->first; } void CreateT(int child, int node, tree *Stablo){ if (Stablo->Element[node].root==0) cout << "Cvor ne postoji. " << endl; else if (Stablo->Element[node].firstchild==-1){ Stablo->Element[node].firstchild=child; Stablo->Element[child].root=1; cout << "Unesite oznaku: " ; cin >> Stablo->Element[child].name; } else{ int brother; bool brat=true; brother=Stablo->Element[node].firstchild; do{ if (Stablo->Element[brother].nextsibling==-1) brat=false; else brother=Stablo->Element[brother].nextsibling; }while(brat); Stablo->Element[brother].nextsibling=child; Stablo->Element[child].root=1; cout << "Unesite oznaku: " ; cin >> Stablo->Element[child].name; } } void ChangeLabelT(int novi, int node, tree *Stablo){ if (Stablo->Element[node].root==1) Stablo->Element[node].name=novi; else cout << "Cvor ne postoji. " << endl; } void Delete(int node, tree *Stablo){ if (Stablo->Element[node].nextsibling==-1 && Stablo->Element[ParentT(node,Stablo)].firstchild==node){ Stablo->Element[ParentT(node,Stablo)].firstchild=-1; Stablo->Element[ParentT(node,Stablo)].root=0; Stablo->Element[node].name=-1; } else if (Stablo->Element[node].nextsibling!=-1 && Stablo->Element[ParentT(node,Stablo)].firstchild==node) Stablo->Element[ParentT(node,Stablo)].firstchild = Stablo->Element[node].nextsibling; else{ int brother=Stablo->Element[ParentT(node,Stablo)].firstchild; while (Stablo->Element[brother].nextsibling!=node) brother=Stablo->Element[brother].nextsibling; Stablo->Element[brother].nextsibling=Stablo->Element[node].firstchild; } } void DeleteT(int node, tree *Stablo){ if (Stablo->Element[node].root==0) cout << "Cvor ne postoji." << endl; else if (Stablo->Element[node].firstchild!=-1){ while (Stablo->Element[node].firstchild!=-1) Delete(Stablo->Element[node].firstchild,Stablo); Delete(node,Stablo); } else Delete(node,Stablo); } void InitT(int x, tree *Stablo){ for (int i=0; i<10000; i++){ Stablo->Element[i].firstchild=-1; Stablo->Element[i].nextsibling=-1; Stablo->Element[i].root=0; } Stablo->first=x; Stablo->Element[x].root=1; cout << "Unesite oznaku korijena: "; cin >> Stablo->Element[x].name; }