1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include "muscle.h"
#include "tree.h"
/***
Simple tree drawing algorithm.
y coordinate of node is index in depth-first traversal.
x coordinate is distance from root.
***/
static unsigned DistFromRoot(const Tree &tree, unsigned uNodeIndex)
{
const unsigned uRoot = tree.GetRootNodeIndex();
unsigned uDist = 0;
while (uNodeIndex != uRoot)
{
++uDist;
uNodeIndex = tree.GetParent(uNodeIndex);
}
return uDist;
}
static void DrawNode(const Tree &tree, unsigned uNodeIndex)
{
if (!tree.IsLeaf(uNodeIndex))
DrawNode(tree, tree.GetLeft(uNodeIndex));
unsigned uDist = DistFromRoot(tree, uNodeIndex);
for (unsigned i = 0; i < 5*uDist; ++i)
Log(" ");
Log("%d\n", uNodeIndex);
if (!tree.IsLeaf(uNodeIndex))
DrawNode(tree, tree.GetRight(uNodeIndex));
}
void DrawTree(const Tree &tree)
{
unsigned uRoot = tree.GetRootNodeIndex();
DrawNode(tree, uRoot);
}
|