Exemple #1
0
// count the num of leaves
int CountLeaves(BiTree BT)
{
	int count = 0;
	if (!BT->lchild && !BT->rchild)
	{
		count++;
	}
	CountLeaves(BT->lchild);
	CountLeaves(BT->rchild);

	return count;
}
Exemple #2
0
static unsigned CountLeaves(const Tree &tree, unsigned uNodeIndex,
  unsigned LeavesUnderNode[])
	{
	if (tree.IsLeaf(uNodeIndex))
		{
		LeavesUnderNode[uNodeIndex] = 1;
		return 1;
		}

	const unsigned uLeft = tree.GetLeft(uNodeIndex);
	const unsigned uRight = tree.GetRight(uNodeIndex);
	const unsigned uRightCount = CountLeaves(tree, uRight, LeavesUnderNode);
	const unsigned uLeftCount = CountLeaves(tree, uLeft, LeavesUnderNode);
	const unsigned uCount = uRightCount + uLeftCount;
	LeavesUnderNode[uNodeIndex] = uCount;
	return uCount;
	}
int main()
{
	AvlTree TestTree = NULL;
	cout << "This is the test for Avl Tree." << endl;
	cout << "Notice:The print method is preorder traversal " << endl;
	cout << "First,insert some datas..." << endl;
	TestTree = Insert(9, TestTree);
	TestTree = Insert(5, TestTree);
	TestTree = Insert(10, TestTree);
	TestTree = Insert(0, TestTree);
	TestTree = Insert(6, TestTree);
	TestTree = Insert(11, TestTree);
	TestTree = Insert(-1, TestTree);
	TestTree = Insert(1, TestTree);
	TestTree = Insert(2, TestTree);
	PrintTree(TestTree);
	cout << "\nThe Min is " << Retrieve(FindMin(TestTree)) << endl;
	cout << "The Max is " << Retrieve(FindMax(TestTree)) << endl;
	cout << "Count Nodes: " << CountNodes(TestTree) << " Leaves: " 
		<< CountLeaves(TestTree) << " Full: " << CountFull(TestTree) << endl;
	cout << "Now delete 10(with one child node)..." << endl;
	TestTree = Delete(10, TestTree);
	PrintTree(TestTree);
	cout << "\nNow delete 11(with none child node)..." << endl;
	TestTree = Delete(11, TestTree);
	PrintTree(TestTree);
	cout << "\nNow delete 1(with two child node)..." << endl;
	TestTree = Delete(1, TestTree);
	PrintTree(TestTree);
	cout << "\nNow Print element between 5 and 10 in order " << endl;
	PrintKeyBetween(5, 10, TestTree);
	cout << "\nMake empty..." << endl;
	TestTree = MakeEmpty(TestTree);
	int Success = TestTree == NULL;
	cout << "Succeed? " << Success << endl;
	cout << "Now Generate the mininum tree with Height equal to 5" << endl;
	AvlTree MinH10 = MinAvlTree(5);
	PrintTree(MinH10);
	cout << "\nNow Generate the perfect tree with Height equal to 5" << endl;
	AvlTree PrefectH10 = PerfectAvlTree(5);
	PrintTree(PrefectH10);
	cout << "\nMake empty..." << endl;
	MinH10 = MakeEmpty(MinH10);
	PrefectH10 = MakeEmpty(PrefectH10);
	cout << "Good bye!" << endl;
	getchar();
}
Exemple #4
0
int VocabTree::WriteFlat(const char *filename) const
{
    if (m_root == NULL)
        return -1;

    FILE *f = fopen(filename, "w");
    
    if (f == NULL) {
        printf("[VocabTree::WriteFlat] Error opening file %s for writing\n",
               filename);
        return -1;
    }
    
    unsigned long num_leaves = CountLeaves();

    fprintf(f, "%lu %d\n", num_leaves, m_dim);
    m_root->WriteFlat(f, m_branch_factor, m_dim);

    fclose(f);

    return 0;
}
Exemple #5
0
void CalcClustalWWeights(const Tree &tree, WEIGHT Weights[])
	{
#if	TRACE
	Log("CalcClustalWWeights\n");
	tree.LogMe();
#endif

	const unsigned uLeafCount = tree.GetLeafCount();
	if (0 == uLeafCount)
		return;
	else if (1 == uLeafCount)
		{
		Weights[0] = (WEIGHT) 1.0;
		return;
		}
	else if (2 == uLeafCount)
		{
		Weights[0] = (WEIGHT) 0.5;
		Weights[1] = (WEIGHT) 0.5;
		return;
		}

	if (!tree.IsRooted())
		Quit("CalcClustalWWeights requires rooted tree");

	const unsigned uNodeCount = tree.GetNodeCount();
	unsigned *LeavesUnderNode = new unsigned[uNodeCount];
	memset(LeavesUnderNode, 0, uNodeCount*sizeof(unsigned));

	const unsigned uRootNodeIndex = tree.GetRootNodeIndex();
	unsigned uLeavesUnderRoot = CountLeaves(tree, uRootNodeIndex, LeavesUnderNode);
	if (uLeavesUnderRoot != uLeafCount)
		Quit("WeightsFromTreee: Internal error, root count %u %u",
		  uLeavesUnderRoot, uLeafCount);

#if	TRACE
	Log("Node  Leaves    Length  Strength\n");
	Log("----  ------  --------  --------\n");
	//    1234  123456  12345678  12345678
#endif

	double *Strengths = new double[uNodeCount];
	for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex)
		{
		if (tree.IsRoot(uNodeIndex))
			{
			Strengths[uNodeIndex] = 0.0;
			continue;
			}
		const unsigned uParent = tree.GetParent(uNodeIndex);
		const double dLength = tree.GetEdgeLength(uNodeIndex, uParent);
		const unsigned uLeaves = LeavesUnderNode[uNodeIndex];
		const double dStrength = dLength / (double) uLeaves;
		Strengths[uNodeIndex] = dStrength;
#if	TRACE
		Log("%4u  %6u  %8g  %8g\n", uNodeIndex, uLeaves, dLength, dStrength);
#endif
		}

#if	TRACE
	Log("\n");
	Log("                 Seq  Path..Weight\n");
	Log("--------------------  ------------\n");
#endif
	for (unsigned n = 0; n < uLeafCount; ++n)
		{
		const unsigned uLeafNodeIndex = tree.LeafIndexToNodeIndex(n);
#if	TRACE
		Log("%20.20s  %4u ", tree.GetLeafName(uLeafNodeIndex), uLeafNodeIndex);
#endif
		if (!tree.IsLeaf(uLeafNodeIndex))
			Quit("CalcClustalWWeights: leaf");

		double dWeight = 0;
		unsigned uNode = uLeafNodeIndex;
		while (!tree.IsRoot(uNode))
			{
			dWeight += Strengths[uNode];
			uNode = tree.GetParent(uNode);
#if	TRACE
			Log("->%u(%g)", uNode, Strengths[uNode]);
#endif
			}
		if (dWeight < 0.0001)
			{
#if	TRACE
			Log("zero->one");
#endif
			dWeight = 1.0;
			}
		Weights[n] = (WEIGHT) dWeight;
#if	TRACE
		Log(" = %g\n", dWeight);
#endif
		}

	delete[] Strengths;
	delete[] LeavesUnderNode;

	Normalize(Weights, uLeafCount);
	}