Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct Node {
  7. long long key = 0;
  8. Node* parent = nullptr;
  9. Node* left = nullptr;
  10. Node* right = nullptr;
  11. Node* root;
  12.  
  13. void insert(long long key) {
  14. auto z = new Node;
  15. z->key = key;
  16. Node* ptr = root;
  17.  
  18. while (ptr != nullptr) {
  19. if (key == ptr->key) {
  20. return;
  21. }
  22. if (key > ptr->key) {
  23. if (ptr->right != nullptr) {
  24. ptr = ptr->right;
  25. } else {
  26. z->parent = ptr;
  27. ptr->right = z;
  28. return;
  29. }
  30. } else if (key < ptr->key) {
  31. if (ptr->left != nullptr) {
  32. ptr = ptr->left;
  33. } else {
  34. z->parent = ptr;
  35. ptr->left = z;
  36. return;
  37. }
  38. }
  39. }
  40.  
  41. root = z;
  42. }
  43. };
  44.  
  45.  
  46. int main() {
  47. ifstream fin("set.in");
  48. ofstream fout("set.out");
  49. Set myset;
  50. myset.res();
  51. string str;
  52. int k;
  53. bool fl;
  54. while(fin>>str>>k){
  55. if (str == "insert") {
  56. myset.insert(k);
  57. }
  58. else if (str == "exists") {
  59. int hash_numb = abs(k) % 1000001;
  60. fl = find(myset.numb[hash_numb].begin(), myset.numb[hash_numb].end(), k) != myset.numb[hash_numb].end();
  61. if (fl) {
  62. fout << "true\n";
  63. }
  64. else {
  65. fout << "false\n";
  66. }
  67. }
  68. else if (str == "delete") {
  69. myset.deleete(k);
  70. }
  71. }
  72. cin.close();
  73. cout.close();
  74. return 0;
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement