Posted By

sam085 on 01/06/11


Tagged


Versions (?)

binarno stablo - pokazivac


 / Published in: C++
 

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. typedef struct element
  6. {
  7. int label;
  8. struct element *left,*right,*parrent;
  9. } *btree, *node;
  10.  
  11. btree init(int x)
  12. {
  13. btree myBTree;
  14.  
  15. myBTree=(btree)malloc(sizeof(struct element));
  16. myBTree->left=NULL;
  17. myBTree->right=NULL;
  18. myBTree->parrent=NULL;
  19. myBTree->label=x;
  20.  
  21. return myBTree;
  22. }
  23.  
  24. node root(btree T){
  25. return T;
  26. }
  27.  
  28. node parrent(node n, btree T)
  29. {
  30. if(n==T)
  31. {
  32. cout << "Zatrazili ste roditelja korjenskog cvora\n" ;
  33. return NULL;
  34. }
  35. else
  36. {
  37. return n->parrent;
  38. }
  39. }
  40.  
  41. node leftChild(node n, btree T)
  42. {
  43. if(n->left == NULL)
  44. {
  45. return NULL;
  46. }
  47. else
  48. {
  49. return n->left;
  50. }
  51. }
  52.  
  53. node rightChild(node n, btree T)
  54. {
  55. if(n->right == NULL)
  56. {
  57. return NULL;
  58. }
  59. else
  60. {
  61. return n->right;
  62. }
  63. }
  64.  
  65. int label(node n, btree T)
  66. {
  67. return n->label;
  68. }
  69.  
  70. void changeLabel(int x, node n, btree T)
  71. {
  72. n->label = x;
  73. }
  74.  
  75. void createLeft(int x, node n, btree T)
  76. {
  77. node child;
  78.  
  79. if(n->left != NULL)
  80. {
  81. cout << "Lijevo dijete ovog cvora vec postoji\n";
  82. return;
  83. }
  84.  
  85. child = (node)malloc(sizeof(struct element));
  86. child->left = NULL;
  87. child->right = NULL;
  88. child->parrent = n;
  89. n->left = child;
  90. child->label = x;
  91. }
  92.  
  93. void createRight(int x, node n, btree T)
  94. {
  95. node child;
  96.  
  97. if(n->right != NULL)
  98. {
  99. cout << "Desno dijete ovog cvora vec postoji\n";
  100. return;
  101. }
  102.  
  103. child = (node)malloc(sizeof(struct element));
  104. child->left = NULL;
  105. child->right = NULL;
  106. child->parrent = n;
  107. n->right = child;
  108. child->label = x;
  109. }
  110.  
  111. void deleteN(node n, btree T){
  112. if( n == NULL )
  113. return;
  114. deleteN(leftChild(n, T), T);
  115.  
  116. node parrent;
  117. parrent = n->parrent;
  118.  
  119. if(parrent->left == n)
  120. parrent->left = NULL;
  121. if(parrent->right == n)
  122. parrent->right=NULL;
  123.  
  124. free(n);
  125. n = parrent;
  126.  
  127. deleteN(rightChild(n, T), T);
  128. }
  129.  
  130. void deleteNode(node n, btree T ){
  131. if(n->parrent==NULL)
  132. {
  133. cout << "Ne mogu obrisati korjen\n";
  134. return;
  135. }
  136.  
  137. node parrent;
  138. parrent = n->parrent;
  139.  
  140. if(parrent->left == n)
  141. parrent->left = NULL;
  142. if(parrent->right == n)
  143. parrent->right = NULL;
  144.  
  145. deleteN(n, T);
  146. }

Report this snippet  

You need to login to post a comment.