bool disjointset::link(TreeNode* x, TreeNode* y)
{
if ( x == y )
returnfalse;
if ( x->getRango() > y->getRango() )
{
y->setParent(x);
x->getChilds()->push_back(y);
list<TreeNode*>* l = x->getChilds();
for(auto it = l->begin(); it != l->end(); ++it)
cout << "Disjoint with: " << (*it)->getKey() << " : "<< (*it)->getValue();
}
else
{
x->setParent(y);
y->getChilds()->push_back(x);
list<TreeNode*>* l = y->getChilds();
for(auto it = l->begin(); it != l->end(); ++it)
cout << "Disjoint with: " << (*it)->getKey() << " : "<<(*it)->getValue();
}
if ( x->getRango() == y->getRango() )
y->setRango(1);
returntrue;
}
if i make union between: 10 and 100 after union 100 -> 10,
now make union from 12 and 100 after union 100 -> 10 -> 12,
but in first step print only 10 and ok, in second step print only 12 and not 10 and 12.
Please show your full, compileable program. Preferably as a single file, so that we can run it in cpp.sh and not have to download lots of individual bits.
#include <iostream>
#include <string>
#include <list>
using std::string;
using std::cin;
using std::cout;
using std::list;class TreeNode
{
private:
int key;
string value;
int rango;
TreeNode* parent;
list<TreeNode*>* childs;
public:
TreeNode(int key, string value)
{
this->key=key;
this->value=value;
this->rango=0;
this->parent=this;
this->childs= new list<TreeNode*>;
}
list <TreeNode*>* getChilds() { return childs; }
int getRango() { return rango; }
void setRango(int r) { rango = r; }
void setParent(TreeNode *p) { parent = p; }
int getKey() const { return key;}
const string &getValue() const { return value; };
};
booldisjointset(TreeNode* x, TreeNode* y)
{
if ( x == y )
returnfalse;
if ( x->getRango() > y->getRango() )
{
y->setParent(x);
x->getChilds()->push_back(y);
list<TreeNode*>* l = x->getChilds();
for(auto it = l->begin(); it != l->end(); ++it)
cout << "Disjoint with: " << (*it)->getKey() << " : "<< (*it)->getValue();
}
else
{
x->setParent(y);
y->getChilds()->push_back(x);
list<TreeNode*>* l = y->getChilds();
for(auto it = l->begin(); it != l->end(); ++it)
cout << "Disjoint with: " << (*it)->getKey() << " : "<<(*it)->getValue();
}
if ( x->getRango() == y->getRango() )
y->setRango(1);
returntrue;
}
int main()
{
int key;
string value;
cin >> key >> value;
TreeNode n1(key, value);
cin >> key >> value;
TreeNode n2(key, value);
disjointset(&n1, &n2);
}
this is the output after i make union with elements: (10,100) (12,100)
I assume "elements" means TreeNode's. I assumed "10,100" and "12,100" means those are the parameters to the constructor. You can see all this in the main program above. When I run that, I get this output:
Disjoint with: 10 : 100
That isn't even in the same format as the output you posted.
I hope this helps you see things from our perspective. We don't know what the code is supposed to do. We can't compile it as you've presented it and even after attempting to fix it, it doesn't generate the output that you posted. So it's very difficult to help with your problem.