Posted By


vangogh94 on 01/21/15

Tagged


Statistics


Viewed 159 times
Favorited by 0 user(s)

Related snippets


binStablo_polje.h


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

Biblioteka sa funkcijama za rad sa stablom pomoću polja


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef int cvor;
  5.  
  6. struct cvorStrukt{
  7. cvor data;
  8. bool used;
  9. cvorStrukt(): used(false){}
  10. };
  11.  
  12. typedef cvorStrukt elem;
  13. struct bStabloStrukt {
  14. cvorStrukt polje [10000];
  15. };
  16. cvor ParentB(cvor indeks, bStabloStrukt *binStablo){
  17. if(indeks==1||binStablo->polje[indeks].used==false)
  18. return 0;
  19. else return indeks/2;
  20. }
  21.  
  22. cvor LeftChildB(cvor indeks, bStabloStrukt *binStablo){
  23. if(binStablo->polje[indeks*2].used==false)
  24. return 0;
  25. else return indeks*2;
  26. }
  27.  
  28. cvor RightChildB(cvor indeks, bStabloStrukt *binStablo){
  29. if(binStablo->polje[indeks*2+1].used==false)
  30. return 0;
  31. else return indeks*2+1;
  32. }
  33.  
  34. cvor LabelB(short indeks, bStabloStrukt *binStablo){
  35. return binStablo->polje[indeks].data;
  36. }
  37.  
  38. void ChangeLabelB(cvor unos, short indeks, bStabloStrukt *binStablo){
  39. if(binStablo->polje[indeks].used==false)
  40. return;
  41. else binStablo->polje[indeks].data=unos;
  42. }
  43.  
  44. cvor RootB(bStabloStrukt *binStablo){
  45. return 1;
  46. }
  47.  
  48. bool CreateLeftB(cvor unos, short indeks, bStabloStrukt *binStablo){
  49. if(binStablo->polje[indeks*2].used==true)
  50. return false;
  51. binStablo->polje[indeks*2].used=true;
  52. binStablo->polje[indeks*2].data=unos;
  53. return true;
  54. }
  55.  
  56. bool CreateRightB(cvor unos, short indeks, bStabloStrukt *binStablo){
  57. if(binStablo->polje[indeks*2+1].used==true)
  58. return false;
  59. binStablo->polje[indeks*2+1].used=true;
  60. binStablo->polje[indeks*2+1].data=unos;
  61. return true;
  62. }
  63.  
  64. void DeleteB(short indeks, bStabloStrukt *binStablo){
  65. if(LeftChildB(indeks, binStablo)==true) //krećemo se lijevom stranom počevši od cvora za brisanje
  66. DeleteB(indeks*2, binStablo);
  67. if(RightChildB(indeks, binStablo)==true)//krecemo se desnom stranom
  68. DeleteB(indeks*2+1, binStablo);
  69. binStablo->polje[indeks].used=false;
  70. }
  71.  
  72. void InitB(cvor unos, bStabloStrukt *&binStablo){
  73. binStablo=new bStabloStrukt;
  74. binStablo->polje[1].used=true;
  75. binStablo->polje[1].data=unos;
  76. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.