Posted By

sanzivode on 01/06/11


Tagged

polje stablo


Versions (?)

Binarno stablo_polje


 / Published in: C++
 

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct element {
  5. int label;
  6. bool used;
  7. };
  8.  
  9. struct bt {
  10. element elements[10000];
  11. };
  12.  
  13. int ParentB(int n, bt* T) {
  14. if (n == 1) return 0;
  15. if (n%2) n--;
  16. return n/2;
  17. }
  18.  
  19. int LeftChildB(int n, bt* T) {
  20. if (T->elements[n].used == 0) return 0;
  21. if (T->elements[2*n].used == 1) return 2*n;
  22. return 0;
  23. }
  24.  
  25. int RightChildB(int n, bt* T) {
  26. if (T->elements[n].used == 0) return 0;
  27. if (T->elements[2*n+1].used == 1) return 2*n+1;
  28. return 0;
  29. }
  30.  
  31. int LabelB(int n, bt* T) {
  32. return T->elements[n].label;
  33. }
  34.  
  35. void ChangeLabelB(int x, int n, bt* T) {
  36. if(T->elements[n].used == 1)
  37. T->elements[n].label = x;
  38. }
  39.  
  40. int RootB(bt* T) {
  41. if(T->elements[1].used == 0) return 0;
  42. return 1;
  43. }
  44.  
  45. void CreateLeftB(int x, int n, bt* T) {
  46. if(T->elements[n].used == 0 || T->elements[2*n].used == 1) {
  47. cout << "Poruka greske" << endl;
  48. return;
  49. }
  50. T->elements[2*n].used = 1;
  51. T->elements[2*n].label = x;
  52. }
  53.  
  54. void CreateRightB(int x, int n, bt* T) {
  55. if(T->elements[n].used == 0 || T->elements[2*n+1].used == 1) {
  56. cout << "Poruka greske" << endl;
  57. return;
  58. }
  59. T->elements[2*n+1].used = 1;
  60. T->elements[2*n+1].label = x;
  61. }
  62.  
  63. void DeleteB(int n, bt* T) {
  64. T->elements[n].used = 0;
  65. if (LeftChildB(n,T)) DeleteB(LeftChildB(n,T),T);
  66. if (RightChildB(n,T)) DeleteB(RightChildB(n,T),T);
  67. }
  68.  
  69. bt* InitB(int x, bt* T) {
  70. T = new bt;
  71. T->elements[1].label = x;
  72. T->elements[1].used = 1;
  73. for(int i = 2; i < 10000; i++)
  74. T->elements[i].used = 0;
  75. return T;
  76. }

Report this snippet  

You need to login to post a comment.