#include <stdio.h>
#define N 5
typedef node {
int w; //가중치
NODE ic, rc; //트리의 자식링크
NODE next; //트리를 구성 요소로 하는 리스트의 연결링크
};
Node list = NULL;
/*리스트에서 가중치가 가장 작은 원소(트리)를 가져오는 함수*/
NODE least(){
NODE t;
if(list == NULL)
return NULL;
t = list;
list = list->next;
return t;
}
/*리스트에서 원소(트리)를 가중치 순서에 맞게 삽입하는 함수*/
void insert(NODE t){
NODE pt, old;
if (list == NULL || t ->w <list ->w){
t->next = list;
list = t;
} else {
old = list;
pt = old->next;
while(pt != NULL && pt->w < t->w){
old = pt;
pt = pt->next;
}
}
}
int main(void) {
int i;
NODE pt;
init();
for( i=1; i<N; i++){
pt = newnode();
pt->lc = least();
pt->rc = least();
pt->w = (pt->lc)->w + (pt->rc)->w;
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: