资源简介
分层建立一个随机层数及子节点的多叉树,并分层遍历输出,在都是奇数节点位置插入偶数节点,输出格式为树状
代码片段和文件信息
#include “StdAfx.h“
#include “MyTree.h“
#define MAX_CENG_SIZE 10 //允许最大层数
#define MAX_NODE_SIZE 3 //允许最大节点数
#define MAX_BUF_SIZE 20 //允许申请最大内存空间
#define MAX_VALUE_SIZE 1000 //允许节点值的最大值
CMyTree::CMyTree(void)
{
m_pTreeRoot = NULL;
}
CMyTree::~CMyTree(void)
{
DeleteTree();
}
int CMyTree::DeleteTree()
{
if (!m_pTreeRoot)
return 0;
/////////////////////
//应用队列遍历多叉树依次删除
queue queueTree;
queueTree.push(m_pTreeRoot);
CTreeNode* pNode = NULL;
while (!queueTree.empty())
{
pNode = queueTree.front();
queueTree.pop();
if (pNode->IsLeaf()) //删除叶子节点
{
if (!pNode->IsRoot())
{
CTreeNode* pNodeParents = pNode->GetParents();
pNodeParents->DeleteChildrenNode(pNode); //首先断开和父节点的链接
}
delete pNode;
continue;
}
list* treeNodeList = pNode->GetChildrenList();
list::iterator ite;
for (ite = treeNodeList->begin(); ite != treeNodeList->end(); ite++)
{
queueTree.push(*ite);
}
}
m_pTreeRoot = NULL;
return 0;
}
int CMyTree::CreatTree()
{
if (m_pTreeRoot)
{
DeleteTree();
}
m_pTreeRoot = new CTreeNode();
/////////////////////////////
//应用2个队列分层创建树
queue queueTree1;
queue queueTree2;
queueTree1.push(m_pTreeRoot);
srand((unsigned)time(NULL));
int nValue = rand()%MAX_VALUE_SIZE;
int nBufSize = rand()%MAX_BUF_SIZE;
/////////////////////////////
//根节点设置数值及申请内存
m_pTreeRoot->SetNodeValue(nValue);
m_pTreeRoot->SetBufSize(nBufSize);
int nHeight = rand()%MAX_CENG_SIZE; //层数
for (int i = 0; i < nHeight-1; i++)
{
queue *pTempQueuePush = NULL;
queue *pTempQueuePop = NULL;
if (0 == i%2)
{
pTempQueuePop = &queueTree1;
pTempQueuePush = &queueTree2;
}
else
{
pTempQueuePop = &queueTree2;
pTempQueuePush = &queueTree1;
}
while (!pTempQueuePop->empty())
{
CTreeNode* pTreeNode = pTempQueuePop->front();
pTempQueuePop->pop();
/////////////////////////////
//添加子节点
int nChildrenNum = rand()%MAX_NODE_SIZE;
nChildrenNum = nChildrenNum == 0 ? 1 : nChildrenNum;
for (int j = 0; j < nChildrenNum; j++)
{
CTreeNode* pChildrenNode = new CTreeNode();
int nValue = rand()%MAX_VALUE_SIZE;
int nBufSize = rand()%MAX_BUF_SIZE;
/////////////////////////////
//设置数值及申请内存
pChildrenNode->SetNodeValue(nValue);
pChildrenNode->SetBufSize(nBufSize);
pTreeNode->InsertChildrenNode(pChildrenNode);
pTempQueuePush->push(pChildrenNode); //记录当前层的节点
}
}
}
return 0;
}
int CMyTree::PrintTree()
{
if (!m_pTreeRoot)
return 0;
stack stackTree; // 保存树节点栈
map mapCengSize; //记录栈中每层的节点数
int nCengNum = 0; //记录当前层数
mapCengSize.insert(map::value_type(0 1));
stackTree.push(m_pTreeRoot);
while(!stackTree.empty())
{
CTreeNode* pNode = stackTree.top();
stackTree.pop();
map
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 7888 2011-08-24 13:03 myTreeApp\myTreeApp\Debug\BuildLog.htm
文件 5732 2011-08-18 10:14 myTreeApp\myTreeApp\MyTree.cpp
文件 452 2011-08-18 10:14 myTreeApp\myTreeApp\MyTree.h
文件 285 2011-08-24 13:10 myTreeApp\myTreeApp\myTreeApp.cpp
文件 4759 2011-08-17 17:19 myTreeApp\myTreeApp\myTreeApp.vcproj
文件 1407 2011-08-24 13:10 myTreeApp\myTreeApp\myTreeApp.vcproj.RZ-PC.RZ.user
文件 1427 2011-08-17 22:15 myTreeApp\myTreeApp\myTreeApp.vcproj.WWW-C6221ACAD07.Administrator.user
文件 6023 2011-08-18 10:18 myTreeApp\myTreeApp\myTree_cpp.txt
文件 533 2011-08-18 10:18 myTreeApp\myTreeApp\myTree_h.txt
文件 1318 2011-08-17 17:14 myTreeApp\myTreeApp\ReadMe.txt
文件 296 2011-08-17 17:14 myTreeApp\myTreeApp\stdafx.cpp
文件 376 2011-08-17 17:14 myTreeApp\myTreeApp\stdafx.h
文件 1499 2011-08-17 17:17 myTreeApp\myTreeApp\TreeNode.cpp
文件 1634 2011-08-18 10:06 myTreeApp\myTreeApp\TreeNode.h
文件 1517 2011-08-18 10:18 myTreeApp\myTreeApp\TreeNode_cpp.txt
文件 1779 2011-08-18 10:18 myTreeApp\myTreeApp\TreeNode_h.txt
文件 892 2011-08-17 17:14 myTreeApp\myTreeApp.sln
..A..H. 17920 2011-08-24 13:10 myTreeApp\myTreeApp.suo
目录 0 2011-08-24 13:10 myTreeApp\myTreeApp\Debug
目录 0 2011-08-24 13:10 myTreeApp\myTreeApp
目录 0 2011-08-24 13:11 myTreeApp
----------- --------- ---------- ----- ----
55737 21
相关资源
- 支持向量机libsvm-2.88(最新版
- 简单多边形的屋脊线计算算法
- 十字链表的定义及实现
- 基于QT的P2P共享文件系统
- Qt 获取exe版本名称,描述等详细信息
- tcp协议简单的聊天程序Console
- VS2010求值MD5的代码
- A*全局路径规划器ros
- SWMM51014代码编译及扩展案例182387
- conio.h头文件
- 栈溢出攻击技术
- Qt5主窗体:文本编辑器
- CMake 完整入门教程
- RAD Studio 10.3.1 4899 keygen
- 英文字典 中国地质大学数据结构A上机
- 南京邮电大学实验一真值表法求主析
- 利用栈求表达式的值
- LINUX 下的超声波驱动
- .NET全套课程
- 五子棋 Linux make
- 基于单片机的正弦波设计程序幅度和
- 数学建模2019美赛D题 逃离卢浮宫 代码
- 操作系统实验报告-动态存储管理
- MODBUS协议 vc实现
- CodeGear RAD Studio 2007-2009 破解补丁
- windows.h
- Windows下使用NIF扩展Erlang完整
- 仿射密码加密解密算法.docx
- tensorflow1.12.0+gpucuda 9.0
- MinGW免安装软件
评论
共有 条评论