Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <sstream>
- using namespace std;
- class Node
- {
- public:
- string key;
- string line;
- Node *LChild;
- Node *RChild;
- Node(string key, string line)
- {
- this->key=key;
- this->line=line;
- LChild=NULL;
- RChild=NULL;
- }
- };
- class Tree
- {
- public:
- Node *root;
- Tree(){
- root=NULL;
- }
- void Add(Node* x, string key, string line){
- //Node* n = new Node(key, line);
- if(root=NULL){
- Node* n = new Node(key, line);
- root = n;
- } else {
- if(key.compare(x->key) == 0){
- x->line = x->line + " " + line;
- //delete n;
- } else if(key.compare(x->key) <0){ //idziemy na lewo
- if(x->LChild == NULL){
- Node* n = new Node(key, line);
- x->LChild = n;
- } else {
- Add(x->LChild, key, line);
- }
- } else {
- if(x->RChild == NULL){
- Node* n = new Node(key, line);
- x->RChild = n;
- } else {
- Add(x->RChild, key, line);
- }
- }
- }
- }
- void InOrder(Node* x)
- {
- if(x->LChild!=NULL)
- {
- InOrder(x->LChild);
- }
- cout<<x->key<< " => "<<endl;
- if(x->RChild!=NULL)
- {
- InOrder(x->RChild);
- }
- }
- };
- int main()
- {
- int nl;
- string line = NULL;
- string key = NULL;
- Tree* tree = new Tree();
- cin>> nl;
- for(int i = nl; i>0; --i){
- cin>> line;
- stringstream j;
- j << line;
- int k;
- j >> k;
- for(k; k>0; --k){
- cin>> key;
- tree->Add(tree->root, key, line);
- }
- }
- tree->InOrder(tree->root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement