Beispiel #1
0
int main() {
    // load vector from file
    ifstream is("numbers_small.txt");
    istream_iterator<int> start(is), end;
    vector<int> x(start, end);
    cout << "Read " << x.size() << " numbers" << endl;

    // print the numbers to stdout
   cout << "numbers read in:\n";
    copy(x.begin(), x.end(),
    ostream_iterator<int>(cout, " "));
    cout << endl;cout << endl;

    CTree<int> tree;
    srand (time(0));
    bool result = 1;

	//cout<<"x.size(): "<<x.size()<<endl;
    for (int i=0; i<x.size(); i++){
        tree.Insert(x[i]);       // edit your insert code in Tree.h
    }

    tree.Preorder(cout);         // edit your preorder traversal code in Tree.h

    cout << endl;

    cout << tree.Depth() << endl;  // edit your depth code in Tree.h

    for (int i=0; i<x.size(); i++){
        if(i%2 == 0){
			//cout<<"i: "<<i<<endl;
            tree.Delete(i);      // edit your deletion code in Tree.h
			//tree.Preorder(cout);
        }
    }

    cout << tree.Depth() << endl;

    cout << endl;

    tree.Preorder(cout);        // edit your preorder traversal code in Tree.h

    cout << endl;

    return 0;
}
Beispiel #2
0
void TestTree()
{
	CTree t;
	const int cnSum = 1000000;
	clock_t tStart;
	clock_t tEnd;
	std::cout << "Test: " << cnSum << std::endl;
	tStart = clock();
	for(int i = 0;i < cnSum; i++)
	{
		int nValue = rand();
		char szName[32] = {0};
		sprintf(szName, "pktree%d", i);
		//cout << "szName: " << szName << ", "
		//	<< "nValue: " << nValue << endl;
		tagTreeNode *newp = t.NewNode(szName, nValue);
		//cout << "newp: " << newp << endl;
		t.Insert(newp);
	}
	tEnd = clock();
	std::cout << "cost time(s): " << (double)(tEnd-tStart)/CLOCKS_PER_SEC << std::endl;
	//
	//cout << "Result: " << endl;
	//t.PrintNode();
	// 递归查找
	int nFind = 0;
	int nNotFind = 0;
	char szName[32] = {0};
	srand((unsigned)time(NULL));	// 随机种子
	tStart = clock();
	for (int i = 0; i < cnSum; i++){
		int nRand = rand()%(10*cnSum);
		sprintf(szName, "pktree%d", nRand);
		tagTreeNode *pNode = t.LookUp(szName);
		if (pNode){
			nFind++;
			//std::cout << nRand << ", "
			//	<< pNode->name << ", " 
			//	<< pNode->value << std::endl;
		}
		else
			nNotFind++;
	}
	tEnd = clock();
	std::cout << "递归:" << std::endl
		<< "Find: " << nFind << std::endl
		<< "NotFind: " << nNotFind << std::endl
		<< "cost time(s): " << (double)(tEnd-tStart)/CLOCKS_PER_SEC << std::endl;
	
	// 循环查找
	nFind = 0;
	nNotFind = 0;
	tStart = clock();
	for (int i = 0; i < cnSum; i++){
		int nRand = rand()%(10*cnSum);
		sprintf(szName, "pktree%d", nRand);
		tagTreeNode *pNode = t.NrLookUp(szName);
		if (pNode){
			nFind++;
			//std::cout << nRand << ", "
			//	<< pNode->name << ", " 
			//	<< pNode->value << std::endl;
		}
		else
			nNotFind++;
	}
	tEnd = clock();
	std::cout << "循环:" << std::endl
		<< "Find: " << nFind << std::endl
		<< "NotFind: " << nNotFind << std::endl
		<< "cost time(s): " << (double)(tEnd-tStart)/CLOCKS_PER_SEC << std::endl;

	// 中序排序
	tStart = clock();
	t.ApplyInOrder();
	tEnd = clock();
	std::cout << "中序排序:" << std::endl
		<< "cost time(s): " << (double)(tEnd-tStart)/CLOCKS_PER_SEC << std::endl;

}