Posted By

kapocrnic on 01/06/11


Tagged

sp


Versions (?)

c zadatak


 / Published in: C++
 

  1. #include <string>
  2. #include <vector>
  3.  
  4.  
  5.  
  6. struct element {
  7. std::string label;
  8. element *left,*right;
  9. element() {
  10. this->left = NULL;
  11. this->right = NULL;
  12. }
  13. };
  14.  
  15.  
  16. typedef element *node;
  17. typedef element btree;
  18.  
  19.  
  20.  
  21.  
  22.  
  23. node LeftChildB(node n, btree *T) {
  24. return n->left;
  25. }
  26.  
  27. node RightChildB(node n, btree *T) {
  28. return n->right;
  29. }
  30.  
  31. std::string LabelB(node n, btree *T) { return n->label; }
  32.  
  33. void ChangeLabelB(std::string x, node n, btree *T) { n->label = x; }
  34.  
  35. node RootB(btree *T) { return T; }
  36.  
  37. bool CreateLeftB(std::string x, node n, btree *T) {
  38. if (!LeftChildB(n, T)) {
  39. node newborn = new element;
  40. newborn->label = x;
  41. n->left = newborn;
  42. return true;
  43. }
  44. else {
  45. return false;
  46. }
  47. }
  48.  
  49. bool CreateRightB(std::string x, node n, btree *T) {
  50. if (!RightChildB(n, T)) {
  51. node newborn = new element;
  52. newborn->label = x;
  53. n->right = newborn;
  54. return true;
  55. }
  56. else {
  57. return false;
  58. }
  59. }
  60.  
  61. node ParentB(node n, btree *T) {
  62.  
  63. std::vector<node> lista;
  64. node tmp = RootB(T);
  65. lista.push_back(tmp);
  66.  
  67. do {
  68. tmp = lista[0];
  69. if (LeftChildB(tmp, T) == n || RightChildB(tmp, T) == n)
  70. return tmp;
  71. if (LeftChildB(tmp, T) != NULL) {
  72. lista.push_back(LeftChildB(tmp, T));
  73. }
  74. if (RightChildB(tmp, T) != NULL) {
  75. lista.push_back(RightChildB(tmp, T));
  76. }
  77. lista.erase(lista.begin());
  78. } while (lista.size() > 0);
  79.  
  80. return NULL;
  81. }
  82.  
  83. void DeleteB(node n, btree *T) {
  84.  
  85. if (n->left) {
  86. DeleteB(n->left, T);
  87. }
  88.  
  89. if (n->right ) {
  90. DeleteB(n->right, T);
  91. }
  92.  
  93. node tmp = ParentB(n, T);
  94. if (n == LeftChildB(tmp, T)) {
  95. tmp->left = NULL;
  96. }
  97. else {
  98. tmp->right = NULL;
  99. }
  100.  
  101.  
  102. delete n;
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109. void InitB(std::string x, btree *T) {
  110. T->label = x;
  111. }

Report this snippet  

You need to login to post a comment.