本文整理汇总了C++中Tree::GetNeighbor1方法的典型用法代码示例。如果您正苦于以下问题:C++ Tree::GetNeighbor1方法的具体用法?C++ Tree::GetNeighbor1怎么用?C++ Tree::GetNeighbor1使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tree
的用法示例。
在下文中一共展示了Tree::GetNeighbor1方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: RootByMidLongestSpan
static void RootByMidLongestSpan(const Tree &tree, EdgeInfo **EIs,
unsigned *ptruNode1, unsigned *ptruNode2,
double *ptrdLength1, double *ptrdLength2)
{
const unsigned uNodeCount = tree.GetNodeCount();
unsigned uLeaf1 = NULL_NEIGHBOR;
unsigned uMostDistantLeaf = NULL_NEIGHBOR;
double dMaxDist = -VERY_LARGE_DOUBLE;
for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex)
{
if (!tree.IsLeaf(uNodeIndex))
continue;
const unsigned uNode2 = tree.GetNeighbor1(uNodeIndex);
if (NULL_NEIGHBOR == uNode2)
Quit("RootByMidLongestSpan: internal error 0");
const double dEdgeLength = tree.GetEdgeLength(uNodeIndex, uNode2);
const EdgeInfo &EI = EIs[uNodeIndex][0];
if (!EI.m_bSet)
Quit("RootByMidLongestSpan: internal error 1");
if (EI.m_uNode1 != uNodeIndex || EI.m_uNode2 != uNode2)
Quit("RootByMidLongestSpan: internal error 2");
const double dSpanLength = dEdgeLength + EI.m_dMaxDistToLeaf;
if (dSpanLength > dMaxDist)
{
dMaxDist = dSpanLength;
uLeaf1 = uNodeIndex;
uMostDistantLeaf = EI.m_uMostDistantLeaf;
}
}
if (NULL_NEIGHBOR == uLeaf1)
Quit("RootByMidLongestSpan: internal error 3");
const double dTreeHeight = dMaxDist/2.0;
unsigned uNode1 = uLeaf1;
unsigned uNode2 = tree.GetNeighbor1(uLeaf1);
double dAccumSpanLength = 0;
#if TRACE
Log("RootByMidLongestSpan: span=%u", uLeaf1);
#endif
for (;;)
{
const double dEdgeLength = tree.GetEdgeLength(uNode1, uNode2);
#if TRACE
Log("->%u(%g;%g)", uNode2, dEdgeLength, dAccumSpanLength);
#endif
if (dAccumSpanLength + dEdgeLength >= dTreeHeight)
{
*ptruNode1 = uNode1;
*ptruNode2 = uNode2;
*ptrdLength1 = dTreeHeight - dAccumSpanLength;
*ptrdLength2 = dEdgeLength - *ptrdLength1;
#if TRACE
{
const EdgeInfo &EI = EIs[uLeaf1][0];
Log("...\n");
Log("Midpoint: Leaf1=%u Leaf2=%u Node1=%u Node2=%u Length1=%g Length2=%g\n",
uLeaf1, EI.m_uMostDistantLeaf, *ptruNode1, *ptruNode2, *ptrdLength1, *ptrdLength2);
}
#endif
return;
}
if (tree.IsLeaf(uNode2))
Quit("RootByMidLongestSpan: internal error 4");
dAccumSpanLength += dEdgeLength;
const unsigned uSub = tree.GetNeighborSubscript(uNode1, uNode2);
const EdgeInfo &EI = EIs[uNode1][uSub];
if (!EI.m_bSet)
Quit("RootByMidLongestSpan: internal error 5");
uNode1 = uNode2;
uNode2 = EI.m_uMaxStep;
}
}
示例2: FindRoot
void FindRoot(const Tree &tree, unsigned *ptruNode1, unsigned *ptruNode2,
double *ptrdLength1, double *ptrdLength2,
ROOT RootMethod)
{
#if TRACE
tree.LogMe();
#endif
if (tree.IsRooted())
Quit("FindRoot: tree already rooted");
const unsigned uNodeCount = tree.GetNodeCount();
const unsigned uLeafCount = tree.GetLeafCount();
if (uNodeCount < 2)
Quit("Root: don't support trees with < 2 edges");
EdgeInfo **EIs = new EdgeInfo *[uNodeCount];
for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex)
EIs[uNodeIndex] = new EdgeInfo[3];
EdgeList Edges;
for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex)
if (tree.IsLeaf(uNodeIndex))
{
unsigned uParent = tree.GetNeighbor1(uNodeIndex);
Edges.Add(uParent, uNodeIndex);
}
#if TRACE
Log("Edges: ");
Edges.LogMe();
#endif
// Main loop: iterate until all distances known
double dAllMaxDist = -1e20;
unsigned uMaxFrom = NULL_NEIGHBOR;
unsigned uMaxTo = NULL_NEIGHBOR;
for (;;)
{
EdgeList NextEdges;
#if TRACE
Log("\nTop of main loop\n");
Log("Edges: ");
Edges.LogMe();
Log("MDs:\n");
ListEIs(EIs, uNodeCount);
#endif
// For all edges
const unsigned uEdgeCount = Edges.GetCount();
if (0 == uEdgeCount)
break;
for (unsigned n = 0; n < uEdgeCount; ++n)
{
unsigned uNodeFrom;
unsigned uNodeTo;
Edges.GetEdge(n, &uNodeFrom, &uNodeTo);
CalcInfo(tree, uNodeFrom, uNodeTo, EIs);
#if TRACE
Log("Edge %u -> %u\n", uNodeFrom, uNodeTo);
#endif
const unsigned uNeighborCount = tree.GetNeighborCount(uNodeFrom);
for (unsigned i = 0; i < uNeighborCount; ++i)
{
const unsigned uNeighborIndex = tree.GetNeighbor(uNodeFrom, i);
if (!Known(tree, EIs, uNeighborIndex, uNodeFrom) &&
AllKnownOut(tree, EIs, uNeighborIndex, uNodeFrom))
NextEdges.Add(uNeighborIndex, uNodeFrom);
}
}
Edges.Copy(NextEdges);
}
#if TRACE
ListEIs(EIs, uNodeCount);
#endif
switch (RootMethod)
{
case ROOT_MidLongestSpan:
RootByMidLongestSpan(tree, EIs, ptruNode1, ptruNode2,
ptrdLength1, ptrdLength2);
break;
case ROOT_MinAvgLeafDist:
RootByMinAvgLeafDist(tree, EIs, ptruNode1, ptruNode2,
ptrdLength1, ptrdLength2);
break;
default:
Quit("Invalid RootMethod=%d", RootMethod);
}
for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex)
delete[] EIs[uNodeIndex];
delete[] EIs;
}