优先队列优化哈夫曼编码

2024-06-02 18:36

本文主要是介绍优先队列优化哈夫曼编码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

个人小记


一、代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_NODE 80
#define MAX_STR 50
#define MAX_HEAP 100
#define father(i) ((i)/2)
#define left(i)   ((i)*2)
#define right(i)  ((i)*2+1)typedef struct Node
{int f;char ch;struct Node *lchild,*rchild;
}Node;typedef struct Heap
{int count,size;Node** __data,**data;
}Heap;void swap(Node** a,Node** b)
{Node* t=*a;*a=*b;*b=t;return ;
}void down_update(Heap*,int);Heap* init_heap(int n)
{Heap* h=(Heap*)malloc(sizeof(Heap));h->count=0;h->size=n;h->__data=(Node**)malloc(sizeof(Node*)*n);h->data=h->__data-1;return h;
}int isempty(Heap *h)
{return h->count==0;
}int isfull(Heap* h)
{return h->count==h->size;
}void up_update(Heap* h,int i)
{while(i>1&&h->data[i]->f<h->data[father(i)]->f){swap(&(h->data[i]),&(h->data[father(i)]));i=father(i);}return ;
}int push(Heap* h,Node* node)
{if(isfull(h)){printf("堆已满,无法压入\n");return 0;}h->data[++h->count]=node;up_update(h,h->count);return 1;
}Node* top(Heap* h)
{if(isempty(h)){printf("堆空,无法查看\n");return 0;}return h->data[1];
}int pop(Heap* h)
{if(isempty(h)){printf("堆空,无法弹出\n");return 0;}h->data[1]=h->data[h->count--];down_update(h,1);return 1;
}void down_update(Heap* h,int i)
{while(left(i)<=h->count){int min=i,l=left(i),r=right(i);if(h->data[min]->f>h->data[l]->f)min=l;if(right(i)<=h->count&&h->data[min]->f>h->data[r]->f)min=r;if(i==min)break;swap(&(h->data[i]),&(h->data[min]));i=min;}return ;
}Node* init_node(char ch,int f)
{Node* node=(Node* )malloc(sizeof(Node));node->f=f;node->ch=ch;node->lchild=node->rchild=NULL;return node;
}
int fc=0;
Node** init_arr(int n,int* s)
{fc=0;Node** arr=(Node**)malloc(sizeof(Node*)*n);for(int i=0;i<n;i++) arr[i]=(Node*)malloc(sizeof(Node));for(int i=0;i<256;i++){if(s[i]!=0){Node* node=init_node((char)i,s[i]);arr[fc]=node;fc++;}}return arr;
}void clear_tree(Node* root)
{if(root==NULL)return ;clear_tree(root->lchild);clear_tree(root->rchild);free(root);return ;
}int* count(char str[],int n)
{int *s=(int*)malloc(sizeof(int)*256);for(int i=0;i<256;i++)s[i]=0;for(int i=0;i<n;i++)s[(int)str[i]]+=1;return s;
}Node* CLO_build_halfman_tree(Node** arr,Heap* h,int n)
{for(int i=0;i<n-1;i++){Node* node1=top(h);pop(h);Node* node2=top(h);pop(h);Node* new_node=init_node('0',node1->f+node2->f);new_node->lchild=node1;new_node->rchild=node2;push(h,new_node);arr[n-i-2]=new_node;}return arr[0];
}void clear_heap(Heap* h)
{if(h==NULL)return ;free(h->__data);free(h);return ;
}void halfman_coding(Node* root,char buff[],int k)
{buff[k]=0;if(root->lchild==NULL&&root->rchild==NULL){printf("%c:%s\n",root->ch,buff);return ;}buff[k]='0';halfman_coding(root->lchild,buff,k+1);buff[k]='1';halfman_coding(root->rchild,buff,k+1);return ;
}int main()
{char str[MAX_STR];// scanf("%[^/n]",str);printf("请输入字符串:");fgets(str,MAX_STR,stdin);//s[strcspn(s,'\n')]=0;char *newline = strchr(str, '\n');if (newline != NULL) *newline = '\0';int* s=count(str,strlen(str));Node** arr=init_arr(MAX_NODE,s);Heap* h=init_heap(MAX_HEAP);for(int i=0;i<fc;i++)push(h,arr[i]);//压入堆Node* root=CLO_build_halfman_tree(arr,h,fc);char buff[MAX_STR];halfman_coding(root,buff,0);clear_tree(root);clear_heap(h);return 0;
}

二、测试结果

请输入字符串:aaaaabbccd
a:0
c:10
d:110
b:111

这篇关于优先队列优化哈夫曼编码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1024764

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

Java实现复杂查询优化的7个技巧小结

《Java实现复杂查询优化的7个技巧小结》在Java项目中,复杂查询是开发者面临的“硬骨头”,本文将通过7个实战技巧,结合代码示例和性能对比,手把手教你如何让复杂查询变得优雅,大家可以根据需求进行选择... 目录一、复杂查询的痛点:为何你的代码“又臭又长”1.1冗余变量与中间状态1.2重复查询与性能陷阱1.

Python内存优化的实战技巧分享

《Python内存优化的实战技巧分享》Python作为一门解释型语言,虽然在开发效率上有着显著优势,但在执行效率方面往往被诟病,然而,通过合理的内存优化策略,我们可以让Python程序的运行速度提升3... 目录前言python内存管理机制引用计数机制垃圾回收机制内存泄漏的常见原因1. 循环引用2. 全局变

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.

Java 中编码与解码的具体实现方法

《Java中编码与解码的具体实现方法》在Java中,字符编码与解码是处理数据的重要组成部分,正确的编码和解码可以确保字符数据在存储、传输、读取时不会出现乱码,本文将详细介绍Java中字符编码与解码的... 目录Java 中编码与解码的实现详解1. 什么是字符编码与解码?1.1 字符编码(Encoding)1

MySQL中优化CPU使用的详细指南

《MySQL中优化CPU使用的详细指南》优化MySQL的CPU使用可以显著提高数据库的性能和响应时间,本文为大家整理了一些优化CPU使用的方法,大家可以根据需要进行选择... 目录一、优化查询和索引1.1 优化查询语句1.2 创建和优化索引1.3 避免全表扫描二、调整mysql配置参数2.1 调整线程数2.