Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>  
  4.  
  5. using namespace std;
  6.  
  7. class Node
  8. {
  9.     public:
  10.     string key;
  11.     string line;
  12.     Node *LChild;
  13.     Node *RChild;
  14.  
  15.     Node(string key, string line)
  16.     {
  17.         this->key=key;
  18.     this->line=line;
  19.         LChild=NULL;
  20.         RChild=NULL;
  21.     }
  22. };
  23.  
  24. class Tree
  25. {
  26. public:
  27.     Node *root;
  28.  
  29.     Tree(){
  30.         root=NULL;
  31.     }
  32.    
  33.     void Add(Node* x, string key, string line){
  34.       //Node* n = new Node(key, line);
  35.       if(root=NULL){
  36.     Node* n = new Node(key, line);
  37.     root = n;
  38.       } else {
  39.     if(key.compare(x->key) == 0){
  40.       x->line = x->line + " " + line;
  41.       //delete n;
  42.     } else if(key.compare(x->key) <0){ //idziemy na lewo
  43.       if(x->LChild == NULL){
  44.         Node* n = new Node(key, line);
  45.         x->LChild = n;
  46.       } else {
  47.         Add(x->LChild, key, line);
  48.       }
  49.     } else {
  50.       if(x->RChild == NULL){
  51.         Node* n = new Node(key, line);
  52.         x->RChild = n;
  53.       } else {
  54.         Add(x->RChild, key, line);
  55.       }
  56.     }
  57.       }
  58.      
  59.     }
  60.      
  61.     void InOrder(Node* x)
  62.     {
  63.         if(x->LChild!=NULL)
  64.         {
  65.             InOrder(x->LChild);
  66.         }
  67.             cout<<x->key<< " => "<<endl;
  68.  
  69.             if(x->RChild!=NULL)
  70.         {
  71.             InOrder(x->RChild);
  72.         }
  73.  
  74.     }
  75. };    
  76.    
  77. int main()
  78. {
  79.     int nl;
  80.     string line = NULL;
  81.     string key = NULL;
  82.     Tree* tree = new Tree();
  83.     cin>> nl;
  84.     for(int i = nl; i>0; --i){
  85.       cin>> line;
  86.       stringstream j;
  87.       j << line;
  88.       int k;
  89.       j >> k;
  90.       for(k; k>0; --k){
  91.     cin>> key;
  92.     tree->Add(tree->root, key, line);
  93.       }
  94.     }
  95.  
  96.     tree->InOrder(tree->root);
  97.    
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement