主题:严蔚敏《数据结构(C语言版)》的算法6.12
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100 //HT 的最大容量
struct HTNode{
int weight;
int parent,lchild,rchild;
Node(int w,int p,int l,int r);
void print();
};
HTNode::Node(int w,int p,int l,int r)
{
HTNode::weight=w;
HTNode::parent=p;
HTNode::lchild=l;
HTNode::rchild=r;
}
void HTNode::print()
{
printf(" %2d %2d %2d %2d ",this->weight,this->parent,this->lchild,this->rchild);
}
//找出没被连接的权值最小的两个结点。
void Select(HTNode HT[],int j,int &s1,int &s2)
{
int count=0;
for(int i=1;i<=j; i++)
{
if(HT[i].parent==0)
{
if(count<1)
{
s1=i;
count++;
}
else
if(count<2&&count>0)
{
s2=i;
count++;
}
else
{
if((HT[i].weight<HT[s1].weight)&&(HT[s1].weight>=HT[s2].weight))
{
s1=i;
}
else
if((HT[i].weight<HT[s1].weight)&&(HT[s1].weight<HT[s2].weight))
{
s2=i;
}
else
if(HT[i].weight<HT[s2].weight)
{
s2=i;
}
}
}
}
}
#include<string.h>
#include<stdlib.h>
#define MAX 100 //HT 的最大容量
struct HTNode{
int weight;
int parent,lchild,rchild;
Node(int w,int p,int l,int r);
void print();
};
HTNode::Node(int w,int p,int l,int r)
{
HTNode::weight=w;
HTNode::parent=p;
HTNode::lchild=l;
HTNode::rchild=r;
}
void HTNode::print()
{
printf(" %2d %2d %2d %2d ",this->weight,this->parent,this->lchild,this->rchild);
}
//找出没被连接的权值最小的两个结点。
void Select(HTNode HT[],int j,int &s1,int &s2)
{
int count=0;
for(int i=1;i<=j; i++)
{
if(HT[i].parent==0)
{
if(count<1)
{
s1=i;
count++;
}
else
if(count<2&&count>0)
{
s2=i;
count++;
}
else
{
if((HT[i].weight<HT[s1].weight)&&(HT[s1].weight>=HT[s2].weight))
{
s1=i;
}
else
if((HT[i].weight<HT[s1].weight)&&(HT[s1].weight<HT[s2].weight))
{
s2=i;
}
else
if(HT[i].weight<HT[s2].weight)
{
s2=i;
}
}
}
}
}