/ Published in: C++
Implementacija binarnog stabla pomoću pokazivaÄa u jeziku C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
struct binarno_stablo { int label; int left,right; }; binarno_stablo *tree = new binarno_stablo[1000]; int ParentB(int n, binarno_stablo *B) { if (n<=1) return -1; if (tree[n].label>0) return (n/2); else return -1; }; int LeftChildB(int n, binarno_stablo *B) { if (tree[n].label>=0) { if (tree[n].left>0) return (n*2); else return -1; } else return -1; }; int RightChildB(int n, binarno_stablo *B) { if (tree[n].label>=0) { if (tree[n].right>0) return (n*2+1); else return -1; } else return -1; }; int LabelB(int n, binarno_stablo *B) { if (tree[n].label>0) return (tree[n].label); else return -1; }; int ChangeLabelB(int x, int n, binarno_stablo *B) { if (tree[n].label>0) tree[n].label=x; else return -1; }; int RootB(binarno_stablo *B) { if (tree[1].label>0) return 1; else return -1; }; int CreateLeftB(int x, int n, binarno_stablo *B) { if (tree[n].label>0) { if (tree[n].left<=0) { binarno_stablo *left1=new binarno_stablo; tree[n].left=n*2; left1->label=x; tree[n*2]=*left1; } else return -1; } else return -1; }; int CreateRightB(int x, int n, binarno_stablo *B) { if (tree[n].label>0) { if (tree[n].right<=0) { binarno_stablo *right1=new binarno_stablo; tree[n].right=(n*2)+1; right1->label=x; tree[(n*2)+1]=*right1; } else return -1; } else return -1; }; int DeleteB(int n, binarno_stablo *B) { if (tree[n].label>0) { if (tree[n*2].label>0) { DeleteB(n*2, B); tree[n*2].label=-1; tree[n*2].left=-1; tree[n*2].right=1; } if (tree[(n*2)+1].label>0) { DeleteB(n*2, B); tree[(n*2)+1].label=-1; tree[(n*2)+1].left=-1; tree[(n*2)+1].right=1; } tree[n].label=-1; tree[n].right=-1; tree[n].left=-1; } }; int InitB(int x, binarno_stablo *B) { B->label=x; B->right=0; B->left=0; tree[1]=*B; };