/ Published in: C++
Implementacija binarnog stabla pomoću polja.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct element{ int label, used; }; struct tree{ element elements[10000]; }; tree *InitB(int x, tree *T){ T = new tree; for(int i=0; i<10000; i++){ T->elements[i].used = 0; T->elements[i].label = -1; } T->elements[1].label = x; T->elements[1].used = 1; return T; } int RootB(tree *T){ return T->elements[1].label; } int ParentB(int n, tree *T){ if(T->elements[1].label == n) return -1; if(n%2) return n/2+1; else return n/2; } int LeftChildB(int n, tree *T){ if(!(T->elements[n*2].used)) return -1; else return T->elements[n*2].label; } int RightChildB(int n, tree *T){ if(!(T->elements[n*2+1].used)) return -1; else return n*2+1; } int LabelB(int n, tree *T){ return T->elements[n].label; } void ChangeLabelB(int x, int n, tree *T){ T->elements[n].label = x; } void CreateLeftB(int x, int n, tree *T){ if(T->elements[n*2].used) cout << "Mjesto je zauzeto!\n"; else{ T->elements[n*2].used = 1; T->elements[n*2].label = x; } } void CreateRightB(int x, int n, tree *T){ if(T->elements[n*2+1].used) cout << "Mjesto je zauzeto!\n"; else{ T->elements[n*2+1].used = 1; T->elements[n*2+1].label = x; } } void DeleteB(int n, tree *T){ if(LeftChildB(n, T) != -1) DeleteB(LeftChildB(n, T), T); if(RightChildB(n, T) != -1) DeleteB(RightChildB(n, T), T); T->elements[n].label = -1; T->elements[n].used = 0; }