Posted By

Mata4004 on 01/19/15


Tagged

tree #binary


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Mata4004


stablo_pokazivac.h


 / Published in: C++
 

URL: http://snipplr.com/view/87767/stablopokazivach/

Pokazivac

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct element {
  5. int label;
  6. element *left, *right;
  7. };
  8.  
  9. element *bstablo = new element;
  10.  
  11. element *Rootb (element *bstablo) {
  12. return bstablo;
  13. }
  14.  
  15. int Labelb (element *el) {
  16. return el->label;
  17. }
  18.  
  19. element *Parentb (element *search, element *bstablo) {
  20. element *pomocni1=NULL, *pomocni2=NULL;
  21. if (bstablo->left == search || bstablo->right == search)
  22. return bstablo;
  23. if (bstablo->left != NULL)
  24. pomocni1 = Parentb(search,bstablo->left);
  25. if (bstablo->right != NULL)
  26. pomocni2 = Parentb(search, bstablo->right);
  27. if (pomocni1 != NULL)
  28. return pomocni1;
  29. if (pomocni2 != NULL)
  30. return pomocni2;
  31. return NULL;
  32. }
  33.  
  34. element *LeftChildb (element *el) {
  35. return el->left;
  36. }
  37.  
  38. element *RightChildb (element *el) {
  39. return el->right;
  40. }
  41.  
  42. void ChangeLabelb (int value, element *el) {
  43. el->label = value;
  44. }
  45.  
  46. bool CreateLeftb (int value, element *el) {
  47. if (LeftChildb(el) != NULL)
  48. return false;
  49. element *novi = new element;
  50. el->left = novi;
  51. novi->label = value;
  52. novi->left = NULL;
  53. novi->right = NULL;
  54. return true;
  55. }
  56.  
  57. bool CreateRightb (int value, element *el) {
  58. if (RightChildb(el) != NULL)
  59. return false;
  60. element *novi = new element;
  61. el->right = novi;
  62. novi->label = value;
  63. novi->left = NULL;
  64. novi->right = NULL;
  65. return true;
  66. }
  67.  
  68. void Deleteb (element *el, element *stablo) {
  69. if (el->left != NULL)
  70. Deleteb(el->left, bstablo);
  71. if (el->right != NULL)
  72. Deleteb(el->right,bstablo);
  73. if (el != Rootb(stablo) && LeftChildb(Parentb(el, bstablo))==el)
  74. Parentb(el, bstablo)->left = NULL;
  75. else if (el != Rootb(bstablo))
  76. Parentb(el, bstablo)->right = NULL;
  77. delete el;
  78. }
  79.  
  80. void Initb (int value, element *bstablo) {
  81. bstablo->right = NULL;
  82. bstablo->left = NULL;
  83. bstablo->label = value;
  84. }
  85.  
  86. element *Search (element *el, int value) {
  87. if (el->label == value)
  88. return el;
  89. if (el->left != NULL)
  90. if (Search(el->left, value) != NULL)
  91. return Search(el->left, value);
  92. if (el->right != NULL)
  93. if (Search(el->right, value) != NULL)
  94. return Search(el->right, value);
  95. return NULL;
  96. }

Report this snippet  

You need to login to post a comment.