Posted By


igotepava on 01/18/15

Tagged


Statistics


Viewed 891 times
Favorited by 0 user(s)

Related snippets


binarno_stablo_polje.h


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

Implementacija binarnog stabla pomoću polja.


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.