Posted By


fstrunja on 01/12/14

Tagged


Statistics


Viewed 423 times
Favorited by 0 user(s)

bstablo_pokazivac.h


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

Implementacija binarnog stabla pomoću pokazivača


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.