Binarno stablo polje


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

polje


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.