Data structure-------Huffman tree

Huffman Tree

basic introduction

Given n weights as n leaf nodes , a binary tree is constructed. If the weighted path length (wpl) of the tree reaches the minimum , the binary tree is called the optimal binary tree, which is also called Huffman Tree (Huffman Tree). ), and some books are translated as Hoffman trees .

In other words: the Huffman tree is the tree with the shortest weighted path length , and the node with the larger weight is closer to the root

Some concepts

Path and path length : In a tree, the path between children or grandchildren nodes 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 layers 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

for example:

Insert picture description here

The power of the node: 13 14 5 16

The weighted path length of the node is 13 as an example. His path is 2 from the first layer to the third layer, so it is 13*2 = 26

The weighted path length of the tree: The weighted path length of the tree is defined as the sum of the weighted path lengths of all leaf nodes , denoted as WPL (weighted path length), the node with the higher the weight is closer to the root node The binary tree is the optimal binary tree .

Huffman tree creation ideas

Given that we have a sequence {13,7,8,3,29,6,1}, it is required to be converted into a Huffman tree

step:

1. Sort from small to large , treat each data as a node, and each node can be regarded as the simplest binary tree

2. Take out the two binary trees with the smallest root node weight

3. To form a new binary tree , the weight of the root node of the new binary tree is the sum of the weights of the root nodes of the previous two binary trees

4. And then Fengyun new binary, the value of the root node to the right size of the sort again , repeating steps 1-2-3-4, until the number of columns, all the data are processed, to obtain a Huffman tree

step:

  1. Sort 1, 3, 4, 7, 8, 13, 29

  2. Pick 1 and 3 to build a new binary tree

Insert picture description here

Combine with number 6

Insert picture description here

7,8 is smaller than 10, take them out separately to form a new binary tree, and so on

Insert picture description here

Code

package 赫夫曼树;

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

public class HuffmanTree {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int arr[] = {
    
    13,7,8,3,29,6,1};
		Node node = createHuffmanTree(arr);
		preOrder(node);
	}
	
	
	//创建赫夫曼树的方法
	public static Node createHuffmanTree(int[] arr) {
    
    
		//先把数组中的所有元素取出来,构建成Node类型
		//放入到ArrayList中,可以排序
		List<Node> nodes = new ArrayList<Node>();
		for(int value : arr){
    
    
			nodes.add(new Node(value));
		}
		while(nodes.size() > 1){
    
    
			//排序  我们是从小到大
			Collections.sort(nodes);
			System.out.println(nodes);
			//取根节点权值最小的两颗二叉树
			//取出最小的二叉树节点(二叉树)
			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);
			System.out.println("第一次处理后"+nodes);
		}
		//返回赫夫曼树的root节点
		return nodes.get(0);
	}
	//前序便利方法
	public static void preOrder(Node root) {
    
    
		if(root != null){
    
    
			root.preOrder();
		}else
		{
    
    
			System.out.println("空树");
		}
	}

}
//创建节点类
//为了让Node支持排序 Collections集合排序
//让Node实现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 preOrder() {
    
    
		System.out.println(this);
		if(this.left != null){
    
    
			this.left.preOrder();
		}
		if(this.right != null){
    
    
			this.right.preOrder();
		}
	}
	
}

Results of the

[Node [value=1], Node [value=3], Node [value=6], Node [value=7], Node [value=8], Node [value=13], Node [value=29]]
第一次处理后[Node [value=6], Node [value=7], Node [value=8], Node [value=13], Node [value=29], Node [value=4]]
[Node [value=4], Node [value=6], Node [value=7], Node [value=8], Node [value=13], Node [value=29]]
第一次处理后[Node [value=7], Node [value=8], Node [value=13], Node [value=29], Node [value=10]]
[Node [value=7], Node [value=8], Node [value=10], Node [value=13], Node [value=29]]
第一次处理后[Node [value=10], Node [value=13], Node [value=29], Node [value=15]]
[Node [value=10], Node [value=13], Node [value=15], Node [value=29]]
第一次处理后[Node [value=15], Node [value=29], Node [value=23]]
[Node [value=15], Node [value=23], Node [value=29]]
第一次处理后[Node [value=29], Node [value=38]]
[Node [value=29], Node [value=38]]
第一次处理后[Node [value=67]]
Node [value=67]
Node [value=29]
Node [value=38]
Node [value=15]
Node [value=7]
Node [value=8]
Node [value=23]
Node [value=10]
Node [value=4]
Node [value=1]
Node [value=3]
Node [value=6]
Node [value=13]

Guess you like

Origin blog.csdn.net/qq_22155255/article/details/113825447