Posted By


sara on 01/05/11

Tagged


Statistics


Viewed 358 times
Favorited by 0 user(s)

Related snippets


bstablo_pokazivaci.h


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



Copy this code and paste it in your HTML
  1. #include <stdlib.h>
  2. #define Null NULL
  3. typedef int labeltype;
  4.  
  5. typedef struct element {
  6. labeltype label;
  7. struct element *left,*right,*parrent;
  8. }*btree, *node;
  9.  
  10. btree InitB (labeltype x){
  11. btree myBTree;
  12.  
  13. myBTree=(btree)malloc(sizeof(struct element));
  14. myBTree->left=NULL;
  15. myBTree->right=NULL;
  16. myBTree->parrent=NULL;
  17. myBTree->label=x;
  18.  
  19. return myBTree;
  20. }
  21.  
  22. node RootB(btree T){
  23. return (node)T;
  24. }
  25.  
  26. node ParrentB(node n, btree T){
  27. if(n==T){
  28. printf("Zatrazili ste roditelja korjenskog cvora\n");
  29. return Null;
  30. }else{
  31. return n->parrent;
  32. }
  33. }
  34.  
  35. node LeftChildB(node n, btree T){
  36. if(n->left==NULL){
  37. return Null;
  38. }else{
  39. return n->left;
  40. }
  41. }
  42.  
  43. node RightChildB(node n, btree T){
  44. if(n->right==NULL){
  45. return Null;
  46. }else{
  47. return n->right;
  48. }
  49. }
  50.  
  51. labeltype LabelB(node n, btree T){
  52. return n->label;
  53. }
  54.  
  55. void ChangeLabelB(labeltype x, node n, btree T){
  56. n->label=x;
  57. }
  58.  
  59. void CreateLeftB(labeltype x, node n, btree T){
  60. node child;
  61.  
  62. if(n->left!=NULL){
  63. printf("Lijevo dijete ovog cvora vec postoji\n");
  64. exit(1);
  65. }
  66.  
  67. child=(node)malloc(sizeof(struct element));
  68. child->left=NULL;
  69. child->right=NULL;
  70. child->parrent=n;
  71. n->left=child;
  72. child->label=x;
  73. }
  74.  
  75. void CreateRightB(labeltype x, node n, btree T){
  76. node child;
  77.  
  78. if(n->right!=NULL){
  79. printf("Desno dijete ovog cvora vec postoji\n");
  80. exit(1);
  81. }
  82.  
  83. child=(node)malloc(sizeof(struct element));
  84. child->left=NULL;
  85. child->right=NULL;
  86. child->parrent=n;
  87. n->right=child;
  88. child->label=x;
  89. }
  90.  
  91. void DeleteBRek(node n, btree T){
  92. if( n == NULL )
  93. return;
  94. DeleteBRek(LeftChildB(n, T), T);
  95.  
  96. node parrent;
  97. parrent=n->parrent;
  98.  
  99. if(parrent->left==n)
  100. parrent->left=NULL;
  101. if(parrent->right==n)
  102. parrent->right=NULL;
  103.  
  104. free(n);
  105. n=parrent;
  106.  
  107. DeleteBRek(RightChildB(n, T), T);
  108. }
  109.  
  110. void DeleteB(node n, btree T ){
  111. if(n->parrent==NULL){
  112. printf("Ne mogu obrisati korjen\n");
  113. exit(1);
  114. }
  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. DeleteBRek(n, T);
  125. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.