Posted By


cvitka on 01/17/15

Tagged


Statistics


Viewed 315 times
Favorited by 0 user(s)

bstablo_polje.h


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

Implementacija binarnog stabla pomoću polja u jeziku C++


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct element
  5. {
  6. int label;
  7. bool used;
  8. };
  9.  
  10. struct binarno_stablo
  11. {
  12. struct element elementi[1000];
  13. };
  14.  
  15.  
  16. int ParentB(int n, binarno_stablo *B)
  17. {
  18. if(B->elementi[n].used!=0)
  19. return (n/2);
  20. else
  21. return -1;
  22. };
  23.  
  24. int LeftChildB(int n, binarno_stablo *B)
  25. {
  26. if(B->elementi[n].used!=0 && B->elementi[n*2].used==1)
  27. return (n*2);
  28. else
  29. return -1;
  30. };
  31.  
  32. int RightChildB(int n, binarno_stablo *B)
  33. {
  34. if(B->elementi[n].used!=0 && B->elementi[n*2+1].used==1)
  35. return (n*2+1);
  36. else
  37. return -1;
  38. };
  39.  
  40. int LabelB(int n, binarno_stablo *B)
  41. {
  42. if(B->elementi[n].used!=0)
  43. return B->elementi[n].label;
  44. else
  45. return -1;
  46. };
  47.  
  48. int ChangeLabelB(int x,int n, binarno_stablo *B)
  49. {
  50. if(B->elementi[n].used!=0)
  51. B->elementi[n].label=x;
  52. else
  53. return -1;
  54. };
  55.  
  56. int RootB(binarno_stablo *B)
  57. {
  58. if(B->elementi[1].used!=0)
  59. return B->elementi[1].label;
  60. else
  61. return -1;
  62. };
  63.  
  64. int CreateLeftB(int x, int n, binarno_stablo *B)
  65. {
  66. if(B->elementi[n].used!=0 && B->elementi[n*2].used==0)
  67. {
  68. B->elementi[n*2].label=x;
  69. B->elementi[n*2].used=1;
  70. }
  71. else
  72. return -1;
  73. };
  74.  
  75. int CreateRightB(int x, int n, binarno_stablo *B)
  76. {
  77. if(B->elementi[n].used!=0 && B->elementi[n*2+1].used==0)
  78. {
  79. B->elementi[n*2+1].label=x;
  80. B->elementi[n*2+1].used=1;
  81. }
  82. else
  83. return -1;
  84. };
  85.  
  86. void DeleteB(int n, binarno_stablo *B)
  87. {
  88. if(B->elementi[n].used!=0)
  89. {
  90. if(B->elementi[n*2].used==1)
  91. DeleteB(n*2, B);
  92. if(B->elementi[n*2+1].used==1)
  93. DeleteB(n*2+1, B);
  94. B->elementi[n].used=0;
  95. }
  96. else
  97. return;
  98. };
  99.  
  100. void InitB(int x, binarno_stablo *B)
  101. {
  102. B->elementi[1].label=x;
  103. B->elementi[1].used=1;
  104. for(int i=2;i<1000;i++)
  105. B->elementi[i].used=0;
  106. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.