Posted By

tjakopec on 01/11/11


Tagged

cpp sp foi


Versions (?)

bstablo_polje.h


 / Published in: C++
 

  1. struct element{
  2. int label;
  3. int koristeno;
  4. };
  5. struct bt{
  6. element elements[10000];
  7. };
  8. typedef int cvor;
  9.  
  10. bt* InitB(int x, bt* T){
  11. if(T) delete T;
  12. T = new bt;
  13. for(int i = 0; i < 10000; i++)
  14. T->elements[i].koristeno = 0;
  15. T->elements[1].label = x;
  16. T->elements[1].koristeno = 1;
  17. return T;
  18. }//initB
  19.  
  20. void CreateLeftB(int x, cvor n, bt* T){
  21. if(T->elements[n].koristeno == 0 || T->elements[2*n].koristeno == 1) {
  22. cout << "Greska" << endl << endl;
  23. return;
  24. }
  25. T->elements[2*n].koristeno = 1;
  26. T->elements[2*n].label = x;
  27. }//CreateLeftB
  28.  
  29. void CreateRightB(int x, cvor n, bt* T){
  30. if(T->elements[n].koristeno == 0 || T->elements[2*n+1].koristeno == 1){
  31. cout << "Greska" << endl << endl;
  32. return;
  33. }
  34. T->elements[2*n+1].koristeno = 1;
  35. T->elements[2*n+1].label = x;
  36. }//CreateRightB
  37.  
  38. cvor LeftChildB(cvor n, bt* T){
  39. if(T->elements[n].koristeno == 0)
  40. return 0;
  41. if(T->elements[2*n].koristeno == 1)
  42. return 2*n;
  43. else return 0;
  44. }//LeftCildB
  45.  
  46. cvor RightChildB(cvor n, bt* T){
  47. if(T->elements[n].koristeno == 0) return 0;
  48. if(T->elements[2*n+1].koristeno == 1) return 2*n+1;
  49. else return 0;
  50. }//RightCildB
  51.  
  52. cvor ParentB(cvor n, bt* T){
  53. if(n == 1) return 0;
  54. if(n%2) n--;
  55. return n/2;
  56. }//ParentB
  57.  
  58. int LabelB(cvor n, bt* T){
  59. return T->elements[n].label;
  60. }//LabelB
  61.  
  62. void ChangeLabelB(int x, cvor n, bt* T){
  63. if(T->elements[n].koristeno == 0) return;
  64. T->elements[n].label = x;
  65. }//ChangeLabelB
  66.  
  67. cvor RootB(bt* T){
  68. if(T->elements[1].koristeno == 0) return 0;
  69. return 1;
  70. }//RootB
  71.  
  72. void DeleteB(cvor n, bt* T){
  73. T->elements[n].koristeno = 0;
  74. if (LeftChildB(n, T)) DeleteB(LeftChildB(n, T), T);
  75. if (RightChildB(n, T)) DeleteB(RightChildB(n, T), T);
  76. }//DeleteB

Report this snippet  

You need to login to post a comment.