主题:[原创]哈佛幔树的编码 大家看看怎么样
我自己 想的关于 哈佛幔树的编码 大家看看怎么样
我感觉书上的不是很好
#include <iostream>
#include <string>
using namespace std;
class H
{
private:
char ch;
int weight;
char Code[15];//=" ";
H * parent;
H * Lchild;
H * Rchild;
H * next;
public:
H();
void create();
void order();
void del(H * p);
void insert(H * P);
void Hcode();
void travel(H * T);
void PrintCode(H * T);
};
H * head=new H;
//////////////////////////////////////
H::H()
{
ch='0';
weight=0;
// Code[]=" ";
for(int i=0;i<15;i++)
Code[i]=' ';
Code[15]='\0';
parent=0;
Lchild=0;
Rchild=0;
next=0;
}
void H::create()
{
cout<<"输入字母及其权值:(以0,0结束)\n";
head->parent=head;
head->next=head;
char ch;
int weight;
cin>>ch;
cin>>weight;
H * p1,* p2;
p1=head;
while(ch!='0'||weight!=0)
{
p2=new H;
p2->ch=ch;
p2->weight=weight;
p1->next=p2;
p2->next=head;
p2->parent=p1;
head->parent=p2;
cin>>ch;
cin>>weight;
p1=p1->next;
}
}
void H::order()
{
bool a=false;
H * p1,* p2,* p=head->next,* temp=head;
while(p->next!=head)
{
p1=head->next;
while(p1->next!=temp)
{
p2=p1->next;
if(p1->weight>p2->weight)
{
p1->parent->next=p2;
p2->parent=p1->parent;
p1->parent=p2;
p1->next=p2->next;
p2->next->parent=p1;
p2->next=p1;
if(p1->next==head)
a=true;
if(p1==p)
p=p2;
}
else
p1=p1->next;
if(a)
{
head->parent=p1;
a=false;
}
}
p=p->next;
temp=temp->parent;
}
}
void H::insert(H * p)//插入到头结点后面,成为第一个有效节点
{
p->parent=head;
p->next=head->next;
head->next->parent=p;
head->next=p;
}
void H::Hcode()
{
H * p1,* p2,* p;
while(head->next->next!=head)
{
order();
p1=head->next;
p2=p1->next;
p=new H;
p->weight=p1->weight+p2->weight;
p1->parent=p;
p2->parent=p;
// p1->next=0;
// p2->next=0;
p->Lchild=p1;
p->Rchild=p2;
if(head->next->next->next!=head)
{
head->next=p2->next;
p2->next->parent=head;
}
else
{
head->next=head;
head->parent=head;
}
p1->next=0;
p2->next=0;
insert(p);
}
head=head->next;
head->parent=0;
}
void H::travel(H * T)
{
if(T)
{
if((T->Lchild==0)&&(T->Rchild==0))
PrintCode(T);
//cout<<T->ch<<" "<<T->weight<<endl;
travel(T->Lchild);
travel(T->Rchild);
}
}
void H::PrintCode(H * T)
{
H * p;
p=T;
char a[15]=" ";
int i=0;
while(p->parent)
{
if(p==p->parent->Lchild)
a[i]='0';
else
a[i]='1';
i++;
p=p->parent;
}
for(int t=0;t<i;t++)
T->Code[t]=a[i-t-1];
cout<<T->ch<<" "<<T->Code<<endl;
}
int main()
{
H b;
b.create();
b.Hcode();
b.travel(head);
return 0;
}
//a 9 b 8 c 7 0 0
我感觉书上的不是很好
#include <iostream>
#include <string>
using namespace std;
class H
{
private:
char ch;
int weight;
char Code[15];//=" ";
H * parent;
H * Lchild;
H * Rchild;
H * next;
public:
H();
void create();
void order();
void del(H * p);
void insert(H * P);
void Hcode();
void travel(H * T);
void PrintCode(H * T);
};
H * head=new H;
//////////////////////////////////////
H::H()
{
ch='0';
weight=0;
// Code[]=" ";
for(int i=0;i<15;i++)
Code[i]=' ';
Code[15]='\0';
parent=0;
Lchild=0;
Rchild=0;
next=0;
}
void H::create()
{
cout<<"输入字母及其权值:(以0,0结束)\n";
head->parent=head;
head->next=head;
char ch;
int weight;
cin>>ch;
cin>>weight;
H * p1,* p2;
p1=head;
while(ch!='0'||weight!=0)
{
p2=new H;
p2->ch=ch;
p2->weight=weight;
p1->next=p2;
p2->next=head;
p2->parent=p1;
head->parent=p2;
cin>>ch;
cin>>weight;
p1=p1->next;
}
}
void H::order()
{
bool a=false;
H * p1,* p2,* p=head->next,* temp=head;
while(p->next!=head)
{
p1=head->next;
while(p1->next!=temp)
{
p2=p1->next;
if(p1->weight>p2->weight)
{
p1->parent->next=p2;
p2->parent=p1->parent;
p1->parent=p2;
p1->next=p2->next;
p2->next->parent=p1;
p2->next=p1;
if(p1->next==head)
a=true;
if(p1==p)
p=p2;
}
else
p1=p1->next;
if(a)
{
head->parent=p1;
a=false;
}
}
p=p->next;
temp=temp->parent;
}
}
void H::insert(H * p)//插入到头结点后面,成为第一个有效节点
{
p->parent=head;
p->next=head->next;
head->next->parent=p;
head->next=p;
}
void H::Hcode()
{
H * p1,* p2,* p;
while(head->next->next!=head)
{
order();
p1=head->next;
p2=p1->next;
p=new H;
p->weight=p1->weight+p2->weight;
p1->parent=p;
p2->parent=p;
// p1->next=0;
// p2->next=0;
p->Lchild=p1;
p->Rchild=p2;
if(head->next->next->next!=head)
{
head->next=p2->next;
p2->next->parent=head;
}
else
{
head->next=head;
head->parent=head;
}
p1->next=0;
p2->next=0;
insert(p);
}
head=head->next;
head->parent=0;
}
void H::travel(H * T)
{
if(T)
{
if((T->Lchild==0)&&(T->Rchild==0))
PrintCode(T);
//cout<<T->ch<<" "<<T->weight<<endl;
travel(T->Lchild);
travel(T->Rchild);
}
}
void H::PrintCode(H * T)
{
H * p;
p=T;
char a[15]=" ";
int i=0;
while(p->parent)
{
if(p==p->parent->Lchild)
a[i]='0';
else
a[i]='1';
i++;
p=p->parent;
}
for(int t=0;t<i;t++)
T->Code[t]=a[i-t-1];
cout<<T->ch<<" "<<T->Code<<endl;
}
int main()
{
H b;
b.create();
b.Hcode();
b.travel(head);
return 0;
}
//a 9 b 8 c 7 0 0