Posted By


marmomcil on 01/06/11

Tagged


Statistics


Viewed 439 times
Favorited by 0 user(s)

bstablo_pokazivac.h - marmomcil


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

Header file bstablo_pokazivac.h, odn. header file implementacija binarnog stabla pomoću pokazivača


Copy this code and paste it in your HTML
  1. struct element{
  2. labeltype oznaka;
  3. struct element *left,*right;
  4. };
  5.  
  6. typedef struct element *node;
  7. typedef struct element *tree;
  8.  
  9. node ParentB(node n,tree stablo){
  10. node i;
  11. int OK=0;
  12. stack S;
  13. MakeNullS(S);
  14. PushS(stablo,S);
  15. while((!IsEmptyS(S)) && (!OK)){
  16. i=PopS(S);
  17. if (i->left!=n) && (i->right!=n)){
  18. if (i->left!=0) PushS(i->left);
  19. if (i->right!=0) PushS(i->right);
  20. else OK=-1;
  21. }
  22. if(OK) return i;
  23. else return 0;
  24. }
  25.  
  26. node LeftChildB(node n,tree stablo){
  27. return n->left;
  28. }
  29.  
  30. node RightChildB(node n,tree stablo){
  31. return n->right;
  32. }
  33.  
  34. labeltype LabelB(node n,tree stablo){
  35. return n->oznaka;
  36. }
  37.  
  38. void ChangeLabelB(labeltype x, node n,tree stablo){
  39. n->oznaka=x;
  40. }
  41.  
  42. node RootB(tree stablo){
  43. return stablo;
  44. }
  45.  
  46. void CreateLeftB(labeltype x,node n,tree stablo){
  47. node l;
  48. if(n->left!=0){
  49. l=(struct element *)malloc(sizeof(struct element));
  50. l->left=l->right=0;
  51. l->oznaka=x;
  52. n->left=l;
  53. }
  54. else return;
  55. }
  56.  
  57. void CreateRightB(labeltype x,node n,tree stablo){
  58. node l;
  59. if (n->right!=0){
  60. l=(struct element *)malloc(sizeof(struct element));
  61. l->left=l->right=0;
  62. l->oznaka=x;
  63. n->right=l;
  64. }
  65. else return;
  66. }
  67.  
  68. void DelB(node n, tree stablo){
  69. if (n->left!=0) DelB((n->left,stablo);
  70. if (n->right!=0) DelB((n->right,stablo);
  71. free(n);
  72. }
  73.  
  74. void DeleteB(node n,tree stablo){
  75. node l;
  76. if (n->left!=0) DelB((n->left,stablo);
  77. if (n->right!=0) DelB((n->right,stablo);
  78. l=ParentB(n,stablo);
  79. if(l->left==n) l->left=0;
  80. else l->right=0;
  81. free(n);
  82. }
  83.  
  84. void InitB(node x, tree *stablo){
  85. node l;
  86. l=(struct element *)malloc(sizeof(struct element));
  87. l->left=l->right=0;
  88. l->oznaka=x;
  89. (*stablo)=l;
  90. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.