SpaceCreator

phone_catalog

Apr 30th, 2019
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.15 KB | None | 0 0
  1. #include <utility>
  2. #include <iostream>
  3. #include <c++/4.8.3/queue>
  4.  
  5. using namespace std;
  6.  
  7. struct Node {
  8.     string name;
  9.     string phone;
  10.     Node *left_son;
  11.     Node *right_son;
  12.  
  13.     explicit Node(string n, string p = "Permisson denied") : name(move(n)), phone(std::move(p)), left_son(nullptr),
  14.                                                              right_son(nullptr) {}
  15. };
  16.  
  17. class SearchTree {
  18.     Node *root;
  19.  
  20.     Node *create_node(const string &name = "", const string &phone = "");
  21.  
  22.     Node *search(const string &name, bool last_prev = false);
  23.  
  24.     void cp(Node *original, Node *copy);
  25.  
  26.     void turn_left(Node *head);
  27.  
  28.     void turn_right(Node *head);
  29.  
  30.     void in_order_print(Node *current_node);
  31.  
  32.     void post_order_del(Node *current_node);
  33.  
  34.     bool is_leaf(Node *curr_node);
  35.  
  36. public:
  37.  
  38.     SearchTree() : root(nullptr) {}
  39.  
  40.     ~SearchTree(){
  41.         if(root){
  42.             post_order_del(root);
  43.         }
  44.     }
  45.  
  46.     void add_contact(const string &name, const string &phone = "");
  47.  
  48.     void remove_contact(const string &name);
  49.  
  50.     string search_phone(const string &name);
  51.  
  52.     void print_list_of_contacts();
  53.  
  54.     void by_level_print();
  55.  
  56.  
  57. };
  58.  
  59. int main() {
  60.     SearchTree my_tree;
  61.     string command;
  62.     string name;
  63.     string phone;
  64.     string temp;
  65.     bool shortcut = false;
  66.     cout << "add <name> <phone(s)> - to add contact in directory\n" <<
  67.          "search <name> - to find phone(s) by name\n" <<
  68.          "erase <name> - to remove contact from directory\n" <<
  69.          "print - to print list of contacts here\n" <<
  70.          "q - to quit\n";
  71.     while (true) {
  72.         if (! shortcut) {
  73.             cin >> command;
  74.         }
  75.         if (command == "add") {
  76.             cin >> name >> phone;
  77.             temp = my_tree.search_phone(name);
  78.             if (temp != "404") {
  79.                 cout
  80.                         << "You already have a contact with that name. Want to add another phone number to this contact? (y/n)"
  81.                         << endl;
  82.                 cin >> command;
  83.                 while ((command != "y") && (command != "n")) {
  84.                     cout << "Incorrect answer. Try again. y or n?" << endl;
  85.                     cin >> command;
  86.                 }
  87.                 if (command == "n") {
  88.                     cout << "In this case, please choose another name." << endl;
  89.                     shortcut = true;
  90.                     command = "add";
  91.                     continue;
  92.                 } else {
  93.                     cout << "OK" << endl;
  94.                 }
  95.             }
  96.             my_tree.add_contact(name, phone);
  97.             shortcut = false;
  98.             cout << "Done" << endl << endl;
  99.             my_tree.by_level_print();
  100.         } else if (command == "search") {
  101.             cin >> name;
  102.             cout << my_tree.search_phone(name) << endl;
  103.         } else if (command == "erase") {
  104.             cin >> name;
  105.             my_tree.remove_contact(name);
  106.             cout << "Done!" << endl << endl;
  107.             my_tree.by_level_print();
  108.         } else if (command == "print") {
  109.             cout << "<name> - <phone number(s)>" << endl;
  110.             my_tree.print_list_of_contacts();
  111.         } else if (command == "q") {
  112.             break;
  113.         } else {
  114.             cout << "Incorrect command, try again. I believe u can~" << endl;
  115.         }
  116.     }
  117.     cout << "Thx 4 using our software :)" << endl;
  118.     return 0;
  119. }
  120.  
  121. Node *SearchTree::create_node(const string &name, const string &phone) {
  122.     return new Node(name, phone);
  123. }
  124.  
  125. Node *SearchTree::search(const string &name, bool last_prev) {
  126.     static Node *last_node_prev = nullptr;
  127.     if (last_prev) {
  128.         return last_node_prev;
  129.     }
  130.     Node *curr_node = root;
  131.     while ((curr_node) && (curr_node->name != name)) {
  132.         last_node_prev = curr_node;
  133.         curr_node = name > curr_node->name ? curr_node->right_son : curr_node->left_son;
  134.     }
  135.     return curr_node;
  136. }
  137.  
  138. void SearchTree::cp(Node *original, Node *copy) {
  139.     copy->name = original->name;
  140.     copy->phone = original->phone;
  141.     copy->left_son = original->left_son;
  142.     copy->right_son = original->right_son;
  143. }
  144.  
  145. void SearchTree::turn_left(Node *head) {
  146.     cout << "turn left" << endl;
  147.     Node *temp = head->right_son;
  148.     head->right_son = temp->left_son;
  149.     Node *new_head = create_node();
  150.     cp(head, new_head);
  151.     cp(temp, head);
  152.     head->left_son = new_head;
  153.     delete temp;
  154. }
  155.  
  156. void SearchTree::turn_right(Node *head) {
  157.     cout << "turn right" << endl;
  158.     Node *temp = head->left_son;
  159.     head->left_son = temp->right_son;
  160.     Node *new_head = create_node();
  161.     cp(head, new_head);
  162.     cp(temp, head);
  163.     head->right_son = new_head;
  164.     delete temp;
  165. }
  166.  
  167. bool SearchTree::is_leaf(Node *curr_node) {
  168.     return (! curr_node->left_son) && (! curr_node->right_son);
  169. }
  170.  
  171. void SearchTree::add_contact(const string &name, const string &phone) {
  172.     Node *new_node = search(name);
  173.     if (new_node) {
  174.         new_node->phone += ", " + phone;
  175.         return;
  176.     }
  177.     new_node = create_node(name, phone);
  178.     if (root == nullptr) {
  179.         root = new_node;
  180.         return;
  181.     }
  182.     Node *curr_node = root;
  183.     Node *prev_node = curr_node;
  184.     while (curr_node) {
  185.         prev_node = curr_node;
  186.         curr_node = new_node->name > prev_node->name ? prev_node->right_son : prev_node->left_son;
  187.     }
  188.     (prev_node->name < new_node->name ? prev_node->right_son : prev_node->left_son) = new_node;
  189. }
  190.  
  191. void SearchTree::remove_contact(const string &name) {
  192.     Node *useless_node = search(name);
  193.     Node *prev_node = nullptr;
  194.     bool left = true;
  195.  
  196.     if (! useless_node) {
  197.         return;
  198.     }
  199.     if (is_leaf(useless_node)) {
  200.         if (root == useless_node) {
  201.             delete root;
  202.             root = nullptr;
  203.             return;
  204.         }
  205.         prev_node = search(name, true);
  206.         left = prev_node->left_son == useless_node;
  207.     }
  208.     while (! is_leaf(useless_node)) {
  209.         if (useless_node->right_son) {
  210.             turn_left(useless_node);
  211.             prev_node = useless_node;
  212.             useless_node = useless_node->left_son;
  213.             left = true;
  214.         } else {
  215.             prev_node = useless_node;
  216.             turn_right(useless_node);
  217.             useless_node = useless_node->right_son;
  218.             left = false;
  219.         }
  220.     }
  221.     delete useless_node;
  222.     if (prev_node) {
  223.         (left ? prev_node->left_son : prev_node->right_son) = nullptr;
  224.     }
  225.  
  226. }
  227.  
  228. string SearchTree::search_phone(const string &name) {
  229.     Node *my_node = search(name);
  230.     if (my_node) {
  231.         return my_node->phone;
  232.     } else {
  233.         return "404";
  234.     }
  235.  
  236. }
  237.  
  238. void SearchTree::in_order_print(Node *current_node) {
  239.     if (current_node->left_son) {
  240.         in_order_print(current_node->left_son);
  241.     }
  242.     cout << current_node->name << " - " << current_node->phone << endl;
  243.     if (current_node->right_son) {
  244.         in_order_print(current_node->right_son);
  245.     }
  246. }
  247.  
  248. void SearchTree::print_list_of_contacts() {
  249.     if (root) {
  250.         in_order_print(root);
  251.     }
  252. }
  253.  
  254. void SearchTree::by_level_print() {
  255.     if(root == nullptr){
  256.         return;
  257.     }
  258.     queue<Node *> *current_level = new queue<Node *>;
  259.     queue<Node *> *next_level = new queue<Node *>;
  260.     current_level->push(root);
  261.     Node *curr_node;
  262.     queue<Node *> *temp;
  263.     while (! current_level->empty()) {
  264.         curr_node = current_level->front();
  265.         current_level->pop();
  266.         cout << curr_node->name << " ";
  267.         if (curr_node->left_son) {
  268.             next_level->push(curr_node->left_son);
  269.         }
  270.         if (curr_node->right_son) {
  271.             next_level->push(curr_node->right_son);
  272.         }
  273.         if (current_level->empty()) {
  274.             temp = next_level;
  275.             next_level = current_level;
  276.             current_level = temp;
  277.             cout << endl;
  278.         }
  279.  
  280.     }
  281.     cout << endl << endl;
  282. }
  283.  
  284. void SearchTree::post_order_del(Node *current_node) {
  285.     if (current_node->left_son) {
  286.         post_order_del(current_node->left_son);
  287.     }
  288.     if (current_node->right_son) {
  289.         post_order_del(current_node->right_son);
  290.     }
  291.     delete current_node;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment