Posted By


atatlikon on 01/12/14

Tagged


Statistics


Viewed 56 times
Favorited by 0 user(s)

bin_stablo_pokazivac


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

SP 4 ZADATAK


Copy this code and paste it in your HTML
  1. using namespace std;
  2. struct btree {
  3. char oznaka;
  4. btree *lijevo, *desno;
  5. };
  6. typedef struct btree *element;
  7.  
  8. element postoji(char oznaka, btree *B){
  9. btree *PL=B;
  10. btree *PD=B;
  11. if(oznaka==B->oznaka) return B;
  12. else{
  13. if(B->lijevo) PL=postoji(oznaka, B->lijevo);
  14. if(B->desno) PD=postoji(oznaka, B->desno);
  15. }
  16. if(PL)
  17. if(oznaka==PL->oznaka) return PL;
  18. if(PD)
  19. if(oznaka==PD->oznaka) return PD;
  20. else return NULL;
  21. }
  22. btree *LeftChildB(btree *n, btree *B){
  23. return n->lijevo;
  24. }
  25.  
  26. btree *RightChildB(btree *n, btree *B){
  27. return n->desno;
  28. }
  29. void CreateLeftB(char x, btree *n, btree *B){
  30. if (n->lijevo) cout << "Taj cvor vec ima lijevo dijete!" << endl;
  31. else{
  32. n->lijevo = new btree;
  33. n->lijevo->oznaka=x;
  34. n->lijevo->lijevo=NULL;
  35. n->lijevo->desno=NULL;
  36. }
  37. }
  38. void ChangeLabelB(char x, btree *n, btree *B){
  39. n->oznaka=x;
  40. }
  41. void CreateRightB(char x, btree *n, btree *B){
  42. if (n->desno) cout << "Taj cvor vec ima desno dijete!" << endl;
  43. else{
  44. n->desno = new btree;
  45. n->desno->oznaka=x;
  46. n->desno->lijevo=NULL;
  47. n->desno->desno=NULL;
  48. }
  49. }
  50. char LabelB(btree *n, btree *B){
  51. return n->oznaka;
  52. }
  53. btree *ParentB(btree *n, btree *B){
  54. static btree *cvor;
  55. static bool nadjen=false;
  56. if (B->lijevo==n || B->desno==n){
  57. nadjen=true;
  58. cvor=B;
  59. }
  60. if (nadjen) return cvor;
  61. if (B->lijevo) ParentB(n,B->lijevo);
  62. if (B->desno) ParentB(n,B->desno);
  63. }
  64. btree *RootB(btree *B){
  65. return B;
  66. }
  67. void DeleteB(btree *n, btree *B){
  68. btree *roditelj = ParentB(n,B);
  69. if (roditelj->lijevo==n) roditelj->lijevo=NULL;
  70. else roditelj->desno=NULL;
  71.  
  72. if (n->lijevo) DeleteB(n->lijevo,B);
  73. if (n->desno) DeleteB(n->desno,B);
  74. delete n;
  75.  
  76. }
  77. btree *InitB(int x, btree *B){
  78. B=new btree;
  79. B->lijevo=NULL;
  80. B->desno=NULL;
  81. B->oznaka=x;
  82. return B;
  83. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.