Java implementation-data structure of the binary tree (5): Huffman tree first understanding and code construction

Data structure of the binary tree (5): Huffman tree

Basic introduction:
Given n weights as n leaf nodes , if the weighted path length (WPL) of the tree reaches the minimum, such a binary tree is called the optimal binary tree , also known as the Huffman tree .
The Huffman tree is the tree with the shortest weighted path length , andthe node with the largest weight is closer to the root.

Basic terms:
path and path length
In a tree, the path between children or grandchildren that can be reached from a node down is called a path. The number of branches in the path is called the path length. If the number of levels of the root node is specified to be 1, the path length from the root node to the node of the Lth layer is L-1.

Node weight and weighted path length
If a node in the tree is assigned a value with a certain meaning, then this value is called the weight of the node. The weighted path length of a node is: the product of the length of the path from the root node to the node and the weight of the node.

The length of the weighted path of the
tree The length of the weighted path of the tree is defined as the sum of the length of the weighted path of all leaf nodes, denoted as WPL.

For example:
Insert picture description here
the weighted path length (WPL) of the tree=13x2+7x2+8x2+3x2=62

Insert picture description here
The weighted path length of the tree is 7x1+3x2+8x3+13x3=76

Steps to build a Huffman tree

1) Sorting from small to large, each data is a node, which can be regarded as the simplest binary tree.
2) Take out the two binary trees with the smallest root node weight to form a new binary tree. The weight of the root node of the new binary tree is the sum of the root node weights of the previous two binary trees.
3) Then the new binary tree, Sort again by the weight of the root node, and repeat these three steps until all the data is processed, and a Huffman tree is constructed.

The following is an example:
the nodes with weights of 13, 7, 8 , 3, 29, 6, 1 construct a binary tree.
First sort them in ascending order: 1, 3, 6, 7, 8, 13, 29

The first step : first take out the two trees with the smallest root node weight to construct a new binary tree, that is, take 1, 3 out. It becomes as follows:
Insert picture description here
**Step 2: **The weight of the root node of the newly obtained tree becomes 4, and then add it to the remaining nodes and sort again, which is 4, 6, 7, 8, 13, 29. Perform the first step again (take 4, 6) to get the following figure:
Insert picture description here
Step 3: Sort the new tree with the root node weight of 10 and the remaining trees again to get 7, 8, 10, 13, 29, perform the first step again, and get the following figure:
Insert picture description here
**Step 4:** The new tree with the root node weight of 15 is sorted again, get 10, 13, 15, 29, and the first step is performed again Get the picture below:
Insert picture description here
Step 5 : Repeat this cycle until all nodes are used up, and finally get the following picture:
Insert picture description here

**

java code to create a huffman tree

**:

package Tree05;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HuffmanTree {
    
    
	public static void main(String[] args) {
    
    
		int arr[]= {
    
    13,7,8,3,29,6,1};
		Node root = createHuffmanTree(arr);
		frontShow(root);
	}
	//创建哈夫曼树
	public static Node createHuffmanTree(int[] arr) {
    
    
		//遍历arr数组,将arr的每个元素构建成一棵树(Node)
		//再将Node放入到ArrayList中
		List<Node> nodes=new ArrayList<Node>();
		for(int value: arr) {
    
    
			nodes.add(new Node(value));
		}
		while(nodes.size()>1) {
    
    
			//排序,升序
			Collections.sort(nodes);//node实现了Comparable接口,可以直接进行排序
			//取出权值最小的两棵二叉树
			Node leftNode=nodes.get(0);
			Node rightNode=nodes.get(1);
			//构建一棵新的二叉树
			Node parent=new Node(leftNode.value+rightNode.value);
			parent.left=leftNode;
			parent.right=rightNode;
			//从ArrayList中删除处理过的二叉树
			nodes.remove(leftNode);
			nodes.remove(rightNode);
			//将parent加入到nodes
			nodes.add(parent);
		}
		//返回哈夫曼树的root节点
		return nodes.get(0);
	}
	//前序遍历
	public static void frontShow(Node root) {
    
    
		if(root!=null) {
    
    
			root.frontShow();
		}else {
    
    
			System.out.println("空树,不能遍历");
		}
	}
}

//创建节点类
//便于节点按权值进行排序,实现Comparable接口
class Node implements Comparable<Node>{
    
    
	int value;//节点权值
	Node left;//左子节点
	Node right;//右子节点
	
	public Node(int value) {
    
    
		this.value=value;
	}

	@Override
	public String toString() {
    
    
		return "Node [value=" + value + "]";
	}
	
	@Override
	public int compareTo(Node o) {
    
    
		// TODO Auto-generated method stub
		//表示从小到大进行排序
		return this.value - o.value;
	}
	
	
	public void frontShow() {
    
    
		System.out.println(this);
		if(this.left!=null) {
    
    
			this.left.frontShow();
		}
		if(this.right!=null) {
    
    
			this.right.frontShow();
		}
	}
	
	
}

Output result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45273552/article/details/109122141