/ Published in: C++
binarno stablo uz pomoc polja
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <cstring> using namespace std; struct element { int used; string label; }; struct bt { element elements[10000]; }; typedef struct bt btree; int Parent(int cvor, btree *stablo){ if(cvor==1) return 0; if(cvor%2) cvor--; return cvor/2; } int LeftChildB(int cvor, btree *stablo) { if(stablo->elements[cvor].used == 0) return 0; if(stablo->elements[2*cvor].used == 1) return 2*cvor; } int RightChildB(int cvor, btree *stablo) { if(stablo->elements[cvor].used == 0) return 0; if(stablo->elements[2*cvor+1].used == 1) return 2*cvor+1; else return 0; } void LabelB(int cvor, btree *stablo) { if(stablo->elements[cvor].used==true) cout<<"Na cvoru "<<cvor<<" je label: "<<stablo->elements[cvor].label<<endl; else cout<<"Taj cvor nije iskoristen...\n"; } void ChangeLabelB(string nova_vrijednost,int cvor, btree *stablo) { if(stablo->elements[cvor].used == 0){ LabelB(cvor,stablo); return; } stablo->elements[cvor].label = nova_vrijednost; } int RootB(btree *stablo) { if(stablo->elements[1].used == 0) return 0; else return 1; } void CreateLeftB(string label, int cvor, btree *stablo) { if(stablo->elements[cvor].used == 0 || stablo->elements[2*cvor].used == 1) { cout << "Greska, zadani cvor ne postoji, ili nejgovo dijete vec postoji\n" << endl << endl; return; } stablo->elements[2*cvor].used = 1; stablo->elements[2*cvor].label = label; } void CreateRightB(string label, int cvor, btree *stablo) { if(stablo->elements[cvor].used == 0 || stablo->elements[2*cvor+1].used == 1) { cout << "Greska, zadani cvor ne postoji, ili nejgovo dijete vec postoji\n" << endl << endl; return; } stablo->elements[2*cvor+1].used = 1; stablo->elements[2*cvor+1].label = label; } void DeleteB(int cvor, btree *stablo) { stablo->elements[cvor].used = 0; if (LeftChildB(cvor, stablo)) DeleteB(LeftChildB(cvor, stablo), stablo); if (RightChildB(cvor, stablo)) DeleteB(RightChildB(cvor, stablo), stablo); } void InitB(string label, btree* stablo) { stablo->elements[1].label = label; stablo->elements[1].used = 1; }