Posted By

mjug on 01/20/15


Tagged

pokazivaci implementacija


Versions (?)

pokazivaci.h


 / Published in: C++
 

implementacija stabla pomoću pokazivača

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef int labeltype;
  5.  
  6.  
  7. struct bt {
  8. labeltype label;
  9. bt *left, *right; };
  10.  
  11. typedef bt *bs;
  12.  
  13. bs InitB (int k, bt *bs) {
  14. bs->label = k;
  15. bs->left = NULL;
  16. bs->right = NULL; }
  17.  
  18.  
  19. bs RootB (bt *bs) {
  20. if (bs) {
  21. return bs; }
  22. else {
  23. cout << "s ne postoji!" << endl; } }
  24.  
  25. void CreateLeftB (int ime, bt *cvor, bt *bs) {
  26. if (cvor->left != NULL) {
  27. cout << "Dijete vec postoji!" << endl; }
  28. else {
  29. bt *pom = new bt;
  30. pom->right = NULL;
  31. pom->left = NULL;
  32. pom->label = ime;
  33. cvor->left = pom; } }
  34. void CreateRightB (int ime, bt *cvor, bt *s) {
  35. if (cvor->right != NULL) {
  36. cout << "Dijete vec postoji!" << endl; }
  37. else {
  38. bt *pom = new bt;
  39. pom->right = NULL;
  40. pom->left = NULL;
  41. pom->label = ime;
  42. cvor->right = pom; } }
  43. bs FindB (int x, bt *bs) {
  44. bt *tekuci, *tekuci2;
  45. tekuci = bs;
  46. while (tekuci) {
  47. if (tekuci->label == x) {
  48. return tekuci; }
  49. else {
  50. tekuci = tekuci->right; } }
  51. tekuci2 = bs;
  52. while (tekuci2) {
  53. if (tekuci2->label == x) {
  54. return tekuci2; }
  55. else {
  56. tekuci2 = tekuci2->left; } }
  57. cout << "Cvor nije nadjen!" << endl;
  58. bt *zadnji;
  59. zadnji = bs;
  60.  
  61. while (zadnji->left) {
  62. zadnji = zadnji->left; }
  63.  
  64. return zadnji; }
  65.  
  66. int LabelB (bt *cvor, bt *s) {
  67. if (cvor == NULL) {
  68. cout << "Cvor ne postoji!" << endl; }
  69. else {
  70. return cvor->label; } }
  71.  
  72. bs LeftChildB (bt *cvor, bt *bs) {
  73. if (cvor == NULL) {
  74. cout << "Lijevo dijete ne postoji!" << endl; }
  75. else {
  76. return cvor->left; } }
  77. bs ParentB (bt *cvor, bt *bs) {
  78. if (cvor == bs) {
  79. cout << "Cvor je korijen" << endl; }
  80. else {
  81. bt *pom = NULL;
  82.  
  83. if (bs->left) {
  84. if (bs->left == cvor) {
  85. return bs; }
  86. pom = ParentB (cvor, bs->left); }
  87. if (bs->right) {
  88. if (bs->right == cvor) {
  89. return bs; }
  90. pom = ParentB (cvor, bs->right); }
  91. return pom; } }
  92. bs RightChildB (bt *cvor, bt *bs) {
  93. if (cvor == NULL) {
  94. cout << "Desno dijete ne postoji!" << endl; }
  95. else {
  96. return cvor->right; } }
  97. void DeleteB (bt *cvor, bt *bs) {
  98. if (cvor == bs) {
  99. cout << "Korijen se ne moze obrisati!" << endl;
  100. return; }
  101. if (cvor->left != NULL) {
  102. DeleteB (cvor->left, bs); }
  103.  
  104. if (cvor->right != NULL) {
  105. DeleteB (cvor->right, bs); }
  106.  
  107. if (cvor != RootB (bs) && LeftChildB (ParentB (cvor, bs), bs) == cvor) {
  108. ParentB (cvor, bs)->left = NULL; }
  109.  
  110. else if (cvor != RootB (bs) ) {
  111. ParentB (cvor, bs)->right = NULL; }
  112.  
  113. delete cvor; }
  114.  
  115. void ChangeLabelB (int ime, bt *cvor, bt *bs) {
  116. if (!bs) {
  117. cout << "Nije moguce promjeniti!" << endl; }
  118. else {
  119. cvor->label = ime; } }

Report this snippet  

You need to login to post a comment.