Posted By


hcosic2 on 01/18/15

Tagged


Statistics


Viewed 80 times
Favorited by 0 user(s)

binstablo_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. {
  6. int label;
  7. element *lijevi, *desni;
  8. };
  9.  
  10. element *Tbin = new element;
  11.  
  12. element *Root (element *Tbin)
  13. {
  14. return Tbin;
  15. }
  16.  
  17. element *pretrazivanje (element *el, int x) {
  18. if (el -> label == x)
  19. return el;
  20. if (el -> lijevi != NULL)
  21. if (pretrazivanje (el -> lijevi, x) != NULL)
  22. return pretrazivanje (el -> lijevi, x);
  23. if (el -> desni != NULL)
  24. if (pretrazivanje (el -> desni, x) != NULL)
  25. return pretrazivanje (el -> desni, x);
  26. return NULL;
  27. }
  28.  
  29. int Label (element *el)
  30. {
  31. return el -> label;
  32. }
  33.  
  34. element *Parent (element *n, element *Tbin)
  35. {
  36. element *child = NULL, *sibling = NULL;
  37. if (Tbin -> lijevi == n || Tbin -> desni == n)
  38. return Tbin;
  39. if (Tbin -> lijevi != NULL)
  40. child = Parent (n, Tbin -> lijevi);
  41. if (Tbin -> desni != NULL)
  42. sibling = Parent (n, Tbin -> desni);
  43. if (child != NULL)
  44. return child;
  45. if (sibling != NULL)
  46. return sibling;
  47. return NULL;
  48. }
  49.  
  50. element *LeftChild (element *el)
  51. {
  52. return el -> lijevi;
  53. }
  54.  
  55. element *RightChild (element *el)
  56. {
  57. return el -> desni;
  58. }
  59.  
  60. void ChangeLabel (int x, element *el)
  61. {
  62. el -> label = x;
  63. }
  64.  
  65. int CreateRight (int x, element *el)
  66. {
  67. if (RightChild (el) != NULL)
  68. return 0;
  69. element *novi = new element;
  70. el -> desni = novi;
  71. novi -> label = x;
  72. novi -> lijevi = NULL;
  73. novi -> desni = NULL;
  74. return 1;
  75. }
  76.  
  77. int CreateLeft (int x, element *el)
  78. {
  79. if (LeftChild (el) != NULL)
  80. return 0;
  81. element *novi = new element;
  82. el -> lijevi = novi;
  83. novi -> label = x;
  84. novi -> lijevi = NULL;
  85. novi -> desni = NULL;
  86. return 1;
  87. }
  88.  
  89. void Delete (element *el, element *Tbin)
  90. {
  91. if (el -> lijevi != NULL)
  92. Delete (el -> lijevi, Tbin);
  93. if (el -> desni != NULL)
  94. Delete (el -> desni, Tbin);
  95. if (el != Root (Tbin) && LeftChild (Parent (el, Tbin)) == el)
  96. Parent (el, Tbin) -> desni = NULL;
  97. else if (el != Root (Tbin))
  98. Parent (el, Tbin) -> desni = NULL;
  99. delete el;
  100. }
  101.  
  102. void Init (int x, element *Tbin)
  103. {
  104. Tbin -> label = x;
  105. Tbin -> lijevi = NULL;
  106. Tbin -> desni = NULL;
  107. }
  108.  
  109. void PreOrder (element *n, element *Tbin)
  110. {
  111. if (n == NULL)
  112. return;
  113. cout << n -> label << ", ";
  114. PreOrder (LeftChild (n), Tbin);
  115. PreOrder (RightChild (n), Tbin);
  116. }
  117.  
  118. void InOrder (element *n, element *Tbin)
  119. {
  120. if (n == NULL)
  121. return;
  122. InOrder (LeftChild (n), Tbin);
  123. cout << n -> label << ", ";
  124. InOrder (RightChild (n), Tbin);
  125. }
  126.  
  127. void PostOrder (element *n, element *Tbin)
  128. {
  129. if (n == NULL)
  130. return;
  131. PostOrder (LeftChild (n), Tbin);
  132. PostOrder (RightChild (n), Tbin);
  133. cout << n -> label << ", ";
  134. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.