用java写.已知四个带权的结点:(A,1),(B,2),(C,2),(D,3),构造Huffman数,并给出每个结点的
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/13 21:03:12
用java写.已知四个带权的结点:(A,1),(B,2),(C,2),(D,3),构造Huffman数,并给出每个结点的编码.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Huffman {
public static void main(String[] args) {
Huffman huff = new Huffman();
//准备数据
Node node_a = new Node("A", 1);
Node node_b = new Node("B", 2);
Node node_c = new Node("C", 2);
Node node_d = new Node("D", 3);
ArrayList<Node> list = new ArrayList<Node>();
list.add(node_a);
list.add(node_b);
list.add(node_c);
list.add(node_d);
//建树
huff.build(list);
//根
Node root = list.get(0);
//编码
huff.setHuffmanCode(root);
//
System.out.println(node_a.getName()+":"+node_a.huffmanCodeString);
System.out.println(node_b.getName()+":"+node_b.huffmanCodeString);
System.out.println(node_c.getName()+":"+node_c.huffmanCodeString);
System.out.println(node_d.getName()+":"+node_d.huffmanCodeString);
}
private void setHuffmanCode(Node huff) {
Node left = huff.getNodeLeft();
// 左节点追加"0"
if (left != null) {
left.huffmanCodeString = huff.huffmanCodeString + "0";
setHuffmanCode(left);
}
Node right = huff.getNodeRight();
// 右节点追加"1"
if (right != null) {
right.huffmanCodeString = huff.huffmanCodeString + "1";
setHuffmanCode(right);
}
}
public void build(List<Node> list) {
// 按权值从小到大排序
if (list.size() > 2)
Collections.sort(list, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return o1.getWeight() - o2.getWeight();
}
});
//移除最小的2个
Node l = list.get(0);
Node r = list.get(1);
list.remove(l);
list.remove(r);
//生成一个新的,并设置左右子节点
Node newNode = new Node(l.getName() + r.getName(), l.getWeight() + r.getWeight());
newNode.setNodeLeft(l);
newNode.setNodeRight(r);
if (list.isEmpty()) {// 根节点
list.add(newNode);
return;
} else {
list.add(newNode);
build(list);
}
}
}
class Node {
//存储编码
public String huffmanCodeString = "";
private String name; // 名称
private int weight; // 权值
private Node nodeLeft; // 左节点
private Node nodeRight;// 右节点
public Node(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Node getNodeLeft() {
return nodeLeft;
}
public void setNodeLeft(Node nodeLeft) {
this.nodeLeft = nodeLeft;
}
public Node getNodeRight() {
return nodeRight;
}
public void setNodeRight(Node nodeRight) {
this.nodeRight = nodeRight;
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Huffman {
public static void main(String[] args) {
Huffman huff = new Huffman();
//准备数据
Node node_a = new Node("A", 1);
Node node_b = new Node("B", 2);
Node node_c = new Node("C", 2);
Node node_d = new Node("D", 3);
ArrayList<Node> list = new ArrayList<Node>();
list.add(node_a);
list.add(node_b);
list.add(node_c);
list.add(node_d);
//建树
huff.build(list);
//根
Node root = list.get(0);
//编码
huff.setHuffmanCode(root);
//
System.out.println(node_a.getName()+":"+node_a.huffmanCodeString);
System.out.println(node_b.getName()+":"+node_b.huffmanCodeString);
System.out.println(node_c.getName()+":"+node_c.huffmanCodeString);
System.out.println(node_d.getName()+":"+node_d.huffmanCodeString);
}
private void setHuffmanCode(Node huff) {
Node left = huff.getNodeLeft();
// 左节点追加"0"
if (left != null) {
left.huffmanCodeString = huff.huffmanCodeString + "0";
setHuffmanCode(left);
}
Node right = huff.getNodeRight();
// 右节点追加"1"
if (right != null) {
right.huffmanCodeString = huff.huffmanCodeString + "1";
setHuffmanCode(right);
}
}
public void build(List<Node> list) {
// 按权值从小到大排序
if (list.size() > 2)
Collections.sort(list, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return o1.getWeight() - o2.getWeight();
}
});
//移除最小的2个
Node l = list.get(0);
Node r = list.get(1);
list.remove(l);
list.remove(r);
//生成一个新的,并设置左右子节点
Node newNode = new Node(l.getName() + r.getName(), l.getWeight() + r.getWeight());
newNode.setNodeLeft(l);
newNode.setNodeRight(r);
if (list.isEmpty()) {// 根节点
list.add(newNode);
return;
} else {
list.add(newNode);
build(list);
}
}
}
class Node {
//存储编码
public String huffmanCodeString = "";
private String name; // 名称
private int weight; // 权值
private Node nodeLeft; // 左节点
private Node nodeRight;// 右节点
public Node(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Node getNodeLeft() {
return nodeLeft;
}
public void setNodeLeft(Node nodeLeft) {
this.nodeLeft = nodeLeft;
}
public Node getNodeRight() {
return nodeRight;
}
public void setNodeRight(Node nodeRight) {
this.nodeRight = nodeRight;
}
}
6、求java算法 已知四个带权的结点:(A,1),(B,2),(C,2),(D,3),构造Huffman数,并给出每个
画出以3,4,6,8,12,13,15,18,25,40为结点权值所构造的Huffman树,并对各结点编码
具有12个结点的完全二叉树有 B .A.5个叶子结点 B.5个度为2的结点 C.7个分支结点 D.2个度为1的结点
一棵树T中,包括一个度为1的结点,两个度为2的结点,三个度为3的结点,四个度为4的结点和若干叶子结点,则T的叶结点数为
有七个带权结点,其权值分别为3,7,8,2,6,10,14,试以它们为叶结点构造一棵哈夫曼树(请按照每个结点的左子树根结
对于给定的8个实数W={8,6,23,15,4,20,35,10};试构造huffman树,并求出每个叶子结点的哈夫曼编
以{5,6,7,8,9,10,15,18,22}作为叶子结点的权值构造一颗Huffman树,计算带权路径长度
3.某二叉树有5个度为2的结点,则该二叉树中的叶子结点数是( C ).A) 10 B) 8 C) 6 D) 4
某二叉树有5个度为2的结点,则该二叉树中的叶子结点数是 A)10 B)8 C)6 D)4
一棵度为3的树有2个度为1的结点,3个度为2的结点,4个度为3的结点,则有多少个叶子结点?给出公式和计算方...
设树T的度为4,其中度为1,2,3,4的结点个数分别为4,2,1,1.则T中的叶子节点数为()A.8 B.7 C.6 D
《数据结构》用广义表的带表头结点的存储表示法表示下列集合 A = ( ) B = (6, 2)C = (‘a’,( 5,