Implementacija binarnog stabla pomocu polja


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



Copy this code and paste it in your HTML
  1. #include<iostream>
  2. using namespace std;
  3. struct elem {
  4. int label;
  5. int used;
  6. };
  7.  
  8. struct bintree {
  9. elem elements[1000];
  10. };
  11.  
  12. typedef struct bintree *bs;
  13. typedef int node;
  14.  
  15. node ParentB(node c,bintree *bs) {
  16.  
  17. if (c=1) return(-1);
  18. else
  19. return((int)c/2);
  20. }
  21. node LeftChildB(node c,bintree *bs) {
  22. if (bs->elements[2*c].used)
  23. return(2*c);
  24. else {
  25. return(-1);
  26. }
  27. }
  28. node RightChildB(node c,bintree *bs) {
  29. if (bs->elements[2*c+1].used)
  30. return(2*c+1);
  31. else {
  32. return(-1);
  33. }
  34. }
  35. node RootB(bintree *bs) {
  36. return(1);
  37. }
  38. void DeleteB(node c,bintree *bs) {
  39. if(bs->elements[(c*2)+1].used==true){
  40. DeleteB((c*2)+1, bs);
  41. }
  42. if(bs->elements[(c*2)+2].used==true){
  43. DeleteB((c*2)+2, bs);
  44. }
  45.  
  46. bs->elements[c].used=false;
  47. }
  48.  
  49. typedef int labeltype;
  50. labeltype LabelB(node c,bintree *bs) {
  51. return(bs->elements[c].label);
  52. }
  53. void ChangeLabelB(labeltype x, node c,bintree *bs) {
  54. if (bs->elements[c].used)
  55. bs->elements[c].label = x;
  56. else {
  57. cout<<"Trazeni cvor u stablu nema vrijednost."<<endl;
  58. }
  59. }
  60. void CreateLeftB(labeltype x,node c,bintree *bs) {
  61. if (bs->elements[2*c].used) {
  62. cout<<"Cvor ima lijevo dijete"<<endl;
  63. }
  64. else {
  65. bs->elements[2*c].used = -1;
  66. bs->elements[2*c].label = x;
  67. }
  68. }
  69. void CreateRightB(labeltype x,node c,bintree *bs) {
  70. if (bs->elements[2*c+1].used) {
  71. cout<<"Cvor ima desno dijete"<<endl;
  72. }
  73. else {
  74. bs->elements[2*c+1].used = -1;
  75. bs->elements[2*c+1].label = x;
  76. }
  77. }
  78. void InitB(node n,bintree *bs) {
  79. int i;
  80. for (i=0;i<=999;i++)
  81. bs->elements[i].label=-1;
  82. bs->elements[1].label=n;
  83. bs->elements[1].used=-1;
  84. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.