注意是左编号小于右编号而不是左权值小于右权值 

#include<iostream>
const int  MAX_INT = 0x7FFFFFFF;
using namespace std;
typedef struct HFTree {
    int weight;
    int left;
    int right;
    int parent;

}HFTree, * LinkHF;

int main() {
    int n;

    while (cin >> n) {

        LinkHF hf = new HFTree[2 * n];
        for (int i = 1; i < 2 * n; i++) {
            hf[i].left = 0;
            hf[i].right = 0;
            hf[i].parent = 0;

        }
        for (int i = 1; i <= n; i++) {
            cin >> hf[i].weight;

        }
        for (int i = n + 1; i < 2 * n; i++) {
            int m = MAX_INT;
            int n = MAX_INT;
            int x1 = 0;
            int x2 = 0;
            for (int j = 1; j < i; j++) {
                if (hf[j].parent == 0 && hf[j].weight < m) {
                    n = m;
                    x2 = x1;
                    m = hf[j].weight;
                    x1 = j;
                }
                else if (hf[j].parent == 0 && hf[j].weight < n)
                {
                    n = hf[j].weight;
                    x2 = j;
                }
            }

            hf[x1].parent = i;
            hf[x2].parent = i;
            if(x1>x2){
                
                int t;
                t=x1;
                x1=x2;
                x2=t;
                    
            }
         
            
            hf[i].left = x1;
            hf[i].right = x2;
            hf[i].weight = hf[x1].weight + hf[x2].weight;

        }


        for (int i = 1; i < 2 * n; i++) {

            cout << hf[i].weight << "," << hf[i].parent << "," << hf[i].left << "," << hf[i].right << endl;


        }
        cout << endl;
    }

    

    return 0;
}

                  --2020/11/10