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

📄 rb_treeview.cpp

📁 Soul的源代码,类似于劲舞团之类的游戏
💻 CPP
字号:
// RB_TreeView.cpp : implementation of the CRB_TreeView class
//

#include "stdafx.h"
#include "RB_Tree.h"

#include "RB_TreeDoc.h"
#include "RB_TreeView.h"

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

#include "Mmsystem.h"

/////////////////////////////////////////////////////////////////////////////
// CRB_TreeView

IMPLEMENT_DYNCREATE(CRB_TreeView, CView)

BEGIN_MESSAGE_MAP(CRB_TreeView, CView)
	//{{AFX_MSG_MAP(CRB_TreeView)
	ON_COMMAND(ID_INSERT, OnInsert)
	ON_COMMAND(ID_DELETE, OnDelete)
	ON_COMMAND(ID_SEARCH, OnSearch)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRB_TreeView construction/destruction

CRB_TreeView::CRB_TreeView()
: MemPool(270000)
{
	// TODO: add construction code here
	RBTree = new RedBlackTree<RedBlackTreeNode>;
	//MemPool = new CMMemPooler<RedBlackTreeNode>(30000);
//	MemPool[4]->storedEntry.key;
	IsSearch = false;
}

CRB_TreeView::~CRB_TreeView()
{
	delete RBTree;
//	delete MemPool;
}

BOOL CRB_TreeView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CRB_TreeView drawing

void CRB_TreeView::OnDraw(CDC* pDC)
{
	CRB_TreeDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	TreeDraw(RBTree);
}

/////////////////////////////////////////////////////////////////////////////
// CRB_TreeView printing

BOOL CRB_TreeView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CRB_TreeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CRB_TreeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CRB_TreeView diagnostics

#ifdef _DEBUG
void CRB_TreeView::AssertValid() const
{
	CView::AssertValid();
}

void CRB_TreeView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CRB_TreeDoc* CRB_TreeView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRB_TreeDoc)));
	return (CRB_TreeDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CRB_TreeView message handlers
void CRB_TreeView::TreeDraw(RedBlackTree<RedBlackTreeNode>* bt)
{
	if(bt->GetRoot()->GetLeftChild() != bt->GetNil())
		TreeOnDraw(bt->GetRoot()->GetLeftChild(), bt->GetNil(), 0, 1);
}

void CRB_TreeView::TreeOnDraw(RedBlackTreeNode *root, RedBlackTreeNode *nil, int width, int depth)
{
	CClientDC cdc(this);
	

	if(root)
	{
		CString temp;
			
		temp.Format("%s",root->GetKey());
		
		CBrush Black_Brush;
		CBrush Red_Brush;
		CBrush Found_Brush;

		Black_Brush.CreateSolidBrush(0x00000000);
		Red_Brush.CreateSolidBrush(0x000000FF);
		Found_Brush.CreateSolidBrush(0x00FFFFFF);

		if( (IsSearch == true) && ( _stricmp( SearchValue, root->GetKey() ) == 0 ) )
		{
			cdc.SelectObject(Found_Brush);
			IsSearch = false;
		}

		else
		{
			if(root->GetColor() == 0)
				cdc.SelectObject(Black_Brush);
			else
				cdc.SelectObject(Red_Brush);
		}
			
		cdc.Ellipse(cdc.GetDeviceCaps(HORZRES)/2 + width -10, 20+depth*90-10, cdc.GetDeviceCaps(HORZRES)/2+width+20, 20+depth*90+20);
		cdc.TextOut(cdc.GetDeviceCaps(HORZRES)/2 + width, 20+depth*90, temp);

		if(root->GetLeftChild()->GetKey() != NULL && root->GetLeftChild() != nil)
		{
			cdc.MoveTo(cdc.GetDeviceCaps(HORZRES)/2 + width-10, 20+depth*90+10);
			cdc.LineTo(cdc.GetDeviceCaps(HORZRES)/2 + (width-250+(depth+1)*37), 20+(depth+1)*90);
		}
		if(root->GetRightChild()->GetKey() != NULL && root->GetRightChild() != nil)
		{
			cdc.MoveTo(cdc.GetDeviceCaps(HORZRES)/2 + width+18, 20+depth*90+10);
			cdc.LineTo(cdc.GetDeviceCaps(HORZRES)/2 + (width+250-(depth+1)*37), 20+(depth+1)*90-8);
		}

		if(root->GetLeftChild()->GetKey() != NULL && root->GetLeftChild() != nil)
			TreeOnDraw(root->GetLeftChild(), nil,  width-250+depth*37, ++depth);
		else
			++depth;

		if(root->GetRightChild()->GetKey() != NULL && root->GetRightChild() != nil)
			TreeOnDraw(root->GetRightChild(), nil, width+250-depth*37, depth--);
		else
			depth--;

	}
}

void CRB_TreeView::OnInsert() 
{
	// TODO: Add your command handler code here
	InputDlg.DoModal();
	int iIndex;
	UpdateData(true);
//	char* key  = new char[20];
	//	strcpy(key, InputDlg.m_Data.GetBuffer());
	char cId[256];
	DWORD dwTime;
    dwTime = timeGetTime();

	//for(int i = 0;i< 260000;i++)
	//{
	//	sprintf(cId,"%08d",i);
	//	RedBlackTreeNode* x;
	//	x = MemPool.Alloc(&iIndex);
	//	//x = new RedBlackEntry(key);

	//	x->SetKey(cId);
	//	/*RedBlackTreeNode* newNode;*/
	//	//if ( !( newNode=RBTree->Search(key) ) )
	//	if(	RBTree->Insert(x) == NULL)
	//		AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	//}


	for(int i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","a",i);
		RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	}

	for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","b",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	}

	for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","j",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	}

	for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","k",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	}

	for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","c",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	}

	for( i = 0 ; i < 20000;i++)
	{ 
		::sprintf(cId,"%s%019d","d",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	}
	
	for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","e",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	}

	for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","g",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");
	}	

	for( i = 0 ; i < 20000;i++)

		{
		::sprintf(cId,"%s%019d","f",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");

	}

		for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","h",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");

	}
		for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","i",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");

	}
		for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","l",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");

	}
		for( i = 0 ; i < 20000;i++)
	{
		::sprintf(cId,"%s%019d","m",i);
				RedBlackTreeNode* x;
		x = MemPool.Alloc(&iIndex);
		//x = new RedBlackEntry(key);

		x->SetKey(cId);
		/*RedBlackTreeNode* newNode;*/
		//if ( !( newNode=RBTree->Search(key) ) )
		if(	RBTree->Insert(x) == NULL)
			AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");

	}



	int itime = timeGetTime() - dwTime;
	char Tmp[256];
	sprintf(Tmp,"单捞鸥 涝仿 矫埃 :: %d \n",itime );
	AfxMessageBox(Tmp);
	//RedBlackTreeNode* x;
	//x = MemPool.Alloc(&iIndex);
	//x = new RedBlackEntry(key);

	//x->SetKey(InputDlg.m_Data.GetBuffer());
	///*RedBlackTreeNode* newNode;*/
	//if ( !( newNode=RBTree->Search(key) ) )
	//if(	RBTree->Insert(x) == NULL)
	//	AfxMessageBox("捞固 粮犁窍绰 蔼涝聪促.");

	MemPool[iIndex];

	Invalidate(true);
	UpdateWindow();
}

void CRB_TreeView::OnDelete() 
{
	// TODO: Add your command handler code here
	InputDlg.DoModal();
	UpdateData(true);
	char* key = InputDlg.m_Data.GetBuffer();
	RedBlackTreeNode* newNode;
	if ( ( newNode=RBTree->Search(key) ) )
	{
		RBTree->DeleteNode(newNode);/*assignment*/
		MemPool.Free(newNode);
	}
	else
		AfxMessageBox("粮犁窍瘤 臼绰 蔼涝聪促.");
	

	Invalidate(true);
	UpdateWindow();
}

void CRB_TreeView::OnSearch() 
{
	// TODO: Add your command handler code here
	InputDlg.DoModal();
//	UpdateData(true);
	int iCnt = 0;
	DWORD dwTime;

	char* key = InputDlg.m_Data.GetBuffer();

	dwTime = timeGetTime();
	while(timeGetTime() - dwTime  < 1000)
	{
		iCnt++;

		RedBlackTreeNode* newNode;
		if ( ( newNode=RBTree->Search(key) ) )
		{
			IsSearch = true;
			SearchValue = newNode->GetKey();
			//AfxMessageBox("八祸 己傍");
			//Invalidate(true);
			//UpdateWindow();
		}
		else
		{
			IsSearch = false;
			AfxMessageBox("八祸 角菩");
		}
		//	p = tmap.find("h0000003")->second;
		//printf("%d",p);
		//		iter = tmap.find( findStr );
		//		p = iter -> second;

		//		sprintf(ttt,"%06d p:%d\n", iCnt, p);
		//		OutputDebugString(ttt);
	}


	float ftime = (float) (timeGetTime() - dwTime);
	char tmp[256];
	sprintf(tmp,"time :: %d  Cnt :: %d 乞闭 :: %f",timeGetTime() - dwTime,iCnt,(ftime /1000)/
(float)iCnt );
	AfxMessageBox(tmp);


}

⌨️ 快捷键说明

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