January 09, 2025

How to calculate the length of the Huffman tree weighted path

What is the length of the weighted path of the Huffman tree?

1. Tree path length

The path length of the tree is the sum of the path lengths from the root of the tree to each node in the tree. In a binary tree with the same number of nodes, the path length of the complete binary tree is the shortest.

2. Weighted path length of the tree (Weighted Path Length of Tree, abbreviated as WPL)

Node weight: In some applications, a real number that has a meaning assigned to a node in the tree.

The length of the weighted path of the node: the product of the length of the path from the node to the root of the tree and the weight of the node.

Weighted Path Length of Tree: Defined as the sum of the weighted path lengths of all leaf nodes in the tree, usually written as:

among them:

n represents the number of leaf nodes

Wi and li respectively represent the weight of the leaf node ki and the path length between the root and the node ki.

The length of the weighted path of the tree is also known as the cost of the tree.

3. Optimal binary tree or Huffman tree

In all binary trees composed of n leaves with weights wl, w2, ..., wn, the binary tree with the smallest weight path (ie, the least cost) is called the optimal binary tree or Huffman tree.

[Example] Given 4 leaf nodes a, b, c and d, respectively, weights 7, 5, 2 and 4. Construct three binary trees (and many others) as shown in the following figure, their weighted path The lengths are:

(a) WPL=7*2+5*2+2*2+4*2=36

(b) WPL=7*3+5*3+2*1+4*2=46

(c) WPL=7*1+5*2+2*3+4*3=35

The (c) tree has the smallest WPL and can be verified. It is the Huffman tree.

note:

1 When the weights on the leaves are the same, the complete binary tree must be the optimal binary tree. Otherwise, the complete binary tree is not necessarily the optimal binary tree.

2 In the optimal binary tree, the larger the weight, the closer the leaves are to the root.

3 The shape of the optimal binary tree is not unique, and the WPL is the smallest.

How to ask Huffman's weighted path length

【Problem Description】

It is known to input two lines of positive integers, and the second line of positive integers is separated by a space bar. Please create a Huffman tree, and enter the number as a leaf node to find the length of the weighted path of the Huffman tree.

[input form]

First, the first behavior enters the number of positive integers, then the next line of positive integers, representing leaf nodes, the number of positive integers does not exceed 1000

[output form]

Output the corresponding weight

[sample input]

5

4 5 6 7 8

[Sample output]

69

About the Huffman Tree -

1, path length

A branch from one node in the tree to another constitutes a path between two nodes, and the number of branches on the path is called the path length.

How to calculate the length of the Huffman tree weighted path

1, the path length of the tree

The path length is the sum of the path lengths from the root of the tree to each node.

How to calculate the length of the Huffman tree weighted path

Fully Transparent Liquid Crystal Display

Fully Transparent Liquid Crystal Display,Industrial Instrument Lcd Display,Household Appliances Lcd Display,Fire Facility Instrument Display

Dongguan Yijia Optoelectronics Co., Ltd. , http://www.everbestlcdlcm.com