Revision: 38989
Updated Code
at January 11, 2011 19:40 by tjakopec
Updated Code
struct element{ int label; int koristeno; }; struct bt{ element elements[10000]; }; typedef int cvor; bt* InitB(int x, bt* T){ if(T) delete T; T = new bt; for(int i = 0; i < 10000; i++) T->elements[i].koristeno = 0; T->elements[1].label = x; T->elements[1].koristeno = 1; return T; }//initB void CreateLeftB(int x, cvor n, bt* T){ if(T->elements[n].koristeno == 0 || T->elements[2*n].koristeno == 1) { cout << "Greska" << endl << endl; return; } T->elements[2*n].koristeno = 1; T->elements[2*n].label = x; }//CreateLeftB void CreateRightB(int x, cvor n, bt* T){ if(T->elements[n].koristeno == 0 || T->elements[2*n+1].koristeno == 1){ cout << "Greska" << endl << endl; return; } T->elements[2*n+1].koristeno = 1; T->elements[2*n+1].label = x; }//CreateRightB cvor LeftChildB(cvor n, bt* T){ if(T->elements[n].koristeno == 0) return 0; if(T->elements[2*n].koristeno == 1) return 2*n; else return 0; }//LeftCildB cvor RightChildB(cvor n, bt* T){ if(T->elements[n].koristeno == 0) return 0; if(T->elements[2*n+1].koristeno == 1) return 2*n+1; else return 0; }//RightCildB cvor ParentB(cvor n, bt* T){ if(n == 1) return 0; if(n%2) n--; return n/2; }//ParentB int LabelB(cvor n, bt* T){ return T->elements[n].label; }//LabelB void ChangeLabelB(int x, cvor n, bt* T){ if(T->elements[n].koristeno == 0) return; T->elements[n].label = x; }//ChangeLabelB cvor RootB(bt* T){ if(T->elements[1].koristeno == 0) return 0; return 1; }//RootB void DeleteB(cvor n, bt* T){ T->elements[n].koristeno = 0; if (LeftChildB(n, T)) DeleteB(LeftChildB(n, T), T); if (RightChildB(n, T)) DeleteB(RightChildB(n, T), T); }//DeleteB
Revision: 38988
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 11, 2011 18:56 by tjakopec
Initial Code
1. struct element{ 2. int label; 3. int koristeno; 4. }; 5. struct bt{ 6. element elements[10000]; 7. }; 8. typedef int cvor; 9. 10. bt* InitB(int x, bt* T){ 11. if(T) delete T; 12. T = new bt; 13. for(int i = 0; i < 10000; i++) 14. T->elements[i].koristeno = 0; 15. T->elements[1].label = x; 16. T->elements[1].koristeno = 1; 17. return T; 18. }//initB 19. 20. void CreateLeftB(int x, cvor n, bt* T){ 21. if(T->elements[n].koristeno == 0 || T->elements[2*n].koristeno == 1) { 22. cout << "Greska" << endl << endl; 23. return; 24. } 25. T->elements[2*n].koristeno = 1; 26. T->elements[2*n].label = x; 27. }//CreateLeftB 28. 29. void CreateRightB(int x, cvor n, bt* T){ 30. if(T->elements[n].koristeno == 0 || T->elements[2*n+1].koristeno == 1){ 31. cout << "Greska" << endl << endl; 32. return; 33. } 34. T->elements[2*n+1].koristeno = 1; 35. T->elements[2*n+1].label = x; 36. }//CreateRightB 37. 38. cvor LeftChildB(cvor n, bt* T){ 39. if(T->elements[n].koristeno == 0) 40. return 0; 41. if(T->elements[2*n].koristeno == 1) 42. return 2*n; 43. else return 0; 44. }//LeftCildB 45. 46. cvor RightChildB(cvor n, bt* T){ 47. if(T->elements[n].koristeno == 0) return 0; 48. if(T->elements[2*n+1].koristeno == 1) return 2*n+1; 49. else return 0; 50. }//RightCildB 51. 52. cvor ParentB(cvor n, bt* T){ 53. if(n == 1) return 0; 54. if(n%2) n--; 55. return n/2; 56. }//ParentB 57. 58. int LabelB(cvor n, bt* T){ 59. return T->elements[n].label; 60. }//LabelB 61. 62. void ChangeLabelB(int x, cvor n, bt* T){ 63. if(T->elements[n].koristeno == 0) return; 64. T->elements[n].label = x; 65. }//ChangeLabelB 66. 67. cvor RootB(bt* T){ 68. if(T->elements[1].koristeno == 0) return 0; 69. return 1; 70. }//RootB 71. 72. void DeleteB(cvor n, bt* T){ 73. T->elements[n].koristeno = 0; 74. if (LeftChildB(n, T)) DeleteB(LeftChildB(n, T), T); 75. if (RightChildB(n, T)) DeleteB(RightChildB(n, T), T); 76. }//DeleteB
Initial URL
Initial Description
Initial Title
bstablo_polje.h
Initial Tags
Initial Language
C++