/ Published in: C++
Implementacija binarnog stabla pomoću polja za kolegij Strukture podataka.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct array_element{ int label, used; }; struct array_tree{ array_element array[10000]; }; array_tree* array_InitB(int x, array_tree* T){ T = new array_tree; for(int i=0; i<10000; i++){ T->array[i].used = 0; T->array[i].label = -1; } T->array[1].label = x; T->array[1].used = 1; return T; } int array_RootB(array_tree* T){ return T->array[1].label; } int array_LabelB(int n, array_tree* T){ return T->array[n].label; } int array_ChangeLabelB(int x, int n, array_tree* T){ return T->array[n].label = x; } int array_ParentB(int n, array_tree *T){ if(T->array[1].label==n) return -1; if(n%2) return n/2+1; else return n/2; } int array_LeftChildB(int n, array_tree* T){ if(!(T->array[2*n].used)) return -1; else return 2*n; } int array_RightChildB(int n, array_tree* T){ if(!(T->array[2*n+1].used)) return -1; else return 2*n+1; } void array_CreateLeftB(int x, int n, array_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 array_CreateRightB(int x, int n, array_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; } } void array_DeleteB(int n, array_tree* T){ if(array_LeftChildB(n,T) != -1) array_DeleteB(array_LeftChildB(n,T),T); if(array_RightChildB(n,T) != -1) array_DeleteB(array_RightChildB(n,T),T); T->array[n].label = -1; T->array[n].used = 0; }