大家 谁知道哈夫曼树,求带权路径长度的算法啊??

conquersky 2006-11-08 04:35:31
如题, 着急 想不明白。。。麻烦大家了
...全文
1770 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bingbinggongzi260 2006-11-10
  • 打赏
  • 举报
回复
我不是按书上搞的
能正确编码的

#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, int i,char a[])
{

if(T)
{
if((T->Lchild==0)&&(T->Rchild==0))
{
cout<<a;
a[--i]=' ';
}
a[i]='0';
i++;
// cout<<T->ch<<" "<<T->weight<<endl;
travel(T->Lchild,i,a);
a[--i]=' ';
a[++i]='1';
travel(T->Rchild,i,a);
a[i]=' ';
}
}*/

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
//a 1
挺拔的劲松 2006-11-10
  • 打赏
  • 举报
回复
http://blog.csdn.net/sdp/archive/2006/11/10/1378327.aspx
Aaron_Jerry 2006-11-10
  • 打赏
  • 举报
回复
随便找个数据结构书,在树那一章肯定有这个算法
conquersky 2006-11-09
  • 打赏
  • 举报
回复
我顶。。。。。。。。
tanglaoya321 2006-11-09
  • 打赏
  • 举报
回复
void Select(HuffmanTree HT,int a,int &s1,int &s2)
{
int j;int i,k;
for(i=1;i<=a;i++)
{
if(HT[i].parent==0){s1=i;break;}
}
for(j=i+1;j<=a;j++)
{
if(HT[j].parent==0){s2=j;break;}
}
for(k=1;k<=a;k++)
{
if(HT[k].parent==0&&HT[k].weight<HT[s1].weight)s1=k;
}
if(s1==s2)s2=i;
for(k=1;k<=a;k++)
{
if(HT[k].parent==0&&HT[k].weight<HT[s2].weight&&k!=s1)
s2=k;
}



}
void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{
//w存放n个字符的权值都大于0,构造赫夫曼树 ht,并求出n个字符的 赫夫曼编码hc
int m;HuffmanTree p;int i;int s2,s1;char* cd;int start,c,f;
if(n<=1)return;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1;i<=n;++i,++p,++w)
{
p->weight=*w;p->lchild=0;p->parent=0;p->rchild=0;
}
for(;i<=m;++i,++p)
{
p->weight=0;p->lchild=0;p->parent=0;p->rchild=0;
}
for(i=n+1;i<=m;++i)//建赫夫曼树
//在ht[]选择parent为0且 weigeht最小的两个节点,其序号为s1和s2

{
Select(HT,i-1,s1,s2);
HT[s1].parent=i;HT[s2].parent=i;
HT[i].lchild=s1;HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
conquersky 2006-11-08
  • 打赏
  • 举报
回复
用 非递归 算法 。。。。

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧