This section describes common application scenarios of Binary Trees. Haha ..............
First of all, we need to know what a user is. The so-called Harman tree is also called an optimal binary tree. It refers to a binary tree with the minimum length of the weighted path for a group of leaf nodes with the fixed weights.
This section describes some basic concepts of the Harman tree.
(1) path: the branch from one node in the tree to another node constitutes the path between the two nodes.
(2) path length: number of branches in the path.
(3) path length of tree: The sum of the path length from the root node of the tree to each node. In a binary tree with the same number of nodes, the path length of the Complete Binary Tree is the shortest.
(4) Weight of node: in some applications, assign a meaningful number of nodes to the tree.
(5) Weight path length of node: the product of the length of the path from the node to the root node of the tree and the weight of the node.
(6) weighted path length (WPL): the sum of the weighted path lengths of all leaf nodes in the tree, which is recorded as Σ = n1kkwpllw
A:
Each of the four Binary Trees shown in has four leaf nodes. Their weights are 1, 2, 3, and 4 respectively. Their weighted path lengths are:
WL = (1 + 2 + 3 + 4) * 2 = 20:
WL = (3 + 4) * 3 + 2*2 + 1 = 26:
WPL = 1x3 + 2x3 + 3X2 + 4x1 = 19:
WPL = 2 × 1 + 1 × 2 + 3 × 3 + 4 × 3 = 25:
So, how to construct a husky tree? He first gave an algorithm with a general rule, commonly known as the algorithm. The description is as follows:
(1) Based on the given n weights {W1, W2 ,..., Wn}, construct n Binary Tree sets with only root nodes F = {T1, T2 ,..., Tn };
(2) Select the binary tree with the minimum weight of the two root nodes from set F as the left and right Subtrees to construct a new binary tree, the weights of the root node of the new binary tree are the sum of the weights of the left and right child root nodes.
(3) Delete these two shards in set F and add the new binary tree to set F;
(4) Repeat the above steps until there is only one binary tree in the collection, and this binary tree is the Harman tree. The steps are as follows:
According to the Construction Algorithm of the Harman tree, an array is used to store the original n leaf nodes and the nodes temporarily generated during the construction process. The size of the array is 2n-1. Therefore, there are two member fields in the huffmantree: The data array is used to store nodes, and leafnum indicates the number of leaf nodes in the huffmantree. A node has four fields, one of which is weight, which is used to store the weight of the node. The other is lchild, which is used to store the serial number of the left child node of the node in the array. The other is rchild, the serial number of the right child node that stores the node in the array. A parent field is used to determine whether the node has been added to the Harman tree. The structure of the Harman Tree node is ,:
Therefore, a node class has four member fields. Weight indicates the weight of the node, and lchild and rchild indicate the serial numbers of the left and right child nodes in the array, respectively, parent indicates whether the node has been added to the table store. If the value of parent is-1, it indicates that the node has not been added to the table store. When the node has been added to the Harman tree, the value of parent is the serial number of its parent node in the array.
The node class is defined as follows:
Public class Node
{
Private int weight; // node weight
Private int lchild; // left child node
Private int rchild; // The right child node.
Private int parent; // parent node
// Node weight attribute
Public int weight
{
Get
{
Return weight;
}
Set
{
Weight = value;
}
}
// Left child node attribute
Public int lchild
{
Get
{
Return lchild;
}
Set
{
Lchild = value;
}
}
// Right child node attributes
Public int rchild
{
Get
{
Return rchild;
}
Set
{
Rchild = value;
}
}
// Parent node attributes
Public int parent
{
Get
{
Return parent;
}
Set
{
Parent = value;
}
}
// Constructor
// Null is the default value.
Public node ()
{
Weight = 0;
Lchild =-1;
Rchild =-1;
Parent =-1;
}
// Constructor
// Assign values to weights, nodes, and nodes.
Public node (int w, int LC, int RC, int P)
{
Weight = W;
Lchild = Lc;
Rchild = RC;
Parent = P;
}
}
There is only one member method create in the huffmantree of the huffmantree class. Its function is to input the weights of N leaf nodes and create a huffmantree. The implementation of the huffmantree class is as follows. :
Public class huffmantree
{
Private node [] data; // node Array
Private int leafnum; // Number of leaf nodes
// The indexer uses the current indexer to traverse.
Public node this [int Index]
{
Get
{
Return data [Index];
}
Set
{
Data [Index] = value;
}
}
// Attributes of the number of leaf nodes
Public int leafnum
{
Get
{
Return leafnum;
}
Set
{
Leafnum = value;
}
}
// The constructor creates a 2 * n-1 binary tree with the right leaf node n
Public huffmantree (int n)
{
Data = new node [2 * n-1];
Leafnum = N;
}
// Create a user-defined tree and start creating the User-Defined tree.
Public void create ()
{
Int M1;
Int m2;
Int x1;
Int X2;
// Enter the weight of N leaf nodes
For (INT I = 0; I <this. leafnum; ++ I)
{
Data [I]. Weight = console. Read ();
}
// Process n leaf nodes and create a Harman tree
For (INT I = 0; I <this. leafnum-1; ++ I)
{
Max1 = max2 = int32.maxvalue;
Tmp1 = tmp2 = 0;
// Add the node with the smallest weight to the Harman tree.
// Find the two nodes with the minimum weight in all nodes
For (Int J = 0; j <this. leafnum + I; ++ J)
{
If (data [I]. weight <max1)
& (Data [I]. Parent =-1 ))
{
Max2 = max1;
Tmp2 = tmp1;
Tmp1 = J;
Max1 = data [J]. weight;
}
Else if (data [I]. weight <max2)
& (Data [I]. Parent =-1 ))
{
Max2 = data [J]. weight;
Tmp2 = J;
}
}
Data [tmp1]. Parent = This. leafnum + I; // Add
Data [This. leafnum + I]. Weight = data [tmp1]. Weight
+ Data [tmp2]. weight;
Data [This. leafnum + I]. lchild = tmp1;
Data [This. leafnum + I]. rchild = tmp2;
}
}
}
// The time complexity of this algorithm is O (n²), as shown in.
We spent so much time introducing the husky tree and what he could do, namely, the coding. He writes the path of each other as 01. The most common application of this application is win-zip. Win-Rar is based on this foundation.
Example 2. Write an algorithm to find the node with the value in the binary tree.
The algorithm is implemented as follows:
Node <t> Search (node <t> root, T value)
{
Node <t> P = root;
If (P = NULL)
{
Return NULL;
}
If (P. Data. Equals (value ))
{
Return P;
}
If (P. lchild! = NULL)
{
Return search (P. lchild, value );
}
If (P. rchild! = NULL)
{
Return search (P. rchild, value );
}
Return NULL; Because recursion is used, the time complexity of this algorithm is O (n² ):
}
Count the number of leaf nodes in a binary tree.
Is to count the number of non-subnodes in the binary tree. Search by recursion. Implement this algorithm recursively. If the binary tree is empty, 0 is returned. If the binary tree has only one node, the root node is the leaf node, and 1 is returned, otherwise, the number of leaf nodes in the left branch of the root node and the number of leaf nodes in the right branch are returned.
Int countleafnode (node <t> root)
{
If (root = NULL)
{
Return 0;
}
Else if (root. lchild = NULL & root. rchild = NULL)
{
Return 1;
}
Else
{
Return (countleafnode (root. lchild) +
Countleafnode (root. rchild ));
}
Due to the implementation of recursive algorithms, the time complexity is O (n²). The implementation of the algorithms is as follows ,:
}
For the last example, let's talk about the data structure of the tree used in actual commercial projects. For example, if you build a parent-child menu, this is a typical application of the tree. During database design, a field parentcode points to enrichment. His final effect ,:
This is a typical application of the tree data structure. In the next article, we will discuss the structure.