# 1: Definition

1: Given n weights as n leaf nodes, a binary tree is constructed. 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 Huffman tree. Other books are translated as Huffman tree.                   2: Huffman tree is the tree with the shortest weighted path length, and the node with larger weight is closer to the root.

## Basic concepts

1: Path and path length: the path between child or grandson nodes that can be reached from one node down in a tree 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, the path length from the root node to the L-th layer node is L-1                                                                                                                                             2: Node weight and weighted path length: if a node in the tree is assigned a value with a certain meaning, this value is called the weight of the node. 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                 3: Weighted path length of the tree: the weighted path length of the tree is specified as the sum of the weighted path lengths of all leaf nodes, which is recorded as WPL(weighted path length). The binary tree with greater weight and closer to the root node is the optimal binary tree. The smallest WPL is Huffman tree

# 2: Create Huffman tree

## Idea:

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

For example: 13, 7, 8, 3, 29, 6, 1   Sort 1, 3, 6, 7, 8, 13, 29

Preorder traversal: 67, 29, 38, 15, 7, 8, 23, 10, 4, 1, 3, 6, 13

```package com.atgguigu.huffmanTree;

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 node = createHuffmanTree(arr);
preOrder(node);
}

public static Node createHuffmanTree(int[] arr){
//Put the array into the Node collection for easy management
List<Node> nodes = new ArrayList<>();
for (int value : arr) {
}

while (nodes.size() > 1){
// 1: Sort
Collections.sort(nodes);

// 2: Take out the two binary trees with the smallest weight of the root node
Node leftNode = nodes.get(0);
Node rightNode = nodes.get(1);

// 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
Node parent = new Node(leftNode.value+ rightNode.value);
parent.left = leftNode;
parent.right = rightNode;

nodes.remove(leftNode);
nodes.remove(rightNode);
// 4: The new binary tree is sorted again according to the weight of the root node
}
return nodes.get(0);
}

//Preorder traversal
public static void preOrder(Node node){
if(node != null){
node.preOrder();
}else {
System.out.println("You play with me? The empty tree is still coming");
}
}
}

//Node class
//Comparable < node > set sorting
class Node implements Comparable<Node>{
int value; //Node weight
Node left;  //Left child node
Node right;  //Right child node

public Node(int value){
this.value = value;
}

@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}

//Comparable < node > set sorting
@Override
public int compareTo(Node o) {
//Indicates sorting from small to large
//return -(this.value-o.value);  Indicates sorting from large to small
return this.value-o.value;
}

//Preorder traversal
public void preOrder(){
System.out.print("==>"+this);

if(this.left != null){
this.left.preOrder();
}

if(this.right != null){
this.right.preOrder();
}
}
}
```

Tags: Java data structure

Posted on Fri, 29 Oct 2021 02:53:46 -0400 by Mattyspatty