Posted By

8ivana8 on 01/18/15


Tagged

Strukture podataka polje implementacija binarno stablo


Versions (?)

binarno stablo_polje


 / Published in: C++
 

Implementacija binarnog stabla pomoću polja za kolegij Strukture podataka.

  1. struct array_element{
  2. int label, used;
  3. };
  4.  
  5. struct array_tree{
  6. array_element array[10000];
  7. };
  8.  
  9. array_tree* array_InitB(int x, array_tree* T){
  10. T = new array_tree;
  11. for(int i=0; i<10000; i++){
  12. T->array[i].used = 0;
  13. T->array[i].label = -1;
  14. }
  15. T->array[1].label = x;
  16. T->array[1].used = 1;
  17. return T;
  18. }
  19.  
  20. int array_RootB(array_tree* T){
  21. return T->array[1].label;
  22. }
  23.  
  24. int array_LabelB(int n, array_tree* T){
  25. return T->array[n].label;
  26. }
  27.  
  28. int array_ChangeLabelB(int x, int n, array_tree* T){
  29. return T->array[n].label = x;
  30. }
  31.  
  32. int array_ParentB(int n, array_tree *T){
  33. if(T->array[1].label==n) return -1;
  34. if(n%2) return n/2+1;
  35. else return n/2;
  36. }
  37.  
  38. int array_LeftChildB(int n, array_tree* T){
  39. if(!(T->array[2*n].used)) return -1;
  40. else return 2*n;
  41. }
  42.  
  43. int array_RightChildB(int n, array_tree* T){
  44. if(!(T->array[2*n+1].used)) return -1;
  45. else return 2*n+1;
  46. }
  47.  
  48. void array_CreateLeftB(int x, int n, array_tree* T){
  49. if(T->array[2*n].used) cout << "Pozicija je zauzeta!" << endl;
  50. else{
  51. T->array[2*n].label = x;
  52. T->array[2*n].used = 1;
  53. }
  54. }
  55.  
  56. void array_CreateRightB(int x, int n, array_tree* T){
  57. if(T->array[2*n+1].used) cout << "Pozicija je zauzeta!" << endl;
  58. else{
  59. T->array[2*n+1].label = x;
  60. T->array[2*n+1].used = 1;
  61. }
  62. }
  63.  
  64. void array_DeleteB(int n, array_tree* T){
  65. if(array_LeftChildB(n,T) != -1) array_DeleteB(array_LeftChildB(n,T),T);
  66. if(array_RightChildB(n,T) != -1) array_DeleteB(array_RightChildB(n,T),T);
  67. T->array[n].label = -1;
  68. T->array[n].used = 0;
  69. }

Report this snippet  

You need to login to post a comment.