Binary tree data structure (a) Huffman coding tree

Data Structure (X)

Finishing learning experiences and knowledge of data structures and algorithms in the process, so that I find myself, and I hope you can share with everyone.

- Huffman coding tree -

1. Title Description

A binary tree structure having n external expansion nodes, each external node has a Ki Wi corresponds to the right as the external node. This extension so that the external path node binary tree leaf weighted sum of minimum length:

        Min( W1 * L1 + W2 * L2 + W3 * L3 + … + Wn * Ln)

Wi: weight of each node value.
Li: distance from the root node to the i-th external leaf node.
Programmed to calculate the minimum length of the sum of the external path.

1.1 input

A first line of input integer t, represents the number of test data sets.
For each test, a first line of the input integer n, the number of external node. The second line of the input n integers, the value of the representation of the respective external node. 2 <= N <= 100

1.2 Output

The minimum length of the sum of the output of the external path.

1.3 Sample input and output

Sample input
2
. 3
. 1 2. 3
. 4
. 1. 1. 3. 5
sample output
. 9
. 17

2. code implementation

c

#include<stdio.h>

void sort(int weight[],int group)
{
    int t;
    for(int i=0;i<group;i++)
    {
        for(int j=0;j<group-i-1;j++)
        {
            if(weight[j]<weight[j+1])
            {
                t=weight[j];
                weight[j]=weight[j+1];
                weight[j+1]=t;
            }
        }
    }
}

int count(int weight[],int group)
{
    if(group==1)
        return 0;
    else
    {
        sort(weight,group);
        weight[group-2]=weight[group-1]+weight[group-2];
        return weight[group-2]+count(weight,group-1);
    }
}

int main()
{
    int num,group;
    scanf("%d",&num);
    while(num--)
    {
        int i;
        scanf("%d",&group);
        int weight[group];
        for(i=0;i<group;i++)
        {
            scanf("%d",&weight[i]);
        }
        printf("%d\n",count(weight,group));
    }
    return 0;
}

c++

#include <iostream>
#include <queue>

using namespace std;

void num_sort(int weight[],int group)
{
    int t;
    for(int i=0;i<group;i++)
    {
        for(int j=0;j<group-i-1;j++)
        {
            if(weight[j]<weight[j+1])
            {
                t=weight[j];
                weight[j]=weight[j+1];
                weight[j+1]=t;
            }
        }
    }
}

int num_count(int weight[],int group)
{
    if(group==1)
        return 0;
    else
    {
        num_sort(weight,group);
        weight[group-2]=weight[group-1]+weight[group-2];
        return weight[group-2]+num_count(weight,group-1);
    }
}

int main()
{
    int num,group;
    cin >>num;
    while(num--)
    {
        int i;
        cin >>group;
        int weight[group];
        for(i=0;i<group;i++)
        {
            cin >>weight[i];
        }
        cout <<num_count(weight,group) <<endl;
    }
    return 0;
}

3. Code Description

In fact, the core algorithm is not difficult, as long as the studied binary tree, do not learn the stack structure can be achieved, is that the sort + computing weights. c and c ++ is not poor, is the basic syntax changed a little bit.
To understand the Huffman coding tree can click on tutorial to see.

Published 36 original articles · won praise 10 · views 5940

Guess you like

Origin blog.csdn.net/qq_44867435/article/details/104359283