Posted By


anblazevi on 01/06/11

Tagged


Statistics


Viewed 540 times
Favorited by 0 user(s)

bstablo_polje.h


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



Copy this code and paste it in your HTML
  1. struct element
  2. {
  3. labeltype label;
  4. int left, right, available;
  5. };
  6.  
  7. typedef struct bt
  8. {
  9. element data [10000];
  10. int current;
  11. } *bTree;
  12.  
  13. typedef int node;
  14.  
  15. int ExistsLeftChild(node n, bTree tTree)
  16. {
  17. if (tTree->data[n].left == -1)
  18. return 0;
  19. else
  20. return 1;
  21. }
  22.  
  23. int ExistsRightChild(node n, bTree tTree)
  24. {
  25. if (tTree->data[n].right == -1)
  26. return 0;
  27. else
  28. return 1;
  29. }
  30.  
  31. void InitB(labeltype tElem, bTree tTree)
  32. {
  33. tTree->data[0].label = tElem;
  34. tTree->data[0].available = 0;
  35. tTree->data[0].left = -1;
  36. tTree->data[0].right = -1;
  37. for (int i = 1; i<10000; i++)
  38. {
  39. tTree->data[i].available = 1;
  40. tTree->data[i].left = -1;
  41. tTree->data[i].right = -1;
  42. }
  43. tTree->current = 1;
  44. }
  45.  
  46. labeltype LabelB(node n, bTree tTree)
  47. {
  48. return tTree->data[n].label;
  49. }
  50.  
  51. node RootB(bTree tTree)
  52. {
  53. if (tTree->data[0].available == 1)
  54. return -1;
  55. else
  56. return 0;
  57. }
  58.  
  59. node LeftChildB(node n, bTree tTree)
  60. {
  61. return tTree->data[n].left;
  62. }
  63.  
  64. node RightChildB(node n, bTree tTree)
  65. {
  66. return tTree->data[n].right;
  67. }
  68.  
  69. void ChangeLabelB(labeltype x, node n, bTree tTree)
  70. {
  71. tTree->data[n].label = x;
  72. }
  73.  
  74. node ParentB(node n, bTree tTree)
  75. {
  76. int pronadjen = 0, i = 0;
  77. if (n == RootB(tTree))
  78. return -1;
  79. else
  80. {
  81. while (pronadjen == 0)
  82. {
  83. if (tTree->data[i].left == n)
  84. {
  85. return i;
  86. pronadjen = 1;
  87. }
  88. else if (tTree->data[i].right == n)
  89. {
  90. return i;
  91. pronadjen = 1;
  92. }
  93. i++;
  94. }
  95. }
  96. }
  97.  
  98. void CreateLeftB(labeltype x, node n, bTree tTree)
  99. {
  100. tTree->current = 1;
  101. while (tTree->data[tTree->current].available == 0)
  102. {
  103. tTree->current++;
  104. }
  105. if (tTree->data[n].available == 1)
  106. {
  107. cout << "Cvor ne postoji te mu ne mozemo dodati djecu.\n" << endl;
  108. }
  109. else
  110. {
  111. tTree->data[n].left = tTree->current;
  112. tTree->data[tTree->current].label = x;
  113. tTree->data[tTree->current].available = 0;
  114. }
  115. }
  116.  
  117. void CreateRightB(labeltype x, node n, bTree tTree)
  118. {
  119. tTree->current = 1;
  120. while (tTree->data[tTree->current].available == 0)
  121. {
  122. tTree->current++;
  123. }
  124. if (tTree->data[n].available == 1)
  125. {
  126. cout << "Cvor ne postoji te mu ne mozemo dodati djecu.\n" << endl;
  127. }
  128. else
  129. {
  130. tTree->data[n].right = tTree->current;
  131. tTree->data[tTree->current].label = x;
  132. tTree->data[tTree->current].available = 0;
  133. }
  134. }
  135.  
  136. void DeleteB(node n, bTree tTree)
  137. {
  138. node pNode;
  139.  
  140. if (ExistsLeftChild(n, tTree) == 1)
  141. DeleteB(LeftChildB(n, tTree), tTree);
  142. if (ExistsRightChild(n, tTree) == 1)
  143. DeleteB(RightChildB(n, tTree), tTree);
  144. pNode = ParentB(n, tTree);
  145.  
  146. if (tTree->data[pNode].left == n)
  147. tTree->data[pNode].left = -1;
  148. else
  149. tTree->data[pNode].right = -1;
  150.  
  151. if ((tTree->data[n].left == -1) && (tTree->data[n].right == -1))
  152. tTree->data[n].available = 1;
  153. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.