⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bintreedemo.cpp

📁 这是一个有关哈夫曼编/译码器的课程设计
💻 CPP
字号:
// BinTreeDemo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "StringIntTree.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

// A tree-callback function that simply prints out some crap
void callback_PrintStuff(CBinTreeNode* p,void* pParam)
{
  CStringIntTreeNode* pNode = (CStringIntTreeNode*) p;

  cout << pNode->GetString() << ", " << pNode->GetInt() << "\n";
}


// A tree-callback function that runs a search for the element
// also illustrates a use for the pParam thingie.
void callback_Find(CBinTreeNode* p,void* pParam)
{
	CStringIntTree* pTree = (CStringIntTree*)pParam;
	CStringIntTreeNode* pNode = (CStringIntTreeNode*) p;    
	
	CStringIntTreeNode* pNodeFound;
	pNodeFound = pTree->FindByString(pNode->GetString());
	if (pNodeFound) 
	{
		cout << pNodeFound->GetString() 
			 << " found after " 
			 << pTree->GetComparisons() 
			 << " comparisons.\n";
	}
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// Standard autogenereated stuff...
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		CStringIntTree tree;

		// Adding some elements, the ints are just random stuff without any real meaning.
		tree.Add("AC/DC",44);
		tree.Add("Wierd Al",-4);
		tree.Add("Metallica",56);
		tree.Add("Kiss",2);
		tree.Add("Therapy?",45);
		tree.Add("Black Sabbath",666);
		tree.Add("Iron Maiden",432);
		tree.Add("Pink Floyd",101);
		tree.Add("B52's",999);
		tree.Add("Eric Clapton",-243);
		tree.Add("Guano Apes",12);
		tree.Add("Hoodo Gurus",22);
		tree.Add("Motorhead",0);
		tree.Add("U2",93);
		tree.Add("W.A.S.P.",0);

		cout << "Traverse(Descending):\n";
		cout << "---------------------\n";

		// Print out the tree in reversed order for illustration
		tree.Traverse(CBinTree::Descending,callback_PrintStuff,NULL);

		cout << "Press enter...";cin.get();

		cout << "---------------------\n";

        // Go through all elements and make a search for each element
		// and print out how tough it was to find.		
		tree.Traverse(CBinTree::Ascending,callback_Find,&tree);

		cout << "---------------------\n";
		cout << "Tree's height: " << tree.GetHeight() << "\n";
		cout << "[Balancing tree]\n";
		// Guess what this one does...
		tree.Balance();
		cout << "Tree's height: " << tree.GetHeight() << "\n";
		cout << "---------------------\n";


		// Do the find thingie again, but on a balanced tree...
		tree.Traverse(CBinTree::Ascending,callback_Find,&tree);
		cout << "Press enter...";cin.get();
		tree.DeleteByString("Guano Apes");
		tree.DeleteByString("Kiss");
		cout << "Tree after deleting some nodes:\n";
		cout << "-------------------------------\n";
		tree.Traverse(CBinTree::Ascending,callback_PrintStuff,&tree);
	
		cout << "Press enter to exit...";cin.get();
	}

	return nRetCode;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -