/ Published in: C++
Binarno stablo polje
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct element2{ int label, used; }; struct tree{ element2 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 (n==RootB(T)) return -1; if (n%2) return (n-1)/2; else return n/2; } int LeftChildB(int n, tree *T){ if(T->elements[n*2].used) return n*2; else return -1; } int RightChildB(int n, tree *T){ if(T->elements[n*2+1].used) return n*2+1; else return -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; }