Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <string>
- std::ifstream FileIn("Task.txt");
- int check = -1;
- struct Tree {
- int value;
- Tree* left;
- Tree* right;
- };
- void addToTree(Tree **root, int value) {
- if ((*root) == NULL) {
- (*root) = new Tree;
- (*root)->value = value;
- (*root)->left = NULL;
- (*root)->right = NULL;
- }
- else if (value <= (*root)->value) {
- addToTree(&(*root)->left, value);
- }
- else {
- addToTree(&(*root)->right, value);
- }
- }
- void fillTree(Tree** root) {
- int val;
- while (1) {
- FileIn >> val;
- if (FileIn.eof()) {
- break;
- }
- addToTree(&(*root), val);
- }
- }
- void sortPrint(Tree** root, int level){
- if (*root != NULL){
- sortPrint(&((**root).right), level + 1);
- for (int i = 1; i <= level; i++) {
- std::cout << "\t";
- }
- std::cout << (**root).value << std::endl;
- sortPrint(&((**root).left), level + 1);
- }
- }
- Tree* Search(Tree* root, int value) {
- if (root == NULL) {
- return NULL;
- }
- if (root->value == value) {
- check = 1;
- return root;
- }
- if (value < root->value) {
- return Search(root->left, value);
- }
- else {
- return Search(root->right, value);
- }
- }
- void deleteTree(Tree* root) {
- if (root != NULL) {
- deleteTree(root->left);
- deleteTree(root->right);
- delete root;
- }
- }
- Tree* searchInInterval(Tree* root, int a, int b) {
- if (root == NULL) {
- return NULL;
- }
- if (root->value >= b) {
- searchInInterval(root->left, a, b);
- }
- if (root->value <= a) {
- searchInInterval(root->right, a, b);
- }
- if (root->value > a && root->value < b) {
- //if (root->value != a && root->value != b) {
- std::cout << root->value << " ";
- //}
- searchInInterval(root->left, a, b);
- searchInInterval(root->right, a, b);
- }
- }
- void menu(int variant, Tree *root, int value, int a, int b) {
- std::cout << "please, select the option\n" << "1. Print the tree\n2. Add value to tree\n3. Search for value in tree\n4. Search for values in interval (a,b)\n5. Exit" << std::endl;
- while (variant != 5) {
- std::cin >> variant;
- switch (variant) {
- case 1:
- std::cout << "===============================================\n";
- sortPrint(&root, 0);
- std::cout << "===============================================\n";
- break;
- case 2:
- std::cout << "===============================================\n";
- std::cout << "Enter value to add\n";
- std::cin >> value;
- addToTree(&root, value);
- std::cout << "Add is successful\n";
- value = 0;
- std::cout << "===============================================\n";
- break;
- case 3:
- std::cout << "===============================================\n";
- std::cout << "Enter value to find\n";
- std::cin >> value;
- Search(root, value);
- if (check == -1) {
- std::cout << "Element with this value doesn't exist in the tree\n";
- }
- else if (check == 1) {
- std::cout << "Element with this value exist in the tree\n";
- }
- check = -1;
- std::cout << "===============================================\n";
- break;
- case 4:
- std::cout << "===============================================\n";
- std::cout << "Write down borders of interval\n";
- std::cin >> a>> b;
- if (a > b) {
- std::swap(a, b);
- }
- std::cout << "a = " << a << " b = " << b << std::endl;
- searchInInterval(root, a, b);
- std::cout << std::endl;
- std::cout << "===============================================\n";
- break;
- case 5:
- std::cout << "===============================================\n";
- std::cout << "Have a nice day :)\n";
- break;
- default:
- std::cout << "===============================================\n";
- std::cout << "Wrong command, try again\n";
- break;
- }
- }
- }
- int main() {
- Tree* root = NULL;
- fillTree(&root);
- int value = 0, variant = 0, a =0, b = 0;
- menu(variant, root, value,a,b);
- deleteTree(root);
- return 0;
- }
Add Comment
Please, Sign In to add comment