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

📄 bt1033.cpp

📁 一个构建、显示和判断完全二叉树的小程序。
💻 CPP
字号:
// BT1033.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "BT1033.h"
#include "BinaryTree.h"
#include "BTControl.h"

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

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

CWinApp theApp;
CBinaryTree *head = 0; //定义头节点指针
CBTControl BTC; 

using namespace std;

void ShowBTFrame();
void InsertNode();
void DeletNode();
void JudgeBT();
bool SetOperate();
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// ===============系统自动注释部份=================
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	// ===============自动注释部份结束=================
	else
	{
		head = new CBinaryTree; // 生成头点

		head->NextNode = new CBinaryTree; // 生成根节点
		head->NextNode->FBTserial = 1;
		while (SetOperate()!=0); //如果操作数不为零则继续操作
	}

	BTC.FreeMemory(head); //删除树结构
	free(head); //删除头节点
	return nRetCode;
}


bool SetOperate() //菜单操作
{
	char op;
	char tmp;

	cout << endl << endl;
	cout << "请选择你需要的操作:" << endl;
	cout << " 1. 显示二叉树结构" << endl;
	cout << " 2. 插入节点"<< endl;
	cout << " 3. 删除节点"<< endl;
	cout << " 4. 完全二叉树判断"<<endl;
	cout << " 0. 退出操作"<<endl<<endl;
	cout << "选择:";
	cin >> op; //除去多余的输入
	do{tmp=getchar();}while(tmp!=10 || tmp>122);

	switch(op) //判断操作.调用函数操作
	{
		case '1':
			ShowBTFrame(); 
			break;
		case '2':
			InsertNode();
			break;
		case '3':
			DeletNode();
			break;
		case '4':
			JudgeBT();
			break;
		case '0':
			return 0;
		default:
			cout << "操作数错误!" << endl;
	}
return 1;
}

void ShowBTFrame()
{
	cout << endl << endl;
	BTC.ShowBTList(head);
}

void InsertNode()
{
	int ip =0; //插入点
	bool lr = false; //左右孩子

	cout << "设置插入点:" ;
	cin >> ip;
	cout << endl << "孩子类型(0:左 1:右):";
	cin >> lr;
	switch (BTC.CHILD(head,lr,ip)) //判断待插入节点状态
	{
		case -1:
			cout << endl << "插入点不存在!";
			break;
		case 0:
			if (BTC.INS_CHILD(head,lr,ip)) //插入节点
				cout << endl << "插入成功!";
			else
				cout << endl << "插入失败!";
			break;
		default:
			cout << endl << "孩子已经存在!";
			break;
	}
}

void DeletNode()
{
	int ip =0; //删除点

	cout << "设置删除点:" ;
	cin >> ip;

	//判断待删除节点状态
	if (ip<2) {cout << endl << "不能删除根节点!";return;}
	if (BTC.CHILD(head,0,ip)>0) {cout << endl << "右孩子存在!";return;}
	if (BTC.CHILD(head,1,ip)>0) {cout << endl << "右孩子存在!";return;}
	if (BTC.CHILD(head,1,ip)==-1) {cout << endl << "节点不存在!";return;}

	//删除节点
	if (BTC.DeletPoint(head,ip))
		cout << endl << "删除成功!";
	else
		cout << endl << "删除失败!";
}

void JudgeBT()
{
	if (BTC.JudgeBT(head))
		cout << endl << "该树是完全二叉树!";
	else
		cout << endl << "该树不是完全二叉树!";
}

⌨️ 快捷键说明

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