Posted By


sasacalopa on 01/06/11

Tagged


Statistics


Viewed 401 times
Favorited by 0 user(s)

bstablo_pokazivac.h


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

Binarno stablo, implementacija pomoću pokazivaća (.h)


Copy this code and paste it in your HTML
  1. #ifndef BSTABLO_POLJE_H
  2.  
  3. #ifndef BSTABLO_POKAZIVAC_H
  4. #define BSTABLO_POKAZIVAC_H
  5.  
  6. struct element{
  7. labeltype label;
  8. struct element *left,*right;
  9. };
  10. typedef struct element *node;
  11. typedef struct element *btree;
  12.  
  13. btree InitB(labeltype x,btree T){
  14. T = new element;
  15. T->label = x;
  16. T->left = NULL;
  17. T->right = NULL;
  18. return T;
  19. }
  20. node RootB(btree T){
  21. return T;
  22. }
  23. bool ExistsLeftChildB(node n,btree T){
  24. if(T==NULL) return LAMBDA;
  25. if(n->left != NULL) return true;
  26. return false;
  27. }
  28. bool ExistsRightChildB(node n,btree T){
  29. if(T==NULL) return LAMBDA;
  30. if(n->right != NULL) return true;
  31. return false;
  32. }
  33. node LeftChildB(node n,btree T){
  34. return n->left;
  35. }
  36. node RightChildB(node n,btree T){
  37. return n->right;
  38. }
  39. labeltype LabelB(node n,btree T){
  40. return n->label;
  41. }
  42. int CreateLeftB(labeltype x,node n,btree T){
  43. element *novi = new element;
  44. n->left = novi;
  45. novi->left = NULL;
  46. novi->right = NULL;
  47. novi->label = x;
  48. return 0;
  49. }
  50. int CreateRightB(labeltype x,node n,btree T){
  51. element *novi = new element;
  52. n->right = novi;
  53. novi->left = NULL;
  54. novi->right = NULL;
  55. novi->label = x;
  56. return 0;
  57. }
  58. node ParentB(node n,btree T){
  59. node cvor = NULL;
  60. if(n == T) return 0;
  61. if(T->left){
  62. if(T->left == n){
  63. cvor = T;
  64. return cvor;
  65. }
  66. cvor = ParentB(n,T->left);
  67. }
  68. if(!cvor)
  69. if(T->right){
  70. if(T->right == n){
  71. cvor = T;
  72. return cvor;
  73. }
  74. cvor = ParentB(n,T->right);
  75. }
  76. return cvor;
  77. }
  78. void DeleteB(node n,btree T){
  79. if(n->left) DeleteB(n->left,T);
  80. if(n->right) DeleteB(n->right,T);
  81. node cvor = ParentB(n,T);
  82. if(cvor!=0){
  83. if(cvor->left == n) cvor->left = NULL;
  84. else cvor->right=NULL;
  85. delete n;
  86. }
  87. }
  88. void ChangeLabelB(labeltype x,node n,btree T){
  89. n->label = x;
  90. }
  91.  
  92. #endif /*BSTABLO_POKAZIVAC_H*/
  93.  
  94. #endif /*BSTABLO_POLJE_H*/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.