Huffman tree (optimal binary tree)

Posted by tHud on Tue, 23 Nov 2021 16:18:20 +0100

Basic introduction:
1) to set n Weights as n individual leaf node , construct a binary tree if the Weighted path length degree ( wpl ) reach To the minimum, such a binary tree is called Optimal binary tree , also known as Huffman tree (Huffman Tree )
2) Hector husband Man tree It is the tree with the shortest weighted path length, and the node with larger weight is closer to the root near .
Concept:
1) road Diameter and path length Degree: In a tree, the path between children or grandchildren that can be reached from one node down is called path. The number of branches in a path is called the path length. If the specified number of layers of the root node is 1 , from the root node to the L The path length of the layer node is L-1
2) Node weight and weighted path length Degree: If a node in the tree is assigned a value with a certain meaning, this value is called the weight of the node. Weighted path length of node Is: the product of the path length from the root node to the node and the weight of the node
3) tree Weighted path length Degree: The weighted path length of the tree is specified as all leaf node The sum of the weighted path lengths of is recorded as WPL(weighted path length ) , The greater the weight, the closer the node is to the root node, and the best binary tree is the binary tree .
4) WPL The smallest is the Huffman tree

{13, 7, 8, 3, 29, 6, 1} 

Steps to form Huffman tree:

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

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

3) Form a new binary tree. The weight of the root node of the new binary tree is the sum of the weight of the root node of the previous two binary trees  

4) Then the new binary tree is sorted again according to the weight of the root node and repeated   Step 1-2-3-4 until all the data in the sequence are processed to obtain a Huffman tree

package com.zhen;

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

public class Huffman {

	public static void main(String[] args) {
		int[] arr= {13,7,8,3,29,6,1};
		Node root=HTree(arr);
		preOrder(root);
	}
	
	public static void preOrder(Node root) {
		if (root!=null) {
			root.preOrder();
		}else {
			System.out.println("empty");
		}
	}
	
	public static Node HTree(int[] arr) {
		//Traverse the arr array, form each element into a Node, and store it in the ArrayList
		List<Node> list = new ArrayList<Node>();
		for(int value:arr) {
			list.add(new Node(value));
		}
		int count=0;
		while(list.size() > 1) {
			//sort
			Collections.sort(list);
			//Take out the two binary trees with the lowest value
			Node left=list.get(0);
			Node right = list.get(1);
			//Construct a new binary tree
			Node parent = new Node(left.value+right.value);
			parent.left=left;
			parent.right = right;
			//Delete used nodes
			list.remove(left);
			list.remove(right);
			//Add the newly generated node to the list
			list.add(parent);
			//Sort output
			Collections.sort(list);
			/*System.out.println("After "+ (+ + count) +" processing: "+ list);*/
		}
		return list.get(0);
	}

}
//Create node class
class Node implements Comparable<Node> {
	//Weight 
	int value;
	//Point to left child node
	Node left;
	//Point to the right child node 
	Node right;
	
	public Node(int value) {
		super();
		this.value = value;
	}
	
	@Override
	public String toString() {
		return "" + value + "";
	}

	//Preorder traversal
	public void preOrder() {
		System.out.println(this);
		if (this.left != null) {
			this.left.preOrder();
		}
		if (this.right != null) {
			this.right.preOrder();
		}
	}
	
	@Override
	public int compareTo(Node o) {
		//Sort from small to large
		return this.value-o.value;
	}
	
}

Topics: Java data structure Back-end