binarno stablo pomocu polja.h


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

zadatak4


Copy this code and paste it in your HTML
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct anode{
  5. int label;
  6. bool used;
  7. };
  8.  
  9. struct atree{
  10. anode polje[1000];
  11. };
  12.  
  13. atree *aInitB(int x,atree *T){
  14. T = new atree;
  15. for(int i=0;i<1000;i++){
  16. T->polje[i].used = false;
  17. T->polje[i].label = -1;
  18. }
  19. T->polje[1].label = x;
  20. T->polje[1].used = true;
  21. return T;
  22. }
  23.  
  24. int aLabelB(int n,atree *T){
  25. return T->polje[n].label;
  26. }
  27.  
  28. void aChangeLabelB(int x,int n,atree *T){
  29. T->polje[n].label = x;
  30. }
  31.  
  32. int aRootB(atree *T){
  33. return T->polje[1].label;
  34. }
  35.  
  36. int aLeftChildB(int n,atree *T){
  37. return 2*n;
  38. }
  39.  
  40. int aRightChildB(int n,atree *T){
  41. return 2*n+1;
  42. }
  43.  
  44. int aParentB(int n,atree *T){
  45. if(T->polje[1].label==n) return -1;
  46. if(n%2) return n/2+1;
  47. else return n/2;
  48. }
  49.  
  50. void aCreateLeftB(int x,int n,atree *T){
  51. if(T->polje[2*n].used) cout << "Lijevo dijete ve����¯�¿�½ postoji!" << endl;
  52. else{
  53. T->polje[2*n].label = x;
  54. T->polje[2*n].used = true;
  55. }
  56. }
  57.  
  58. void aCreateRightB(int x,int n,atree *T){
  59. if(T->polje[2*n+1].used) cout << "Desno dijete ve����¯�¿�½ postoji!" << endl;
  60. else{
  61. T->polje[2*n+1].label = x;
  62. T->polje[2*n+1].used = true;
  63. }
  64. }
  65.  
  66. void aDeleteB(int n,atree *T){
  67. if(T->polje[aLeftChildB(n,T)].used) aDeleteB(2*n,T);
  68. if(T->polje[aRightChildB(n,T)].used) aDeleteB(2*n-1,T);
  69. T->polje[n].label = -1;
  70. T->polje[n].used = false;
  71. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.