Posted By

kapocrnic on 01/06/11


Tagged

sp


Versions (?)

b zadatak


 / Published in: C++
 

  1. // binarno stablo pomocu polja, b zadatak
  2.  
  3. #include <string>
  4.  
  5. struct element {
  6. std::string label;
  7. bool used;
  8. element() {
  9. this->used = false;
  10. }
  11. };
  12.  
  13. struct bt {
  14. element elements[10000];
  15. };
  16.  
  17. typedef bt btree;
  18. typedef int node;
  19.  
  20.  
  21. node ParentB(node n, btree *T) {
  22. return n/2;
  23. }
  24.  
  25.  
  26. node LeftChildB(node n, btree* T) {
  27. if (T->elements[n*2].used)
  28. return n*2;
  29. else
  30. return 0;
  31. }
  32.  
  33. node RightChildB(node n, btree* T) {
  34. if (T->elements[n*2+1].used)
  35. return n*2+1;
  36. else
  37. return 0;
  38. }
  39.  
  40. std::string LabelB(node n, btree* T) {
  41. return T->elements[n].label;
  42. }
  43.  
  44. void ChangeLabelB(std::string x, node n, btree* T) { T->elements[n].label = x; }
  45.  
  46. node RootB(btree *T) { return 1; }
  47.  
  48. bool CreateLeftB(std::string x, node n, btree* T) {
  49. if (!LeftChildB(n, T)) {
  50. T->elements[n*2].label = x;
  51. T->elements[n*2].used = true;
  52. return true;
  53. }
  54. else {
  55. return false;
  56. }
  57. }
  58.  
  59. bool CreateRightB(std::string x, node n, btree *T) {
  60. if (!RightChildB(n, T)) {
  61. T->elements[n*2+1].label = x;
  62. T->elements[n*2+1].used = true;
  63. return true;
  64. }
  65. else {
  66. return false;
  67. }
  68. }
  69.  
  70. void DeleteB(node n, btree *T) {
  71. if (n*2 < 10000 && T->elements[n*2].used) {
  72. DeleteB(n*2, T);
  73. }
  74. if (n*2+1 < 10000 && T->elements[n*2+1].used) {
  75. DeleteB(n*2+1, T);
  76. }
  77. T->elements[n].used = false;
  78. }
  79.  
  80. void InitB(std::string x, btree *T) {
  81. T->elements[1].label = x;
  82. T->elements[1].used = true;
  83. }

Report this snippet  

You need to login to post a comment.