Posted By


vale on 01/17/15

Tagged


Statistics


Viewed 39 times
Favorited by 1 user(s)

binarno_pokazivac


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

Implementacija binarnog stabla pomocu pokazivaca


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.