Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.56 KB | None | 0 0
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. class node;
  6. node * Tree_root;
  7. class node
  8. {
  9. public:
  10.     node * left;
  11.     node * parent;
  12.     node * right;
  13.     char name[15];
  14.     long long value;
  15.  
  16.     node(long long V, char N[], node * P){
  17.         left = NULL;
  18.         right= NULL;
  19.         parent = P;
  20.         strcpy(name, N);
  21.         value = V;
  22.     };
  23.     ~node(){
  24.         if(left) delete left;
  25.         if(right) delete right;
  26.     };
  27.  
  28.     static bool insert(node * root, node * to_add){
  29.  
  30.         if(root->value==to_add->value)
  31.             return false;
  32.  
  33.         if(root->value > to_add->value){
  34.             if(root->left==NULL){
  35.                 root->left = to_add;
  36.                 to_add->parent = root;
  37.             } else {
  38.                 return insert(root->left, to_add);
  39.             }
  40.         } else {
  41.             if(root->right==NULL){
  42.                 root->right = to_add;
  43.                 to_add->parent = root;
  44.             } else {
  45.                 return insert(root->right, to_add);
  46.             }
  47.         }
  48.         return true;
  49.     };
  50.     static node *find(node * root, long long  V){ //znajduje element o wartosci V
  51.         if(root==NULL){
  52.             return NULL;
  53.         } else {
  54.             if(root->value==V){
  55.                 return root;
  56.             } else if(root->value>V) {
  57.                 return find(root->left, V);
  58.             } else{
  59.                 return find(root->right, V);
  60.             }
  61.         }
  62.     };
  63.     static void remove(long long V){
  64.     };
  65.     static bool del(node * &root, long long  V){ //usuwa element o kluczu V
  66.         node *DelNode = find(root, V);
  67.         if(DelNode==NULL){
  68.             return false;
  69.         }
  70.         if(DelNode->left==NULL && DelNode->right==NULL){ //brak synow
  71.             if(DelNode==Tree_root){
  72.                 Tree_root = NULL;
  73.                 //return true;
  74.             } else {
  75.                 if(DelNode==DelNode->parent->left){ //usuwany jest lewy
  76.                     DelNode->parent->left = NULL;
  77.                     //return true;
  78.                 }else if(DelNode==DelNode->parent->right){ //usuwany jest prawy
  79.                     DelNode->parent->right = NULL;
  80.                     //return true;
  81.                 }
  82.             }
  83.         } else if(DelNode->left!=NULL && DelNode->right==NULL){ //tylko lewy syn
  84.             if(DelNode==Tree_root){ //jak usuwany jest korzeniem
  85.                 Tree_root = Tree_root->left;
  86.                 Tree_root->parent=NULL;
  87.                 //return true;
  88.             } else {
  89.                 if(DelNode==DelNode->parent->left){ //usuwany jest lewy
  90.                     DelNode->parent->left = DelNode->left;
  91.                     DelNode->left->parent = DelNode->parent;
  92.                     //return true;
  93.                 } else
  94.                 if(DelNode==DelNode->parent->right){ //usuwany jest prawy
  95.                     DelNode->parent->right = DelNode->left;
  96.                     DelNode->left->parent = DelNode->parent;
  97.                     //return true;
  98.                 }
  99.             }  
  100.         } else if(DelNode->left==NULL && DelNode->right!=NULL){ //tylko prawy syn
  101.             if(DelNode==Tree_root){ //jak usuwany jest korzeniem
  102.                 Tree_root = Tree_root->right;
  103.                 Tree_root->parent=NULL;
  104.                 //return true;
  105.             } else {
  106.                 if(DelNode==DelNode->parent->left){ //usuwany jest lewy
  107.                     DelNode->parent->left = DelNode->right;
  108.                     DelNode->right->parent = DelNode->parent;
  109.                     //return true;
  110.                 } else
  111.                 if(DelNode==DelNode->parent->right){ //usuwany jest prawy
  112.                     DelNode->parent->right = DelNode->right;
  113.                     DelNode->right->parent = DelNode->parent;
  114.                     //return true;
  115.                 }
  116.             }
  117.         } else if(DelNode->left!=NULL && DelNode->right!=NULL){ //lewy i prawy syn
  118.             node * next = succ(DelNode);
  119.             strcpy(DelNode->name,  next->name);
  120.             DelNode->value = next->value;
  121.             //zastąpienie następnika jego prawym synem
  122.             node * par = next->parent;
  123.             if(next->right==NULL){
  124.                 if(next==par->right){
  125.                     par->right = NULL;
  126.                 } else if(next==par->left){
  127.                     par->left = NULL;
  128.                 }
  129.             } else{
  130.                 if(next==par->right){
  131.                     par->right = next->right;
  132.                     next->right->parent = par;
  133.                 } else if(next==par->left){
  134.                     par->left = next->right;
  135.                     next->right->parent = par;
  136.                 }
  137.             }
  138.             //return true;
  139.         }
  140.        
  141.         return true;
  142.     };
  143.     static void print(node * root){
  144.         if(root -> left != NULL){
  145.             print(root -> left);
  146.         }
  147.         printf("%010lld ", root->value);
  148.         //cout << "Main: " << root <<" parent: " << root->parent << " left: " << root->left <<" right:" << root->right << endl;
  149.         //cout << root->name << endl;
  150.         printf("%s\n", root->name);
  151.         if(root ->right != NULL){
  152.             print(root ->right);
  153.         }
  154.     };
  155.     static bool cmp(string &a, string &b){
  156.         return false;
  157.     };
  158.     static node * succ(node * x)
  159.     {
  160.         if(x->right) return minnode(x->right);
  161.  
  162.         node * y;
  163.  
  164.         do
  165.         {
  166.             y = x;
  167.             x = x->parent;
  168.         } while(x && (x->left != y));
  169.  
  170.         return x;
  171.     }
  172.     static node * minnode(node * x){
  173.         if(x!=NULL){
  174.             while(x->left!=NULL)
  175.                 x = x->left;
  176.         }
  177.         return x;
  178.     }
  179. };
  180.  
  181. int main(){
  182.     int z;
  183.     scanf("%d", &z);
  184.     while(z--){
  185.         int n;
  186.         Tree_root = NULL;
  187.         scanf("%d", &n);
  188.         for(int i = 0; i<n; i++){
  189.             char cmd[20];
  190.             scanf("%s", cmd);
  191.             if(cmd[0]=='I'){
  192.                 long long V;
  193.                 char name[15];
  194.                 scanf("%lld%s", &V, name);
  195.                 node * temp = new node(V, name, NULL);
  196.                 if(Tree_root==NULL){
  197.                     Tree_root = temp;
  198.                     printf("OK\n");
  199.                 } else{
  200.                     if(node::insert(Tree_root, temp)){
  201.                         printf("OK\n");
  202.                     } else {
  203.                         delete temp;
  204.                         printf("ERROR\n");
  205.                     }
  206.                 }
  207.             } else if(cmd[0]=='F'){
  208.                 long long V;
  209.                 scanf("%lld", &V);
  210.                 node * res = node::find(Tree_root, V);
  211.                 if(res == NULL){
  212.                     printf("FIND ERROR\n");
  213.                 } else {
  214.                     printf("FIND %010lld %s\n", res->value, res->name);
  215.                 }
  216.             } else if(cmd[0]=='D'){
  217.                 long long V;
  218.                 scanf("%lld", &V);
  219.                 bool res = node::del(Tree_root, V);
  220.  
  221.                 if(res==false){
  222.                     printf("ERROR\n");
  223.                 } else{
  224.                     //res->left = NULL;
  225.                     //res->right = NULL;
  226.                     //delete res;
  227.                     printf("OK\n");
  228.                 }
  229.                 //delete res;
  230.             } else if(cmd[0]=='P'){
  231.                 if(Tree_root==NULL){
  232.                     printf("EMPTY\n");
  233.                 } else{
  234.                     node::print(Tree_root);
  235.                 }
  236.             }
  237.         }
  238.         delete Tree_root;
  239.     }
  240.     return 0;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement