Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <time.h>
- using namespace std;
- struct Tree {
- string name;
- string number;
- Tree* right = NULL;
- Tree* left = NULL;
- };
- void Add(string aName, string aNumber, Tree*& root) {// * - a iaei? noai?eii iiea??ee ia ei??iu aa?aaa, & - uia i?ao?aaoe c i?ea?iaeii ? ci?i?aaoe eiai
- if (root == NULL) {
- root = new Tree;
- root->name = aName;
- root->number = aNumber;
- return;
- }
- if (aName > root->name) {
- return Add(aName, aNumber, root->right);
- }
- return Add(aName, aNumber, root->left);
- }
- string Search(string aName, Tree* root) {// iiea??ee ia ei??iu
- if (root == NULL) {
- return "This name isn`t in list";
- }
- if (aName == root->name) {
- return root->number;
- }
- if (aName > root->name) {
- return Search(aName, root->right);
- }
- return Search(aName, root->left);
- }
- Tree* Delete(string aName, Tree*& root) {
- if (root == NULL) {
- cout << "This name isn`t in list" << endl;
- return root;
- }
- if (root->name == aName) {
- Tree* tmp;
- if (root->right == NULL) {
- tmp = root->left;
- }
- else {
- Tree* ptr = root->right;
- if (ptr->left == NULL) {
- ptr->left = root->right;
- tmp = ptr;
- }
- else {
- Tree* pMin = ptr->left;
- while (pMin->left) {
- ptr = pMin;
- pMin = ptr->left;
- }
- ptr->left = pMin->right;
- pMin->left = root->left;
- pMin->right = root->right;
- tmp = pMin;
- }
- }
- delete root;
- return tmp;
- }
- }
- int main() {
- Tree* a = NULL;
- Add("Nik", "123456", a);
- cout << Search("Nik", a) << endl;
- Add("Onf", "234567", a);
- Add("Dih", "4637468274", a);
- cout << Search("Onf", a) << endl;
- cout << Search("Dih", a) << endl;
- Delete("Nik", a);
- Delete("Onf", a);
- cout << Search("Onf", a) << endl;
- cout << Search("Nik", a) << endl;
- cout << Search("Dih", a) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement