Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct Node
- {
- string tel; //ключ
- string name;
- int step;
- Node* left;
- Node* right;
- };
- void print(Node* root)
- {
- if (root != NULL)
- {
- cout << root->step << ": " << root->tel << endl;
- //cout << "Телефон: " << root->tel << "; Имя: " << root->name << endl;
- //cout << "влево ";
- print(root->left);
- //cout << "вправо ";
- print(root->right);
- }
- else {
- //cout << " конец ветки " << endl;
- }
- }
- Node* add(string tel, string name, Node* root, int step = 0)
- {
- if (root == NULL)
- {
- root = new Node;
- root->tel = tel;
- root->name = name;
- root->left = NULL;
- root->right = NULL;
- root->step = step;
- }
- else if (tel < root->tel)
- {
- //cout << "<- ";
- step += 1;
- root->left = add(tel, name, root->left, step);
- }
- else
- {
- step += 1;
- root->right = add(tel, name, root->right, step);
- }
- return root;
- }
- int main()
- {
- setlocale(0, "rus");
- Node* tree = NULL;
- tree = add("10000000001", "Вася", tree);
- tree = add("11000000000", "Вася", tree);
- tree = add("10000000010", "Вася", tree);
- tree = add("10000000100", "Вася", tree);
- tree = add("10000000000", "Вася", tree);
- tree = add("11100010000", "Вася", tree);
- tree = add("10000010000", "Вася", tree);
- tree = add("1000000000", "Вася", tree);
- print(tree);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment