zaglavlje bstablo_polje.h


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

jedno od zaglavlja koje treba koristiti u zadnjem (:D) zadatku


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.