Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <string>
- const std::string Traverses[3] = {"INFIX","PREFIX","POSTFIX"};
- struct node {
- node* left = nullptr;
- node* parent = nullptr;
- node* right = nullptr;
- int info;
- bool IsRed = 1;
- };
- void checkEasy(node*&);
- void showTree (node* &r, const int mode){
- if (mode == 0)
- {
- if (r->left != nullptr)
- showTree(r -> left,mode);
- std::cout << "[" << r -> info << "] ";
- if (r->right != nullptr)
- showTree(r -> right,mode);
- }
- if (mode == 1)
- {
- std::cout << "[" << r -> info << "] ";
- if (r->left != nullptr)
- showTree(r -> left,mode);
- if (r->right != nullptr)
- showTree(r -> right,mode);
- }
- if (mode == 2)
- {
- if (r->left != nullptr)
- showTree(r -> left,mode);
- if (r->right != nullptr)
- showTree(r -> right,mode);
- std::cout << "[" << r -> info << "] ";
- }
- }
- node* findGran(node *&n){
- if ((n != nullptr) && (n -> parent != nullptr))
- return n -> parent -> parent;
- else
- return nullptr;
- }
- node* findUncle(node *&n){
- node* g = findGran(n);
- if (g == nullptr)
- return nullptr;
- if (n -> parent == g -> left)
- return g -> right;
- else
- return g -> left;
- }
- void rotRight(node* &n)
- {
- node* p = n -> left;
- p -> parent = n -> parent;
- if (n -> parent != nullptr)
- {
- if (n -> parent -> left == n)
- n -> parent -> left = p;
- else
- n -> parent -> right = p;
- }
- n -> left = p -> right;
- if (p -> right != nullptr)
- p -> right -> parent = n;
- n -> parent = p;
- p -> right = n;
- }
- void rotLeft(node* &n)
- {
- node* p = n -> right;
- p -> parent = n -> parent;
- if (n -> parent != nullptr) {
- if (n -> parent -> left == n)
- n -> parent -> left = p;
- else
- n -> parent -> right = p;
- }
- n->right = p->left;
- if (p -> left != nullptr)
- p -> left -> parent = n;
- n -> parent = p;
- p -> left = n;
- }
- void checkTree(node* &n){
- node *u = findUncle(n);
- node *g = findGran(n);
- if ((u != nullptr) && (u -> IsRed == 1))
- {
- n -> parent -> IsRed = 0;
- u -> IsRed = 0;
- g -> IsRed = 1;
- checkEasy(g);
- }
- else
- {
- if ((n == n -> parent -> right) && (n -> parent == g -> left))
- {
- rotLeft(n->parent);
- n = n->left;
- }
- else
- if ((n == n->parent->left) && (n->parent == g->right))
- {
- rotRight(n->parent);
- n = n->right;
- }
- g = findGran(n);
- n->parent->IsRed = 0;
- g->IsRed = 1;
- if ((n == n->parent->left) && (n->parent == g->left))
- rotRight(g);
- if ((n == n->parent->right) && (n->parent == g->right))
- rotLeft(g);
- }
- }
- void checkEasy(node* &n){
- if (n -> parent == NULL)
- n -> IsRed = 0;
- else
- {
- if (n -> parent -> IsRed == 0)
- return;
- else
- checkTree(n);
- }
- }
- int readInt(int &h){
- std::string str = "";
- char ch = 0;
- while (1)
- {
- ch = getch ();
- if ((ch == '-') and (str.length() == 0))
- {
- putch(ch);
- str += ch;
- }
- if ((ch >= '0') and (ch <= '9'))
- {
- putch(ch);
- str += ch;
- }
- if ((ch == 8) and (str.length() != 0))
- {
- putch(ch);
- putch(0);
- putch(ch);
- str.pop_back();
- }
- if (ch == 13)
- {
- h=atoi(str.c_str());
- return 0;
- }
- if (ch == 'q')
- return 1;
- }
- }
- void addValue(node* p, node* &r, int &num){
- if (r == nullptr)
- {
- r = new node;
- r -> info = num;
- r -> parent = p;
- checkEasy(r);
- return;
- }
- if (num > r-> info)
- addValue(r, r -> right, num);
- if (num < r-> info)
- addValue(r, r -> left, num);
- }
- bool findValue(node* &r, int &num){
- if (r == nullptr)
- return 0;
- if (num == r -> info)
- return 1;
- if (num > r -> info)
- if (findValue(r -> right, num))
- return 1;
- if (num < r -> info)
- if (findValue(r -> left, num))
- return 1;
- return 0;
- }
- void changeTraverse(int &s){
- char c = ' ';
- while (c!=13)
- {
- system("cls");
- std::cout << "\n\n [Choose the traverse you need by arrows & 'Enter']\n\n\n";
- for (int i=0;i<3;i++)
- {
- if (i==s)
- std::cout<<"-> ";
- else
- std::cout<<" ";
- std::cout << Traverses[i]<<std::endl;
- }
- c=getch();
- if (c==80)
- s=(s+1)%3;
- if (c==72)
- s=(s+2)%3;
- }
- }
- node* createTree(){
- node* x=new node;
- int h;
- system("cls");
- std::cout << "\n\n [Enter first value, or press 'Q' to exit]\n>";
- if (readInt(h))
- exit(0);
- x -> info = h;
- x -> IsRed = 0;
- system("cls");
- std::cout << "\n\n |Current Tree|\n\n ";
- showTree (x,0);
- while (1)
- {
- std::cout << "\n\n [Enter another value, to complete, press 'Q']\n>";
- if (readInt(h))
- break;
- addValue(nullptr, x, h);
- showTree(x,0);
- system("pause");
- system("cls");
- std::cout << "\n\n |Current Tree|\n\n ";
- showTree (x,0);
- }
- return x;
- }
- int maxValue(node* r){
- if (r -> right == nullptr)
- return r -> info;
- return maxValue(r->right);
- }
- int minValue(node* r){
- if (r -> left == nullptr)
- return r -> info;
- return minValue(r->left);
- }
- void deleteTree(node* r){
- if (r -> left != nullptr)
- deleteTree(r -> left);
- if (r -> right != nullptr)
- deleteTree(r -> right);
- delete r;
- }
- node* removeValue(node *r, int n){
- if (r == nullptr)
- return r;
- if (n < r -> info)
- r->left = removeValue(r->left, n);
- else if (n > r -> info)
- r->right = removeValue(r->right, n);
- else
- {
- if ((r->left != nullptr) and (r->right != nullptr))
- {
- r->info = minValue(r->right);
- r->right = removeValue(r->right, r->info);
- }
- else
- {
- if (r->left != nullptr)
- {
- node* t=r;
- r = r->left;
- delete t;
- }
- else if (r->right != nullptr)
- {
- node* t=r;
- r = r->right;
- delete t;
- }
- else
- {
- delete r;
- return nullptr;
- }
- }
- }
- return r;
- }
- void SomeKindaUI(){
- node* Root;
- int num, s = 0;
- Root=createTree();
- system("cls");
- while (1)
- {
- if (Root==nullptr)
- Root=createTree();
- std::cout << "\n\n |Current Tree|\n\n ";
- showTree (Root,s);
- std::cout << "\n\n\nTo delete value, press '1'.\nTo add value, press '2'.\nTo recreate a tree, press '3'.\nTo change traverse, press '4'.\nTo find a value, press '5'.\nTo find min value, press '6'.\nTo find max value, press '7'.\nPress 'q' to quit. \n ";
- char ch = getch();
- if (ch=='q')
- break;
- if (ch=='1')
- {
- std::cout << "\n\n [Enter value to delete]\n>";
- readInt(num);
- if (not findValue(Root, num))
- std::cout<< "There is no such an element";
- else
- Root=removeValue(Root,num);
- }
- if (ch=='2')
- {
- std::cout << "\n\n [Enter another value]\n>";
- readInt(num);
- addValue(nullptr, Root, num);
- }
- if (ch=='3')
- {
- deleteTree(Root);
- Root = createTree();
- }
- if (ch=='4')
- changeTraverse(s);
- if (ch=='5')
- {
- std::cout << "\n\n [Enter value to find]\n>";
- readInt(num);
- std::cout << std::endl;
- if (findValue(Root, num))
- std::cout<< "True";
- else
- std::cout<< "False";
- std::cout << "\n\nPress any key to continue...";
- getch();
- }
- if (ch=='6')
- {
- std::cout << "\n\nMin Value: "<< minValue(Root);
- std::cout << "\n\nPress any key to continue...";
- getch();
- }
- if (ch=='7')
- {
- std::cout << "\n\nMax Value: "<< maxValue(Root);
- std::cout << "\n\nPress any key to continue...";
- getch();
- }
- system("cls");
- }
- }
- int main() {
- SomeKindaUI();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement