/ Published in: C++
binarno stablo_polja
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct belement { int v; bool u; }; struct bstablo { belement elems[10000]; }; typedef bstablo* binStablo; typedef int binNode; binStablo InitB(int x, binStablo t) { t = new bstablo; t->elems[0].u=0; for(int i=2; i<10000; i++) t->elems[i].u=0; t->elems[1].v = x; t->elems[1].u = 1; return t; } binNode RootB(binStablo t) { return 1; } binNode ParentB(binNode n, binStablo t) { if(n==RootB(t)) return 0; else return n/2; } binNode LeftChildB(binNode n, binStablo t) { if(t->elems[n*2].u==0) return 0; else return n*2; } binNode RightChildB(binNode n, binStablo t) { if(t->elems[n*2+1].u==0) return 0; else return n*2+1; } int LabelB(binNode n, binStablo t) { int label=t->elems[n].v; return label; } void ChangeLabelB(int x, binNode n, binStablo t) { if(n>=0 && n<10000) t->elems[n].v = x; } void CreateLeftB(int x, binNode n, binStablo t) { if(n<0 || n>10000) return; if(t->elems[n*2].u) return; t->elems[n*2].v = x; t->elems[n*2].u = 1; } void CreateRightB(int x, binNode n, binStablo t) { if(n<0 || n>10000) return; if(t->elems[n*2+1].u) return; t->elems[n*2+1].v = x; t->elems[n*2+1].u = 1; } void DeleteB(binNode n, binStablo t) { if(n==1)return; if(n>=10000)return; if(t->elems[n*2].u) DeleteB(n*2, t); if(t->elems[n*2+1].u) DeleteB(n*2+1, t); t->elems[n].u = 0; }