Implementacija binarnog stabla pomoću polja


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



Copy this code and paste it in your HTML
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct Selement
  5. {
  6. double oznaka;
  7. bool istrosenost;
  8. };
  9.  
  10. struct bin_stablo
  11. {
  12. Selement *elements[1000];
  13. };
  14.  
  15. typedef int element;
  16.  
  17. element ParentB(bin_stablo *bin, element n)
  18. {
  19. return (n/2);
  20. };
  21.  
  22. element LeftChildB(bin_stablo *bin, element n)
  23. {
  24. return (2*n);
  25. }
  26.  
  27. element RightChildB(bin_stablo *bin, element n)
  28. {
  29. return (2*n+1);
  30. }
  31.  
  32. double LabelB(bin_stablo *bin, element n)
  33. {
  34. return bin->elements[n]->oznaka;
  35. }
  36.  
  37. element RootB(bin_stablo *bin)
  38. {
  39. return 1;
  40. }
  41.  
  42. void CreateLeftB(bin_stablo *bin, element n, double x)
  43. {
  44. if(bin->elements[2*n]->istrosenost == true)
  45. {
  46. cout <<"Lijevo dijete vec postoji" << endl;
  47. exit(0);
  48. }
  49. else
  50. {
  51. bin->elements[2*n]->istrosenost = true;
  52. bin->elements[2*n]->oznaka =x;
  53. }
  54. }
  55.  
  56. void CreateRight(bin_stablo *bin, element n, double x)
  57. {
  58. if(bin->elements[2*n+1]->istrosenost == true)
  59. {
  60. cout <<"Desno dijete vec postoji " << endl;
  61. exit(0);
  62. }
  63. else
  64. {
  65. bin->elements[2*n+1]->istrosenost = true;
  66. bin->elements[2*n+1]->oznaka = x;
  67. }
  68. }
  69.  
  70.  
  71.  
  72. void Del(bin_stablo *bin, element n)
  73. {
  74. if(bin->elements[2*n]->istrosenost == true)Del(bin,2*n);
  75. if(bin->elements[2*n+1]->istrosenost == true)Del(bin,2*n+1);
  76.  
  77. bin->elements[n]->istrosenost = false;
  78. }
  79.  
  80. void Delete(bin_stablo *bin, element n)
  81. {
  82. if(bin->elements[n]->istrosenost == false)
  83. {
  84. cout <<"Cvor ne postoji" << endl;
  85. exit(0);
  86. }
  87. else
  88. {
  89. Del(bin,n);
  90. }
  91. }
  92.  
  93. void InitN (bin_stablo *bin, element n){
  94. for (int i=0;i<999;i++){
  95. bin->elements[i]->istrosenost=0;
  96. }
  97. bin->elements[1]->oznaka = n;
  98. bin->elements[1]->istrosenost = 1;
  99. }
  100.  
  101. int main(){
  102.  
  103. return 0;
  104. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.