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

📄 二叉树的显示输出view.cpp

📁 是编译器我花了好几个礼拜才编好的确实难的这是个不错的编译器
💻 CPP
字号:
// 二叉树的显示输出View.cpp : implementation of the CMyView class
//

#include "stdafx.h"
#include "二叉树的显示输出.h"

#include "二叉树的显示输出Doc.h"
#include "二叉树的显示输出View.h"



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

extern CBinTree3P<char> theTree;

/////////////////////////////////////////////////////////////////////////////
// CMyView

IMPLEMENT_DYNCREATE(CMyView, CView)

BEGIN_MESSAGE_MAP(CMyView, CView)
	//{{AFX_MSG_MAP(CMyView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction

CMyView::CMyView()
{
	// TODO: add construction code here

}

CMyView::~CMyView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyView drawing

void CMyView::OnDraw(CDC* pDC)
{

	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	DrawTree(theTree,pDC);

}

/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers


void CMyView::DrawTree(CBinTree3P<char> & pTree,CDC* pDC){

	AnalyzeTree();
	if(pTree.IsEmpty()) {
		AfxMessageBox("CMyView::DrawTree !树为空!");
		return;
	}
	DrawTreNode(pTree.m_pRoot,m_nHorizoDista,m_nLeverDista,1,m_nHorizoDista/2,pDC);


}

void CMyView::DrawTreNode(CBinTreNode<char> *pNode,int x,int y,int nLeverNow,int nHoriDisNow, CDC* pDC){


	pDC->Ellipse(x - 10, y - 5, x + 20 , y + 20);
	char tem[2] = "X";
	tem[0] = pNode->m_MData;
	CString strNode = tem;
	pDC->TextOut(x,y,strNode);



	int nTemY = m_nLeverDista * (nLeverNow + 1);

	if(pNode->m_pLefC != NULL){
		pDC->MoveTo(x+3,y+20);
		pDC->LineTo(x - nHoriDisNow,nTemY-5);
		DrawTreNode(pNode->m_pLefC,x - nHoriDisNow,nTemY,nLeverNow+1,nHoriDisNow /2 ,pDC);
	}
	if(pNode->m_pRigC != NULL){
		pDC->MoveTo(x+3,y+20);
		pDC->LineTo(x + nHoriDisNow,nTemY-5);
		DrawTreNode(pNode->m_pRigC,x + nHoriDisNow,nTemY,nLeverNow+1,nHoriDisNow /2 ,pDC);
	}
	//未完成!!!


}

void CMyView::AnalyzeTree(){
	AnalyzeNode(theTree.m_pRoot,1);

    CRect rect;
    GetClientRect (&rect);
	
	m_nLeverDista = rect.Height() /(m_nTreeLever + 2);
	m_nHorizoDista = rect.Width() /2;


}

void CMyView::AnalyzeNode(CBinTreNode<char> *pNode,int nLeverNow){

	if(pNode->m_pLefC != NULL){
		if(m_nTreeLever < nLeverNow+1) m_nTreeLever = nLeverNow+1;
		AnalyzeNode(pNode->m_pLefC,nLeverNow+1);
	}

	if(pNode->m_pRigC != NULL){
		if(m_nTreeLever < nLeverNow+1) m_nTreeLever = nLeverNow+1;
		AnalyzeNode(pNode->m_pRigC,nLeverNow+1);
	}


}

⌨️ 快捷键说明

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