Posted By

sargaxon on 01/19/15


Tagged

binarno stablo


Versions (?)

bstablo_polje


 / Published in: C++
 

strukture podataka

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int bstablo[1000];
  5.  
  6. int RootB (int bstablo[]) {
  7. return 1;
  8. }
  9.  
  10. int LabelB (int n, int bstablo[]) {
  11. return bstablo[n];
  12. }
  13.  
  14. int ParentB (int n, int bstablo[]) {
  15. if (n==RootB(bstablo))
  16. return -1;
  17. if (n%2)
  18. return (n-1)/2;
  19. else
  20. return n/2;
  21. }
  22.  
  23. int LeftChildB (int n, int bstablo[]) {
  24. if (LabelB(n*2, bstablo)==-1)
  25. return -1;
  26. return n*2;
  27. }
  28.  
  29. int RightChildB (int n, int bstablo[]) {
  30. if (LabelB(n*2+1, bstablo)==-1)
  31. return -1;
  32. return n*2+1;
  33. }
  34.  
  35. void ChangeLabelB (int x, int n, int bstablo[]) {
  36. bstablo[n] = x;
  37. }
  38.  
  39. bool CreateLeftB (int x, int n, int bstablo[]) {
  40. if (LeftChildB(n, bstablo)!=-1)
  41. return false;
  42. bstablo[n*2] = x;
  43. return true;
  44. }
  45.  
  46. bool CreateRightB (int x, int n, int bstablo[]) {
  47. if (RightChildB(n, bstablo)!=-1)
  48. return false;
  49. bstablo[n*2+1] = x;
  50. return true;
  51. }
  52.  
  53. void DeleteB (int n, int bstablo[]) {
  54. if (LeftChildB(n, bstablo)!=-1)
  55. DeleteB(LeftChildB(n, bstablo), bstablo);
  56. if (RightChildB(n, bstablo)!=-1)
  57. DeleteB(RightChildB(n, bstablo), bstablo);
  58. bstablo[n]=-1;
  59. }
  60.  
  61. void InitB (int n, int bstablo[]) {
  62. for (int i=0; i<1000; i++)
  63. bstablo[i] = -1;
  64. bstablo[1] = n;
  65. }

Report this snippet  

You need to login to post a comment.