Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. /*Дерево поиска
  2. 1 создать
  3. 2 уничтожить
  4. 3 пусто ли
  5. 4 очистить
  6. 5 добавить эл
  7. 6 уничтожить эл
  8. 7 найти эл
  9. 8.9 найти мин.макс
  10. 10 найти след эл.предыдущий
  11. 11 обьединение деревьев с/без разбора эл
  12. 12 разбиение дерева
  13. */
  14.  
  15. #include <iostream>
  16. using namespace std;
  17. template<class T>class CTree
  18. {
  19. public:
  20. class CNode
  21. {
  22. public:
  23. T v; CNode *left, *right, *par;
  24. CNode(const T&v,CNode *par){this->v=v; this->par=par; left=right=NULL;}
  25. CNode *FindMin(){CNode *n;if(this==NULL)return NULL;for(n=this;n->left; n=n->left);return n;}
  26. CNode ){CNode *n;if(this==NULL return NULL;for(n=this;n->right; n=n->right);return n;}
  27. CNode ){if(this==NULLreturn NULL;if(right) return right->FindMin();CNode *n;
  28. for(n=this;n->par;n=n->par)if(n==n->par->left) return n->par;return NULL;}
  29. CNode *Add(const T&x){if(this==NULL||v==x)return NULL;if(x>v){if(right==NULL) return right=new CNode(x,this);return right->Add(x);}
  30. {if(left==NULL)return left=new CNode(x,this);return left->Add(x);}
  31. }
  32. } *root;
  33. CTree(){root=NULL;}
  34. int IsEmpty(){return root==NULL;}
  35. CNode *FindMin(){return root->FindMin();}
  36. CNode ){return root->FindMax(;}
  37. CNode CNode*n{return n->Next();}
  38. CNode *Add(const T&x){if(root==NULL)return root=new CNode(x,NULL); return root->Add(x);}
  39. };
  40.  
  41. int main(void)
  42. {
  43. CTree<int> t; int i,x; CTree<int>::CNode *n;
  44. for(i=0,x=7;i<13;i++,x+=7)t.Add(x%13);
  45. for(n=t.FindMin();n;n=t.Next(n))cout«n->v«" "; cout«endl;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement