Posted By

Sibabic on 01/06/11


Tagged


Versions (?)

Zadatak 4 Implementacija binarnog stabla pomocu pokazivaca


 / Published in: C++
 

  1. typedef struct element
  2. {
  3. labeltype label;
  4. struct element *left,*right;
  5. } bt;
  6.  
  7. typedef struct element *node;
  8. typedef struct element *bTree;
  9.  
  10. node p;
  11.  
  12. node RootB(bTree T)
  13. {
  14. return T;
  15. };
  16.  
  17. node LeftChildB(node n, bTree T)
  18. {
  19. return n->left;
  20. };
  21.  
  22. node RightChildB(node n, bTree T)
  23. {
  24. return n->right;
  25. };
  26.  
  27. labeltype LabelB(node n, bTree T)
  28. {
  29. return n->label;
  30. };
  31.  
  32. int ExistsLeftChild(node n, bTree T)
  33. {
  34. if (n->left == NULL)
  35. return 0;
  36. else
  37. return 1;
  38. };
  39.  
  40. int ExistsRightChild(node n, bTree T)
  41. {
  42. if (n->right == NULL)
  43. return 0;
  44. else
  45. return 1;
  46. };
  47.  
  48. void ChangeLabelB(labeltype x, node n, bTree T)
  49. {
  50. n->label = x;
  51. };
  52.  
  53. void CreateLeftB(labeltype x, node n, bTree T)
  54. {
  55. element *tmp = new element;
  56. n->left = tmp;
  57. tmp->left = NULL;
  58. tmp->right = NULL;
  59. tmp->label = x;
  60. };
  61.  
  62. void CreateRightB(labeltype x, node n, bTree T)
  63. {
  64. element *tmp=new element;
  65. n->right = tmp;
  66. tmp->left = NULL;
  67. tmp->right = NULL;
  68. tmp->label = x;
  69. };
  70.  
  71. void SearchParentInternal(node n, bTree T)
  72. {
  73. if (LeftChildB(T,T) != NULL)
  74. if (T->left == n)
  75. p = T;
  76. else
  77. SearchParentInternal(n, T->left);
  78. if (RightChildB(T,T) != NULL)
  79. if (T->right == n)
  80. p = T;
  81. else
  82. SearchParentInternal(n, T->right);
  83. };
  84.  
  85. node ParentB(node n, bTree T)
  86. {
  87. SearchParentInternal(n,T);
  88. return p;
  89. };
  90.  
  91. void DeleteB(node n, bTree T)
  92. {
  93. node tmp;
  94. if (n != RootB(T))
  95. {
  96. tmp = ParentB(n,T);
  97. if(tmp->left == n)
  98. tmp->left = NULL;
  99. else
  100. tmp->right = NULL;
  101. }
  102. if(n->left != NULL)
  103. DeleteB(n->left, T);
  104. if(n->right != NULL)
  105. DeleteB(n->right, T);
  106. delete n;
  107. };
  108.  
  109. void InitB(labeltype x, bTree T)
  110. {
  111. T->label = x;
  112. T->left = NULL;
  113. T->right = NULL;
  114. };

Report this snippet  

You need to login to post a comment.