#include <iostream>
#include <vector>
using namespace std;
struct tree{
int weight;//权值
int rchild;//右孩子
int lchild;//左孩子
int parent;//父亲
};
void g(vector<tree> &t)
{
int size = t.size();
for(vector<tree>::iterator i = t.begin();i != t.end(); i++)
{
(*i).parent = -1;
}
for(int i = 1; i!=size; i++)
{
int low1 = 100, low2 = 100;
int a, b;
for(int i = 0;i != t.size(); i++)
{
if(-1 == t[i].parent)
{
if(t[i].weight<low1)
{
low1 = (t[i]).weight;
a = i;
}
else
if((t[i]).weight<low2)
{
low2 = (t[i]).weight;
b = i;
}
}
}
(t[a]).parent = t.size();
(t[b]).parent = t.size();
tree m;
m.lchild = a;
m.rchild = b;
m.weight = low1+low2;
m.parent = -1;
t.push_back(m);
}
for(int i = 0; i < size; i++)
{
{
int p = i;
vector<int> out;
while(t[p].parent!=-1)
{
// cout<<(t[p]).parent<<" ";
if(t[t[p].parent].rchild == p)
{
out.push_back(1);
}
else
{
out.push_back(0);
}
p = t[p].parent;
}
for(vector<int>::iterator it_ = out.begin(); it_!=out.end();it_++)
{
cout<<*it_;
}
cout<<endl;
}
}
}
int main()
{
vector<tree> t;
for(int i = 0; i < 10; i++)
{
tree s;
s.weight = i;
s.lchild = s.rchild = 0;
t.push_back(s);
}
g(t);
cout<<endl<<endl;
for(vector<tree>::iterator i = t.begin(); i != t.end(); i++)
{
cout<<(*i).weight<<" "<<(*i).rchild<<" "<<(*i).lchild<<" "<<(*i).parent<<endl;
}
return 0;
}
#include <vector>
using namespace std;
struct tree{
int weight;//权值
int rchild;//右孩子
int lchild;//左孩子
int parent;//父亲
};
void g(vector<tree> &t)
{
int size = t.size();
for(vector<tree>::iterator i = t.begin();i != t.end(); i++)
{
(*i).parent = -1;
}
for(int i = 1; i!=size; i++)
{
int low1 = 100, low2 = 100;
int a, b;
for(int i = 0;i != t.size(); i++)
{
if(-1 == t[i].parent)
{
if(t[i].weight<low1)
{
low1 = (t[i]).weight;
a = i;
}
else
if((t[i]).weight<low2)
{
low2 = (t[i]).weight;
b = i;
}
}
}
(t[a]).parent = t.size();
(t[b]).parent = t.size();
tree m;
m.lchild = a;
m.rchild = b;
m.weight = low1+low2;
m.parent = -1;
t.push_back(m);
}
for(int i = 0; i < size; i++)
{
{
int p = i;
vector<int> out;
while(t[p].parent!=-1)
{
// cout<<(t[p]).parent<<" ";
if(t[t[p].parent].rchild == p)
{
out.push_back(1);
}
else
{
out.push_back(0);
}
p = t[p].parent;
}
for(vector<int>::iterator it_ = out.begin(); it_!=out.end();it_++)
{
cout<<*it_;
}
cout<<endl;
}
}
}
int main()
{
vector<tree> t;
for(int i = 0; i < 10; i++)
{
tree s;
s.weight = i;
s.lchild = s.rchild = 0;
t.push_back(s);
}
g(t);
cout<<endl<<endl;
for(vector<tree>::iterator i = t.begin(); i != t.end(); i++)
{
cout<<(*i).weight<<" "<<(*i).rchild<<" "<<(*i).lchild<<" "<<(*i).parent<<endl;
}
return 0;
}