/ Published in: C++
Implementacija binarnog stabla koristeći se poljem.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct arr_element{ int label, used; }; struct arr_tree{ arr_element array[10000]; }; arr_tree* InitB(int x, arr_tree* T){ T = new arr_tree; for(int i=0; i<10000; i++) T->array[i].used = 0; T->array[1].label = x; T->array[1].used = 1; return T; } int RootB(arr_tree* T){ return T->array[1].label; } int LabelB(int n, arr_tree* T){ return T->array[n].label; } int ChangeLabelB(int x, int n, arr_tree* T){ return T->array[n].label = x; } int ParentB(int n,arr_tree *T){ if(T->array[1].label==n) return -1; if(n%2) return n/2+1; else return n/2; } int LeftChildB(int n, arr_tree* T){ if(!(T->array[2*n].used)) return -1; else return 2*n; } int RightChildB(int n, arr_tree* T){ if(!(T->array[2*n+1].used)) return -1; else return 2*n+1; } void CreateLeftB(int x, int n, arr_tree* T){ if(T->array[2*n].used) cout << "Pozicija je zauzeta!" << endl; else{ T->array[2*n].label = x; T->array[2*n].used = 1; } } void CreateRightB(int x, int n, arr_tree* T){ if(T->array[2*n+1].used) cout << "Pozicija je zauzeta!" << endl; else{ T->array[2*n+1].label = x; T->array[2*n+1].used = 1; } } bool DeleteB(int n, arr_tree* T){ if(LeftChildB(n,T) != -1) DeleteB(LeftChildB(n,T),T); if(RightChildB(n,T) != -1) DeleteB(RightChildB(n,T),T); T->array[n].used = 0; }