Posted By

tturek on 01/03/14


Tagged

Strukture


Versions (?)

binarno_polje


 / Published in: C++
 

eto polja

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct element{
  5. int label;
  6. bool used;
  7. };
  8.  
  9. struct tree{
  10. element polje[1000];
  11. };
  12.  
  13. tree *InitB(int x,tree *T){
  14. T = new tree;
  15. for(int i=0;i<1000;i++) T->polje[i].used = false;
  16. T->polje[1].label = x;
  17. T->polje[1].used = true;
  18. return T;
  19. }
  20.  
  21. int LabelB(int n,tree *T){
  22. return T->polje[n].label;
  23. }
  24.  
  25. void ChangeLabelB(int x,int n,tree *T){
  26. T->polje[n].label = x;
  27. }
  28.  
  29. int RootB(tree *T){
  30. return T->polje[1].label;
  31. }
  32.  
  33. int LeftChildB(int n,tree *T){
  34. if(!T->polje[2*n].used) return -1;
  35. return 2*n;
  36. }
  37.  
  38. int RightChildB(int n,tree *T){
  39. if(!T->polje[2*n+1].used) return -1;
  40. return 2*n+1;
  41. }
  42.  
  43. int ParentB(int n,tree *T){
  44. if(T->polje[1].label==n) return -1;
  45. if(n%2) return n/2+1;
  46. else return n/2;
  47. }
  48.  
  49. void CreateLeftB(int x,int n,tree *T){
  50. if(T->polje[2*n].used) cout << "Zauzeta pozicija!" << endl;
  51. else{
  52. T->polje[2*n].label = x;
  53. T->polje[2*n].used = true;
  54. }
  55. }
  56.  
  57. void CreateRightB(int x,int n,tree *T){
  58. if(T->polje[2*n+1].used) cout << "Zauzeta pozicija!" << endl;
  59. else{
  60. T->polje[2*n+1].label = x;
  61. T->polje[2*n+1].used = true;
  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->polje[n].used = false;
  69. }

Report this snippet  

You need to login to post a comment.