binarno stablo polje


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



Copy this code and paste it in your HTML
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct element {
  6. int label;
  7. int used;
  8. };
  9.  
  10. struct bt {
  11. struct element elements [10000];
  12. };
  13.  
  14. typedef struct bt *btree;
  15. typedef int node;
  16.  
  17. node ParentB (node n, btree T) {
  18. if (n<2) {
  19. cout << "Greska!" << endl;
  20. return 0;
  21. }
  22. else if (n%2==0) return n/2;
  23. else return n/2-1;
  24. }
  25.  
  26. node LeftChildB (node n, btree T) {
  27. if (T->elements[n].used==0) return 0;
  28. else return 2*n;
  29. }
  30.  
  31. node RightChildB (node n, btree T) {
  32. if (T->elements[n].used==0) return 0;
  33. else return 2*n+1;
  34. }
  35.  
  36. int LabelB (node n, btree T) {
  37. if (T->elements[n].used==0) {
  38. cout << "Greska!" << endl;
  39. return 0;
  40. }
  41. else return T->elements[n].label;
  42. }
  43.  
  44. void ChangeLabelB (int x, node n, btree T) {
  45. if (T->elements[n].used==0) {
  46. cout << "Greska!" << endl;
  47. return;
  48. }
  49. else T->elements[n].label=x;
  50. }
  51.  
  52. node RooTB (btree T) {
  53. if (T->elements[1].used==0) {
  54. cout << "Greska!" << endl;
  55. return 0;
  56. }
  57. else return 1;
  58. }
  59.  
  60. void CreateLeftB (int x, node n, btree T) {
  61. if (T->elements[2*n].used==1 || T->elements[n].used==0) {
  62. cout << "Greska!" << endl;
  63. return;
  64. }
  65. T->elements[2*n].used = 1;
  66. T->elements[2*n].label = x;
  67. }
  68.  
  69. void CreateRightB (int x, node n, btree T) {
  70. if (T->elements[2*n+1].used==1 || T->elements[n].used==0) {
  71. cout << "Greska!" << endl;
  72. return;
  73. }
  74. T->elements[2*n+1].used = 1;
  75. T->elements[2*n+1].label = x;
  76. }
  77.  
  78. void DeleteB (node n, btree T) {
  79. T->elements[n].used=0;
  80. if (LeftChildB(n, T)) DeleteB(LeftChildB(n, T), T);
  81. if (RightChildB(n, T)) DeleteB(RightChildB(n, T), T);
  82. }
  83.  
  84. btree InitB (int x, btree T) {
  85. T = new bt;
  86. for (int i=0; i<10000; i++)
  87. T->elements[i].used=0;
  88. T->elements[1].label=x;
  89. T->elements[1].used=1;
  90. return T;
  91. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.