In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article gives you an introduction to Java how to achieve the creation of Huffman tree, the content is very detailed, interested friends can refer to, I hope to help you.
What is a Huffman tree?
Given N weights as N leaf nodes, a binary tree is constructed, and if the weighted path length (WPL) of the tree reaches a minimum, such a binary tree is called an optimal binary tree, also known as a Huffman tree. Huffman tree is the tree with shortest weighted path length, and the nodes with larger weights are closer to the root.
Figure 1 A Huffman tree
1. Path and Path Length
A path between child or grandchild nodes reachable from a node down in a tree is called a path.
For example, the path from the root node to node b in Figure 1 is called a path.
For each node in a path, the path length is incremented by 1. If the number of root nodes is 1, then the path length from root node to Lth node is L-1.
For example, the path length from root node to node c in Figure 1 is 4 - 1 = 3.
2. Weight of nodes and weighted path length
If a node in a tree is assigned a value with some meaning, this value is called the node's weight.
For example, the weights of abcd nodes in Figure 1 are 12, 5, 6, and 21, respectively.
The weighted path length of a node is the product of the path length from the root node to the node and the weight of the node.
For example, the weighted path length of node c in Figure 1 is 3 * 6 = 18.
3. Weighted Path Length of Trees
The weighted path length of a tree is defined as the sum of the weighted path lengths of all leaf nodes, and is denoted as WPL.
For example, WPL =(5 + 6)* 3 + 12 * 2 + 21 = 78
Second, create Huffman tree 1. Graphic creation process
Assuming there are n weights, the Huffman tree constructed has n leaf nodes. If the n weights are w1, w2,..., wn, then the Huffman tree construction rules are:
例如有四个叶子节点 a b c d 权值分别为 12、5、6、21
创建赫夫曼树前森林如下
(1) 将w1、w2、…,wn看成是有n 棵树的森林(每棵树仅有一个结点);
(2) 在森林中选出两个根结点的权值最小的树合并,作为一棵新树的左、右子树,且新树的根结点权值为其左、右子树根结点权值之和;
在森林中取出 b c节点 形成一棵新树M
(3)从森林中删除选取的两棵树,并将新树加入森林;
将新树M添加到森林后 森林如下
(4)重复(2)、(3)步,直到森林中只剩一棵树为止,该树即为所求得的哈夫曼树。
** 4.1重复步骤(2)
在森林中取出权为11的节点以及a节点组成一棵新树N
** 4.2重复步骤(3)
将新树N添加到森林中 森林如下
** 4.3重复步骤(2)
在森林中取出b节点和权为23的节点组成一棵新树S
则新树S就是我们要创建的赫夫曼树
2.代码实现
创建赫夫曼树的过程中,为确保每次从森林中取出的节点为最小值,这里采用快速排序算法,每次取出节点前,将森林中的树按照权值从小到大重新排列一次
节点的结构如下:
class Node implements Comparable { private int element; //节点的权 private Node left; //节点的左子树 private Node right; //节点的右子树 //构造器 public Node(int aElement) { this.element = aElement; } public int getElement() { return element; } public void setElement(int element) { this.element = element; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } //前序遍历 public void preOrder() { System.out.print(this + " "); if (this.getLeft() != null) { this.getLeft().preOrder(); } if (this.getRight() != null) { this.getRight().preOrder(); } } @Override public String toString() { return element + ""; } @Override public int compareTo(Node o) { return this.getElement() - o.getElement(); //从小大到排序 }}
完整代码如下:
package com.xx.huffmantree;import java.util.*;/** * @author 谢鑫 * @version 1.0 * @date 2021/12/7 16:31 * 赫夫曼树 */public class HuffmanTreeDemo { public static void main(String[] args) { int[] arr = {12, 5, 6, 21}; HuffmanTree huffmanTree = new HuffmanTree(); Node root = huffmanTree.creTree(arr); huffmanTree.preOrder(root); }}class HuffmanTree { public Node creTree(int[] aArr) { List list = new ArrayList(); //用于存放数组元素 //将数组放存放list中 for (int element : aArr) { list.add(new Node(element)); } while (list.size() > 1) { //循环创建树 Collections.sort(list); //从小到大排序 //从list中从小取出两个节点 Node left = list.get(0); Node right = list.get(1); //初始化小树根节点 Node root = new Node(left.getElement() + right.getElement()); //小树根节点为左右子树节点element值的和 //构建小树 root.setLeft(left); root.setRight(right); list.add(root); //将小树根节点再次添加到list中 //移除集合中已经参与构建过树的节点 list.remove(left); list.remove(right);// list.remove(0);// list.remove(0); //取出两个队头元素 也可 } return list.get(0); } //前序遍历 public void preOrder(Node aRoot) { if (aRoot != null) { aRoot.preOrder(); } else { System.out.println("此树为空, 无法完成前序遍历!"); } }}class Node implements Comparable { private int element; //节点的权 private Node left; //节点的左子树 private Node right; //节点的右子树 //构造器 public Node(int aElement) { this.element = aElement; } public int getElement() { return element; } public void setElement(int element) { this.element = element; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } //前序遍历 public void preOrder() { System.out.print(this + " "); if (this.getLeft() != null) { this.getLeft().preOrder(); } if (this.getRight() != null) { this.getRight().preOrder(); } } @Override public String toString() { return element + ""; } @Override public int compareTo(Node o) { return this.getElement() - o.getElement(); //从小大到排序 }}
最后我们采用前序遍历输出我们创建的赫夫曼树,结果如下
关于Java怎样实现赫夫曼树的创建就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.