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;
- };
- Tree* 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 root;
- }
- if (aName > root->name) {
- root->right = Add(aName, aNumber, root->right);
- }
- else {
- root->left = Add(aName, aNumber, root->left);
- }
- return root;
- }
- 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* SearchMin(Tree* root) {
- if (root->left) {
- return SearchMin(root->left);
- }
- return root;
- }
- Tree* Delete(string aName, Tree* root) {
- if (root == NULL) {
- cout<<"This name isn`t in list"<<endl;
- return root;
- }
- if (root->name == aName) {
- if (root->left && root->right) {
- Tree* min = SearchMin(root->right);
- root->name = min->name;
- root->number = min->number;
- root->right = Delete(min->name, root->right);
- return root;
- }
- if (root->left) {
- root->name = root->left->name;
- root->number = root->left->number;
- root->left = Delete(root->left->name, root->left);
- return root;
- }
- if (root->right) {
- root->name = root->right->name;
- root->number = root->right->number;
- root->right = Delete(root->right->name, root->right);
- return root;
- }
- root = NULL;
- return root;
- }
- if (root->name > aName) {
- root->right = Delete(aName, root->right);
- }
- else {
- root->left = Delete(aName, root->left);
- }
- return root;
- }
- int main() {
- Tree* a = NULL;
- a = Add("Nik", "123456", a);
- a = Add("Onf", "234567", a);
- a = Add("Dih", "4637468274", a);
- cout << Search("Nik", a) << endl;
- cout << Search("Onf", a) << endl;
- cout << Search("Dih", a) << endl;
- a = Delete("Nik", a);
- if (!a) cout << "null" << endl;
- cout << Search("Nik", a) << endl;
- cout << Search("Onf", a) << endl;
- cout << Search("Dih", a) << endl;
- a = Delete("Onf", a);
- cout << Search("Nik", a) << endl;
- cout << Search("Onf", a) << endl;
- cout << Search("Dih", a) << endl;
- a = Delete("Dih", a);
- cout << Search("Nik", a) << endl;
- cout << Search("Onf", a) << endl;
- cout << Search("Dih", a) << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment