📄 rb_treeview.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
/////////////////////////////////////////////////////////////////////////////
// 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(30000)
{
// TODO: add construction code here
RBTree = new RedBlackTree;
//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* 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());
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);
char* key = InputDlg.m_Data.GetBuffer();
RedBlackTreeNode* newNode;
if ( ( newNode=RBTree->Search(key) ) )
{
IsSearch = true;
SearchValue = newNode->GetKey();
AfxMessageBox("八祸 己傍");
Invalidate(true);
UpdateWindow();
}
else
{
IsSearch = false;
AfxMessageBox("八祸 角菩");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -