Posted By


sasacalopa on 01/06/11

Tagged


Statistics


Viewed 537 times
Favorited by 0 user(s)

bstablo_polje.h


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

Binarno stablo, implementacija pomoću polja(.h)


Copy this code and paste it in your HTML
  1. #ifndef BSTABLO_POKAZIVAC_H
  2.  
  3. #ifndef BSTABLO_POLJE_H
  4. #define BSTABLO_POLJE_H
  5.  
  6. struct element{
  7. labeltype label;
  8. int used;
  9. };
  10. struct bt{
  11. struct element elements[MAX_EL];
  12. };
  13. typedef struct bt *btree;
  14. typedef int node;
  15.  
  16. btree InitB(labeltype x,btree T){
  17. T = new bt;
  18. T->elements[1].label = x;
  19. T->elements[1].used = 1;
  20. return T;
  21. }
  22. node ParentB(node n,btree T){
  23. if(T==NULL) return 0;
  24. if(n<2) return LAMBDA;
  25. return n/2;
  26. }
  27. node LeftChildB(node n,btree T){
  28. if(T->elements[2*n].used == PRAZNO) return LAMBDA;
  29. return 2*n;
  30. }
  31. node RightChildB(node n,btree T){
  32. if(T->elements[2*n+1].used == PRAZNO) return LAMBDA;
  33. return 2*n+1;
  34. }
  35. labeltype LabelB(node n,btree T){
  36. if(T==NULL) return 0;
  37. if(T->elements[n].used == PRAZNO) return LAMBDA;
  38. return T->elements[n].label;
  39. }
  40. void ChangeLabelB(labeltype x,node n,btree T){
  41. T->elements[n].label = x;
  42. }
  43. node RootB(btree T){
  44. if(T==NULL) return 0;
  45. if(T->elements[1].used == PRAZNO) return LAMBDA;
  46. return 1;
  47. }
  48. int CreateLeftB(labeltype x,node n,btree T){
  49. if(T->elements[2*n].used == PUNO) return GRESKA;
  50. T->elements[2*n].used = PUNO;
  51. T->elements[2*n].label = x;
  52. return 0;
  53. }
  54. int CreateRightB(labeltype x,node n,btree T){
  55. if(T->elements[2*n+1].used == PUNO) return GRESKA;
  56. T->elements[2*n+1].used = PUNO;
  57. T->elements[2*n+1].label = x;
  58. return 0;
  59. }
  60. bool ExistsLeftChildB(node n,btree T){
  61. if(T==NULL) return LAMBDA;
  62. if(T->elements[n*2].used == PUNO) return true;
  63. return false;
  64. }
  65. bool ExistsRightChildB(node n,btree T){
  66. if(T==NULL) return LAMBDA;
  67. if(T->elements[n*2+1].used == PUNO) return true;
  68. return false;
  69. }
  70. void DeleteB(node n,btree T){
  71. if(ExistsLeftChildB(n,T)){
  72. DeleteB(LeftChildB(n,T),T);
  73. }
  74. if(ExistsRightChildB(n,T)){
  75. DeleteB(RightChildB(n,T),T);
  76. }
  77. T->elements[n].used = PRAZNO;
  78. }
  79.  
  80. #endif /*BSTABLO_POLJE_H*/
  81.  
  82. #endif /*BSTABLO_POKAZIVAC_H*/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.