lz78压缩算法c语言实现,Java 实现LZ78压缩算法的示例代码

LZ78 压缩算法的 Java 实现

1、压缩算法的实现

通过多路搜索树提高检索速度

package com.wretchant.lz78;

import java.util.*;

/**

多路英文单词查找树

*/

class Trie {

private TrieNode root;

public Trie() {

root = new TrieNode();

root.wordEnd = false;

}

public void insert(String word) {

TrieNode node = root;

for (int i = 0; i < word.length(); i++) {

Character c = word.charAt(i);

if (!node.childdren.containsKey(c)) {

node.childdren.put(c, new TrieNode());

}

node = node.childdren.get(c);

}

node.wordEnd = true;

}

public boolean search(String word) {

TrieNode node = root;

for (int i = 0; i < word.length(); i++) {

Character c = word.charAt(i);

if (!node.childdren.containsKey(c)) {

return false;

}

node = node.childdren.get(c);

}

return node.wordEnd;

}

}

class TrieNode {

Map childdren;

boolean wordEnd;

public TrieNode() {

childdren = new HashMap();

wordEnd = false;

}

}

/**

编码表

*/

class Output {

private Integer index;

private Character character;

Output(Integer index, Character character) {

this.index = index;

this.character = character;

}

public Integer getIndex() {

return index;

}

public Character getCharacter() {

return character;

}

}

class LZencode {

@FunctionalInterface

interface Encode {

List encode(String message);

}

/**

构建多路搜索树

*/

static Trie buildTree(Set keys) {

Trie trie = new Trie();

keys.forEach(trie::insert);

return trie;

}

public static final Encode ENCODE = message -> {

// 构建压缩后的编码表

List outputs = new ArrayList<>();

Map treeDict = new HashMap<>();

int mLen = message.length();

int i = 0;

while (i < mLen) {

Set keySet = treeDict.keySet();

// 生成多路搜索树

Trie trie = buildTree(keySet);

char messageI = message.charAt(i);

String messageIStr = String.valueOf(messageI);

// 使用多路树进行搜索

if (!trie.search(messageIStr)) {

outputs.add(new Output(0, messageI));

treeDict.put(messageIStr, treeDict.size() + 1);

i++;

} else if (i == mLen - 1) {

outputs.add(new Output(treeDict.get(messageIStr), ' '));

i++;

} else {

for (int j = i + 1; j < mLen; j++) {

String substring = message.substring(i, j + 1);

String str = message.substring(i, j);

// 使用多路树进行搜索

if (!trie.search(substring)) {

outputs.add(new Output(treeDict.get(str), message.charAt(j)));

treeDict.put(substring, treeDict.size() + 1);

i = j + 1;

break;

}

if (j == mLen - 1) {

outputs.add(new Output(treeDict.get(substring), ' '));

i = j + 1;

}

}

}

}

return outputs;

};

}

2、解压缩算法的实现

package com.wretchant.lz78;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class LZdecode {

@FunctionalInterface

interface Decode {

/**

@param outputs 编码表

@return 解码后的字符串

*/

String decode(List outputs);

}

/**

根据编码表进行解码

*/

public static final Decode DECODE = (List outputs) -> {

StringBuilder unpacked = new StringBuilder();

Map treeDict = new HashMap<>();

for (Output output : outputs) {

Integer index = output.getIndex();

Character character = output.getCharacter();

if (index == 0) {

unpacked.append(character);

treeDict.put(treeDict.size() + 1, character.toString());

continue;

}

String term = "" + treeDict.get(index) + character;

unpacked.append(term);

treeDict.put(treeDict.size() + 1, term);

}

return unpacked.toString();

};

}

3、测试和使用

package com.wretchant.lz78;

import java.io.InputStream;

import java.util.List;

import java.util.Scanner;

import java.util.function.ToIntFunction;

public class LZpack {

public static final ToIntFunction> DICT_PRINT = outputs -> {

outputs.forEach(output -> {

System.out.println("index :" + output.getIndex() + " char :" + output.getCharacter());

});

return 1;

};

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Please input text ");

String input = scanner.nextLine();

LZencode.Encode encode = LZencode.ENCODE;

List outputs = encode.encode(input);

DICT_PRINT.applyAsInt(outputs);

}

}

测试结果如下

a5ae16ced0663baf840fc5431180548e.png

4、Python 版本的实现代码

def compress(message):

tree_dict, m_len, i = {}, len(message), 0

while i < m_len:

# case I

if message[i] not in tree_dict.keys():

yield (0, message[i])

tree_dict[message[i]] = len(tree_dict) + 1

i += 1

# case III

elif i == m_len - 1:

yield (tree_dict.get(message[i]), '')

i += 1

else:

for j in range(i + 1, m_len):

# case II

if message[i:j + 1] not in tree_dict.keys():

yield (tree_dict.get(message[i:j]), message[j])

tree_dict[message[i:j + 1]] = len(tree_dict) + 1

i = j + 1

break

# case III

elif j == m_len - 1:

yield (tree_dict.get(message[i:j + 1]), '')

i = j + 1

def uncompress(packed):

unpacked, tree_dict = '', {}

for index, ch in packed:

if index == 0:

unpacked += ch

tree_dict[len(tree_dict) + 1] = ch

else:

term = tree_dict.get(index) + ch

unpacked += term

tree_dict[len(tree_dict) + 1] = term

return unpacked

if __name__ == '__main__':

messages = ['ABBCBCABABCAABCAAB', 'BABAABRRRA', 'AAAAAAAAA']

for m in messages:

pack = compress(m)

unpack = uncompress(pack)

print(unpack == m)

到此这篇关于Java 实现LZ78压缩算法的文章就介绍到这了,更多相关Java LZ78压缩算法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值