📄 bitreedlg.cpp
字号:
// BiTreeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BiTree.h"
#include "BiTreeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
////////////////////////////////////////
template<class T>
void Stack<T>::Init_SeqStack(){
top =-1;
}
template<class T>
int Stack<T>::Empty_SeqStack(){
if(top ==-1)
return 1;
else
return 0;
}
template<class T>
int Stack<T>::Push_SeqStack(T x){
if(top==MAX)
return 0;
else
{
top++;
data [top]=x;
return 1;
}
}
template<class T>
int Stack<T>::Pop_SeqStack(T *x){
if(Empty_SeqStack())
return 0;
else
{
*x=data [top];
top --;
return 1;
}
}
///***********************************************************************
//按先序次序输入,构造二叉链表表示的二叉树T,# 表示空树
void CBiTreeDlg::CreateBiTree(BiTree &bt,CString &code)
{
char ch;
if(code.GetLength()==0)
{
bt=NULL;
return ;
}
ch=code[0];
if(ch=='#')
{
bt=NULL;
code=code.Right(code.GetLength()-1);
}
else
{
bt=(BiTree)malloc(sizeof(Bnode));
bt->data=ch;
code=code.Right(code.GetLength()-1);
CreateBiTree(bt->lchild,code);
CreateBiTree(bt->rchild,code);
}
}
//**************************************************************************
// 先序遍历
void CBiTreeDlg::PreOrderTraverse(BiTree bt){
if(bt){
m_b+=bt->data;
PreOrderTraverse(bt->lchild);
PreOrderTraverse(bt->rchild);
}
}
void CBiTreeDlg::CPreOrderTraverse(BiTree t){
Stack<BiTree> S;
Bnode *p=t;
S.Init_SeqStack();
while(p||!S.Empty_SeqStack())
{
if(p)
{
m_b+=p->data ;
S.Push_SeqStack(p);
p=p->lchild ;
}
else
{
S.Pop_SeqStack(&p);
p=p->rchild ;
}
}
}
//******************************************************************
//中序遍历
void CBiTreeDlg::InOrderTraverse(BiTree bt){
if(bt){
InOrderTraverse(bt->lchild);
m_c+=bt->data;
InOrderTraverse(bt->rchild);
}
}
void CBiTreeDlg::CInOrderTraverse(BiTree t){
Stack<BiTree> S;
Bnode *p=t;
S.Init_SeqStack();
while(p||!S.Empty_SeqStack())
{
if(p)
{
S.Push_SeqStack(p);
p=p->lchild ;
}
else
{
S.Pop_SeqStack(&p);
m_c+=p->data ;
p=p->rchild ;
}
}
}
//**************************************************************************
//后序遍历
void CBiTreeDlg::PostOrderTraverse(BiTree t){
if(t){
PostOrderTraverse(t->lchild );
PostOrderTraverse(t->rchild );
m_d+=t->data;
}
}
void CBiTreeDlg::CPostOrderTraverse(BiTree t){
Stack<DataType> S;
DataType Sq;
Bnode *p=t;
S.Init_SeqStack();
while(p||!S.Empty_SeqStack())
{
if(p)
{
Sq.flag=0;
Sq.node=p;
S.Push_SeqStack(Sq);
p=p->lchild ;
}
else
{
S.Pop_SeqStack(&Sq);
p=Sq.node;
if(Sq.flag==0)
{
Sq.flag=1;
S.Push_SeqStack(Sq);
p=p->rchild ;
}
else
{
m_d+=p->data ;
p=NULL;
}
}
}
}
//**************************************************************************
//层序遍历
void CBiTreeDlg::LevelOrderTraverse(BiTree t){
const int MaxLength=1024;
BiTree Q[MaxLength];
int front=0,rear=0;
BiTree p;
if(t){ //根结点入队
Q[rear]=t;
rear=(rear+1)%MaxLength;
}
while(front!=rear){
p=Q[front]; //队头元素出队
front=(front+1)%MaxLength;
m_e+=p->data;
if(p->lchild){ //左孩子不为空,入队
Q[rear]=p->lchild;
rear=(rear+1)%MaxLength;
}
if(p->rchild){ //右孩子不为空,入队
Q[rear]=p->rchild;
rear=(rear+1)%MaxLength;
}
}
}
//**************************************************************************
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBiTreeDlg dialog
CBiTreeDlg::CBiTreeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBiTreeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBiTreeDlg)
m_a = _T("ab##c##");
m_b = _T("");
m_c = _T("");
m_d = _T("");
m_e = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
root=NULL;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBiTreeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBiTreeDlg)
DDX_Text(pDX, IDC_EDIT1, m_a);
DDX_Text(pDX, IDC_EDIT2, m_b);
DDX_Text(pDX, IDC_EDIT3, m_c);
DDX_Text(pDX, IDC_EDIT4, m_d);
DDX_Text(pDX, IDC_EDIT5, m_e);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBiTreeDlg, CDialog)
//{{AFX_MSG_MAP(CBiTreeDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBiTreeDlg message handlers
BOOL CBiTreeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CBiTreeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CBiTreeDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CBiTreeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBiTreeDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString temp;
temp=m_a;
if(m_a.IsEmpty())
::AfxMessageBox("请输入建树所需要的先序序列!");
else
CreateBiTree(root,temp);
}
/*void CBiTreeDlg::OnButton10()
{
// TODO: Add your control notification handler code here
m_e.Empty();
CLevelOrderTraverse(root);
UpdateData(FALSE);
}*/
void CBiTreeDlg::OnButton2()
{
// TODO: Add your control notification handler code here
m_b.Empty();
m_c.Empty();
m_d.Empty();
m_e.Empty ();
UpdateData(FALSE);
}
void CBiTreeDlg::OnButton3()
{
// TODO: Add your control notification handler code here
m_b.Empty();
PreOrderTraverse(root);
UpdateData(FALSE);
}
void CBiTreeDlg::OnButton4()
{
// TODO: Add your control notification handler code here
m_c.Empty();
InOrderTraverse(root);
UpdateData(FALSE);
}
void CBiTreeDlg::OnButton5()
{
// TODO: Add your control notification handler code here
m_d.Empty();
PostOrderTraverse(root);
UpdateData(FALSE);
}
void CBiTreeDlg::OnButton6()
{
// TODO: Add your control notification handler code here
m_e.Empty();
LevelOrderTraverse(root);
UpdateData(FALSE);
}
void CBiTreeDlg::OnButton7()
{
// TODO: Add your control notification handler code here
m_b.Empty();
CPreOrderTraverse(root);
UpdateData(FALSE);
}
void CBiTreeDlg::OnButton8()
{
// TODO: Add your control notification handler code here
m_c.Empty();
CInOrderTraverse(root);
UpdateData(FALSE);
}
void CBiTreeDlg::OnButton9()
{
// TODO: Add your control notification handler code here
m_d.Empty();
CPostOrderTraverse(root);
UpdateData(FALSE);
}
void CBiTreeDlg::OnButton11()
{
// TODO: Add your control notification handler code here
CBiTreeDlg::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -