int value = 0;
int number = 0;
for(index = 0; index < length; index ++){
number = getVectexNumFromTree(pTree[index], start, end);
if(number > value)
value = number;
}
return (value == 2) 1 : 0;
} 线段的判断之后,如果发现在两颗独立的最小生成树上面,那么还需要进行合并操作,删除其中一个最小生成树,把另外一个生成树的所有点和线段都要添加到没有删除的这颗最小生成树上面。当然还有一点不要忘记了,最后还要加上端口分别在两棵树上的这个线段。
void mergeTwoMiniGenerateTree(MINI_GENERATE_TREE* pTree[], int length, int start, int end, int weight)
{
MINI_GENERATE_TREE* pFirst;
MINI_GENERATE_TREE* pSec;
DIR_LINE* line;
int index;
/* 删除多余的最小生成树*/
pFirst = find_tree_by_index(start);
pSec = find_tree_by_index(end);
delete_mini_tree_from_group(pTree, length, pSec);
/* 合并节点*/
for(index = 0; index < pSec->node_num; index ++){
pFirst->pNode[pFirst->node_num + index] = pSec->pNode[index];
}
pFirst->node_num += pSec->node_num;
/* 合并线段*/
for(line = pSec->pLine; line; line = line->next){
insert_line_into_queue(&pFirst->pLine, line->start, line->end, line->weight);
}
insert_line_into_queue(&pFirst->pLine, start, end, weight);
/* 函数返回*/
return;
}
void mergeTwoMiniGenerateTree(MINI_GENERATE_TREE* pTree[], int length, int start, int end, int weight)
{
MINI_GENERATE_TREE* pFirst;
MINI_GENERATE_TREE* pSec;
DIR_LINE* line;
int index;
/* 删除多余的最小生成树*/
pFirst = find_tree_by_index(start);
pSec = find_tree_by_index(end);
delete_mini_tree_from_group(pTree, length, pSec);
/* 合并节点*/
for(index = 0; index < pSec->node_num; index ++){
pFirst->pNode[pFirst->node_num + index] = pSec->pNode[index];
}
pFirst->node_num += pSec->node_num;
/* 合并线段*/
for(line = pSec->pLine; line; line = line->next){
insert_line_into_queue(&pFirst->pLine, line->start, line->end, line->weight);
}
insert_line_into_queue(&pFirst->pLine, start, end, weight);
/* 函数返回*/
return;
}
文章总结:
(1)本篇主要介绍了克鲁斯卡尔算法编写中需要处理的三个问题,排序、查找和合并
(2)复杂的函数都是由简单的函数构造而成的,我们可以把算法分成几个独立的部分,各个击破
(3)解决了这三个问题,下一篇博客就可以进行归总分析处理,逻辑上也十分清晰了