Guest User

Untitled

a guest
Jul 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #define DULJ 100
  2.  
  3. typedef int TIP;
  4. class elem
  5. {
  6. public:
  7. TIP vrij;
  8. bool used;
  9.  
  10. elem()
  11. {
  12. used = false;
  13. }
  14. }; //class elem
  15.  
  16. struct bt
  17. {
  18. elem el[DULJ];
  19. }; //struct bt
  20.  
  21.  
  22. int ParentB(int n, bt T)
  23. {
  24. if(!T.el[n].used) return -1;
  25. if((n % 2) == 0) return (n/2);
  26. else return((n-1)/2);
  27. } //int ParentB(int n, bt T)
  28.  
  29.  
  30. int LeftChildB(int n, bt T)
  31. {
  32. if(!T.el[n].used) return -1;
  33. int r = n * 2;
  34. if(r > DULJ || !T.el[r].used) return -1;
  35. else return r;
  36. } //int LeftChildB(int n, bt T)
  37.  
  38.  
  39. int RightChildB(int n, bt T)
  40. {
  41. if(!T.el[n].used) return -1;
  42. int r = n * 2 + 1;
  43. if(r >= DULJ || !T.el[r].used) return -1;
  44. else return r;
  45. } //int RightChildB(int n, bt T)
  46.  
  47.  
  48. TIP LabelB(int n, bt T)
  49. {
  50. if(!T.el[n].used) return -1;
  51. else return T.el[n].vrij;
  52. } //int LabelB(int n, bt T)
  53.  
  54.  
  55. int ChangeLabelB(TIP x, int n, bt &T)
  56. {
  57. if(!T.el[n].used) return -1;
  58. else T.el[n].vrij = x;
  59. return 0;
  60. } //int ChangeLabelB(int x, int n, bt &T)
  61.  
  62.  
  63. int RootB(bt T)
  64. {
  65. if(!T.el[1].used) return -1;
  66. else return 1;
  67. } //int RootB(bt T)
  68.  
  69.  
  70. int CreateLeftB(TIP x, int n, bt &T)
  71. {
  72. if(!T.el[n].used) return -1;
  73. int r = n * 2;
  74.  
  75. if(T.el[r].used || r >= DULJ) return -1;
  76. T.el[r].used = true;
  77. T.el[r].vrij = x;
  78. return 0;
  79. } //int CreateLeftB(int x, int n, bt &T)
  80.  
  81.  
  82. int CreateRightB(TIP x, int n, bt &T)
  83. {
  84. if(!T.el[n].used) return -1;
  85. int r = (n * 2) + 1;
  86.  
  87. if(T.el[r].used || r >= DULJ) return -1;
  88. T.el[r].used = true;
  89. T.el[r].vrij = x;
  90. return 0;
  91. } //int CreateRightB(int x, int n, bt &T)
  92.  
  93.  
  94. void DeleteB(int n, bt &T)
  95. {
  96. if(n == -1) return;
  97. if(!T.el[n].used) return;
  98.  
  99. DeleteB(LeftChildB(n, T), T);
  100. DeleteB(RightChildB(n, T), T);
  101. T.el[n].used = false;
  102. } //int DeleteB(int n, bt &T)
  103.  
  104.  
  105. void InitB(TIP x, bt &T)
  106. {
  107. if(T.el[1].used) DeleteB(RootB(T), T);
  108. T.el[1].used = true;
  109. T.el[1].vrij = x;
  110. } //void InitB(int x, bt &T)
Add Comment
Please, Sign In to add comment