binarno stablo pomocu pokazivaca.h


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

binarno


Copy this code and paste it in your HTML
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct pnode{
  5. int label;
  6. pnode *left,*right;
  7. };
  8.  
  9. pnode *InitB(int n,pnode *T){
  10. T = new pnode;
  11. T->left = T->right = NULL;
  12. T->label = n;
  13. return T;
  14. }
  15.  
  16. pnode *RootB(pnode *T){
  17. return T;
  18. }
  19.  
  20. void ChangeLabelB(int x,pnode *T){
  21. T->label = x;
  22. }
  23.  
  24. pnode *ParentB(int n,pnode *T){
  25. if(T->label==n) return NULL;
  26. if(T->left){
  27. if(T->left->label==n) return T;
  28. ParentB(n,T->left);
  29. }
  30. if(T->right){
  31. if(T->right->label==n) return T;
  32. ParentB(n,T->right);
  33. }
  34. }
  35.  
  36. pnode *LeftChildB(pnode *T){
  37. return T->left;
  38. }
  39.  
  40. pnode *RightChildB(pnode *T){
  41. return T->right;
  42. }
  43.  
  44. int LabelB(pnode *T){
  45. return T->label;
  46. }
  47.  
  48. void CreateLeftB(int x,pnode *T){
  49. if(T->left) cout << "Lijevo dijete već postoji!" << endl;
  50. else{
  51. pnode *novi = new pnode;
  52. novi->label = x;
  53. novi->left = NULL;
  54. novi->right = NULL;
  55. T->left = novi;
  56. }
  57. }
  58.  
  59. void CreateRightB(int x,pnode *T){
  60. if(T->right) cout << "Desno dijete već postoji!" << endl;
  61. else{
  62. pnode *novi = new pnode;
  63. novi->label = x;
  64. novi->left = NULL;
  65. novi->right = NULL;
  66. T->right = novi;
  67. }
  68. }
  69.  
  70. void DeleteB(pnode *P,pnode *T){
  71. pnode *erase = P;
  72. if(P!=T){
  73. pnode *roditelj = ParentB(P->label,T);
  74. if(roditelj->left==P) roditelj->left = NULL;
  75. else roditelj->right = NULL;
  76. }
  77. if(P->left) DeleteB(P->left,T);
  78. if(P->right) DeleteB(P->right,T);
  79. delete erase;
  80. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.