binarno stablo-pokazivaci


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

binarno stablo-pokazivaci


Copy this code and paste it in your HTML
  1. struct belement {
  2. int v;
  3. belement *lijevo;
  4. belement *desno;
  5. };
  6.  
  7. typedef belement* binStablo;
  8. typedef belement* binNode;
  9.  
  10. binStablo InitB(int x, binStablo t) {
  11. t = new belement;
  12. t->v = x;
  13. t->lijevo = 0;
  14. t->desno = 0;
  15.  
  16. return t;
  17. }
  18.  
  19. binNode RootB(binStablo t) {
  20. return t;
  21. }
  22.  
  23. binNode ParentB(binNode n, binStablo t) {
  24. binNode r = 0;
  25.  
  26. if(n==RootB(t))return 0;
  27. if(t->lijevo==n || t->desno==n)
  28. return t;
  29.  
  30. if(t->lijevo)
  31. r=ParentB(n, t->lijevo);
  32. if(r)return r;
  33. if(t->desno)
  34. r=ParentB(n, t->desno);
  35. if(r)return r;
  36. }
  37.  
  38. binNode LeftChildB(binNode n, binStablo t) { return n->lijevo;}
  39. binNode RightChildB(binNode n, binStablo t) { return n->desno;}
  40.  
  41. int LabelB(binNode n, binStablo t) {
  42. return n->v;
  43. }
  44. void ChangeLabelB(int x, binNode n, binStablo t) {
  45. if(n!=0)
  46. n->v = x;
  47. }
  48.  
  49. void CreateLeftB(int x, binNode n, binStablo t) {
  50. binNode nel = new belement;
  51.  
  52. nel->v = x; nel->lijevo = 0; nel->desno = 0;
  53. n->lijevo = nel;
  54. }
  55.  
  56. void CreateRightB(int x, binNode n, binStablo t) {
  57. binNode nel = new belement;
  58.  
  59. nel->v = x; nel->lijevo = 0; nel->desno = 0;
  60. n->desno = nel;
  61. }
  62.  
  63. void DeleteB(binNode n, binStablo t) {
  64. if(t!=0) {
  65. if(ParentB(n, t)==0) return;
  66.  
  67. binNode r = ParentB(n, t);
  68. if(r->lijevo==n)r->lijevo=0;
  69. if(r->desno==n)r->desno=0;
  70.  
  71. DeleteB(n, 0);
  72. }
  73. else {
  74. if(n->lijevo!=0)DeleteB(n->lijevo, 0);
  75. if(n->desno!=0)DeleteB(n->desno, 0);
  76.  
  77. delete n;
  78. }
  79. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.