Posted By

akljaic on 01/19/15


Tagged

tree binary


Versions (?)

binarno_pokazivac.h


 / Published in: C++
 

URL: asdf

asdf

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct tr{
  5. int label;
  6. tr *left, *right;
  7. };
  8.  
  9. bool ExistLeftChild(tr *n, tr *T){
  10. if(n->left == NULL) return false;
  11. return true;
  12. }
  13.  
  14. bool ExistRightChild(tr *n, tr *T){
  15. if(n->right == NULL) return false;
  16. return true;
  17. }
  18.  
  19. tr *ParentB(tr *n, tr *T){
  20. tr *cvor;
  21. bool nadjen = false;
  22. if(T->left == n || T->right == n){
  23. nadjen = true;
  24. cvor = T;
  25. }
  26. if(nadjen) return cvor;
  27. if(T->left) ParentB(n,T->left);
  28. if(T->right) ParentB(n,T->right);
  29. }
  30.  
  31. tr *LeftChildB(tr *n, tr *T){
  32. return n->left;
  33. }
  34.  
  35. tr *RightChildB(tr *n, tr *T){
  36. return n->right;
  37. }
  38.  
  39. int LabelB(tr *n, tr *T){
  40. return n->label;
  41. }
  42.  
  43. void ChangeLabelB(int x, tr *n, tr *T){
  44. n->label = x;
  45. }
  46.  
  47. tr *RootB(tr *T){
  48. return T;
  49. }
  50.  
  51. void CreateLeftB(int x, tr *n, tr *T){
  52. if(n->left) cout << "Ima vec lijevo dijete!" << endl;
  53. else {
  54. n->left = new tr;
  55. n->left->label = x;
  56. n->left->left = NULL;
  57. n->left->right = NULL;
  58. }
  59. }
  60.  
  61. void CreateRightB(int x, tr *n, tr *T){
  62. if(n->right) cout << "Ima vec desno dijete!" << endl;
  63. else {
  64. n->right = new tr;
  65. n->right->label = x;
  66. n->right->left = NULL;
  67. n->right->right = NULL;
  68. }
  69. }
  70.  
  71. void DeleteB(tr *n, tr *T){
  72. tr *parent = ParentB(n,T);
  73. if(parent->left == n)
  74. parent->left = NULL;
  75. else
  76. parent->right = NULL;
  77. if(n->left)
  78. DeleteB(n->left, T);
  79. if(n->right)
  80. DeleteB(n->right, T);
  81. delete n;
  82. }
  83.  
  84. tr *InitB(int x, tr *T){
  85. T->left = NULL;
  86. T->right = NULL;
  87. T->label = x;
  88. return T;
  89. }

Report this snippet  

You need to login to post a comment.