Posted By


brumihali on 01/19/15

Tagged


Statistics


Viewed 496 times
Favorited by 0 user(s)

b_stablo_polje.h


/ Published in: C++
Save to your folder(s)

Binarno stablo polje


Copy this code and paste it in your HTML
  1. struct element2{
  2. int label, used;
  3. };
  4.  
  5. struct tree{
  6. element2 elements[10000];
  7. };
  8.  
  9. tree *InitB(int x, tree *T){
  10. T = new tree;
  11. for(int i=0; i<10000; i++){
  12. T->elements[i].used = 0;
  13. T->elements[i].label = -1;
  14. }
  15. T->elements[1].label = x;
  16. T->elements[1].used = 1;
  17. return T;
  18. }
  19.  
  20. int RootB(tree *T){
  21. return T->elements[1].label;
  22. }
  23.  
  24. int ParentB (int n, tree *T) {
  25. if (n==RootB(T)) return -1;
  26. if (n%2) return (n-1)/2;
  27. else return n/2;
  28. }
  29.  
  30. int LeftChildB(int n, tree *T){
  31. if(T->elements[n*2].used) return n*2;
  32. else return -1;
  33. }
  34.  
  35. int RightChildB(int n, tree *T){
  36. if(T->elements[n*2+1].used) return n*2+1;
  37. else return -1;
  38. }
  39.  
  40.  
  41. int LabelB(int n, tree *T){
  42. return T->elements[n].label;
  43. }
  44.  
  45. void ChangeLabelB(int x, int n, tree *T){
  46. T->elements[n].label = x;
  47. }
  48.  
  49. void CreateLeftB(int x, int n, tree *T){
  50. if(T->elements[n*2].used) cout << "Mjesto je zauzeto!\n";
  51. else{
  52. T->elements[n*2].used = 1;
  53. T->elements[n*2].label = x;
  54. }
  55. }
  56.  
  57. void CreateRightB(int x, int n, tree *T){
  58. if(T->elements[n*2+1].used) cout << "Mjesto je zauzeto!\n";
  59. else{
  60. T->elements[n*2+1].used = 1;
  61. T->elements[n*2+1].label = x;
  62. }
  63. }
  64.  
  65. void DeleteB(int n, tree *T){
  66. if(LeftChildB(n, T) != -1) DeleteB(LeftChildB(n, T), T);
  67. if(RightChildB(n, T) != -1) DeleteB(RightChildB(n, T), T);
  68. T->elements[n].label = -1;
  69. T->elements[n].used = 0;
  70. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.