Posted By


AOT_code on 01/12/14

Tagged


Statistics


Viewed 719 times
Favorited by 0 user(s)

binarno_stabo_polje.h


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

Implementacija binarnog stabla koristeći se poljem.


Copy this code and paste it in your HTML
  1. struct arr_element{
  2. int label, used;
  3. };
  4.  
  5. struct arr_tree{
  6. arr_element array[10000];
  7. };
  8.  
  9. arr_tree* InitB(int x, arr_tree* T){
  10. T = new arr_tree;
  11. for(int i=0; i<10000; i++) T->array[i].used = 0;
  12. T->array[1].label = x;
  13. T->array[1].used = 1;
  14. return T;
  15. }
  16.  
  17. int RootB(arr_tree* T){
  18. return T->array[1].label;
  19. }
  20.  
  21. int LabelB(int n, arr_tree* T){
  22. return T->array[n].label;
  23. }
  24.  
  25. int ChangeLabelB(int x, int n, arr_tree* T){
  26. return T->array[n].label = x;
  27. }
  28.  
  29. int ParentB(int n,arr_tree *T){
  30. if(T->array[1].label==n) return -1;
  31. if(n%2) return n/2+1;
  32. else return n/2;
  33. }
  34.  
  35. int LeftChildB(int n, arr_tree* T){
  36. if(!(T->array[2*n].used)) return -1;
  37. else return 2*n;
  38. }
  39.  
  40. int RightChildB(int n, arr_tree* T){
  41. if(!(T->array[2*n+1].used)) return -1;
  42. else return 2*n+1;
  43. }
  44.  
  45. void CreateLeftB(int x, int n, arr_tree* T){
  46. if(T->array[2*n].used) cout << "Pozicija je zauzeta!" << endl;
  47. else{
  48. T->array[2*n].label = x;
  49. T->array[2*n].used = 1;
  50. }
  51. }
  52.  
  53. void CreateRightB(int x, int n, arr_tree* T){
  54. if(T->array[2*n+1].used) cout << "Pozicija je zauzeta!" << endl;
  55. else{
  56. T->array[2*n+1].label = x;
  57. T->array[2*n+1].used = 1;
  58. }
  59. }
  60.  
  61. bool DeleteB(int n, arr_tree* T){
  62. if(LeftChildB(n,T) != -1) DeleteB(LeftChildB(n,T),T);
  63. if(RightChildB(n,T) != -1) DeleteB(RightChildB(n,T),T);
  64. T->array[n].used = 0;
  65. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.