Facebook
From Soft Bird, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 264
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct wezel
  5. {
  6.         wezel *up;
  7.         wezel *left;
  8.         wezel *right;
  9.         int dane;
  10. };
  11. wezel *root = NULL;
  12.  
  13. void add(int temp)
  14. {
  15. wezel *nowy;
  16. nowy = new wezel;
  17. nowy->dane=temp;
  18. nowy->up=NULL;
  19. nowy->left=NULL;
  20. nowy->right=NULL;
  21. if(root==NULL){
  22.         root=nowy;
  23. }
  24.  
  25. else{
  26.         wezel *x=root;
  27.         wezel *p=x;    
  28.        
  29.         while(x!=NULL){
  30.                 p=x;
  31.                 if(nowy->dane<x->dane)
  32.                 x=x->left;
  33.                 else
  34.                 x=x->right;
  35.         }
  36.         if(nowy->dane<p->dane)
  37.         p->left=nowy;
  38.         else
  39.         p->right=nowy; 
  40.        
  41.         nowy->up=p;
  42. }
  43. }
  44. void order(wezel* p){
  45. if(p!=NULL)
  46. {
  47.         order(p->left);
  48.         cout<<p->dane<<" ";
  49.         order(p->right);
  50. }
  51. }
  52.  
  53.  
  54. wezel *szukaj(int s, wezel *xs){
  55. if(xs==NULL || xs->dane==s)
  56.         return xs;
  57. else
  58. if(s<xs->dane)
  59.         return szukaj(s,xs->left);
  60. else
  61.         return szukaj(s,xs->right);    
  62. }
  63.  
  64. wezel *usun(int u,wezel *xu)
  65. {
  66. wezel *temp=szukaj(u,root);
  67.  
  68. if(temp->left==NULL && temp->right==NULL){
  69.         if(temp->up->right==temp)
  70.                 temp->up->right=NULL;
  71.         else
  72.                 temp->up->left=NULL;
  73.         delete temp;
  74. }
  75.  
  76. }
  77.  
  78.  
  79. int main(int argc, char* argv){
  80. add(5);
  81. add(3);
  82. add(8);
  83. add(1);
  84. add(4);
  85. add(6);
  86. add(10);
  87. add(7);
  88. add(12);
  89. order(root);
  90. cout<<"\n\n";
  91. cout<<szukaj(12,root);
  92. cout<<"\n\n";
  93. usun(12,root);
  94. order(root);
  95. return 0;
  96.  
  97. }