当前位置: 首页 > wzjs >正文

网站去公安局备案流程深圳市企业网站seo营销工具

网站去公安局备案流程,深圳市企业网站seo营销工具,南京 郑州网站建设公司 网络服务,asp网站制作工具一、引言 在计算机科学领域,树结构是一种非常重要的数据结构,尤其是二叉树,它在搜索、排序和存储数据方面有着广泛的应用。本文将详细介绍一个使用 C 实现的简单二叉树项目,该项目包含节点插入和数据打印功能。我们将从代码结构、…

一、引言

在计算机科学领域,树结构是一种非常重要的数据结构,尤其是二叉树,它在搜索、排序和存储数据方面有着广泛的应用。本文将详细介绍一个使用 C++ 实现的简单二叉树项目,该项目包含节点插入和数据打印功能。我们将从代码结构、核心功能实现到测试用例等方面进行深入分析。

二、项目概述

本项目主要由两个文件组成:main.cpp 和 tree.h。tree.h 文件定义了二叉树的节点结构和树类,包含节点的插入、数据打印和内存释放等功能;main.cpp 文件则是程序的入口,负责创建树对象并进行节点插入和数据打印操作。

三、代码结构分析

1. tree.h 文件

节点结构定义
template<class Data>
struct node
{Data data;node* parent;node* pLeft;node* pRight;node* brother;node();
};

这里定义了一个模板结构体 node,用于表示二叉树的节点。每个节点包含数据 data、父节点指针 parent、左右子节点指针 pLeft 和 pRight 以及兄弟节点指针 brother。构造函数 node() 用于初始化节点的数据和指针。

树类定义
template<class Data>
class tree
{node<Data>* pRoot;
public:tree();~tree();void insertChildren(Data d, int position = 1);void insertChildren(node<Data>* pos, Data d, int position = 1);node<Data>* pNode();void printData();
private:node<Data>* creatNode(Data d);bool isEndNode(node<Data>* p);void _printData(node<Data>* p);void _insertNode_L(node<Data>* root, Data data);void _insertNode_R(node<Data>* root, Data data);void _destroyData(node<Data>* curr_root);
};

tree 类是二叉树的核心类,包含根节点指针 pRoot。公有成员函数包括构造函数 tree()、析构函数 ~tree()、节点插入函数 insertChildren、获取根节点指针函数 pNode() 和数据打印函数 printData()。私有成员函数用于辅助实现节点创建、节点插入和内存释放等功能。

核心功能实现
  • 节点插入insertChildren 函数用于在指定位置插入新节点。根据 position 参数的值,可以选择将新节点插入到左子节点或右子节点位置。

template<class Data>
inline void tree<Data>::insertChildren(node<Data>* pos, Data d, int position)
{if (pos){if (position == 1){if (pos->pLeft)_insertNode_L(pos->pLeft, d);else{pos->pLeft = creatNode(d);if (SHOW_DEBUG_DATA == 1)printf("已向堆区%p申请空间存储数据\n", pos->pLeft);}}else if (position == 2){if (pos->pRight)_insertNode_R(pos->pRight, d);else{pos->pRight = creatNode(d);if (SHOW_DEBUG_DATA == 1)printf("已向堆区%p申请空间存储数据\n", pos->pRight);}}}
}

  • 数据打印printData 函数使用递归方式遍历二叉树,并打印每个节点的数据。

template<class Data>
void tree<Data>::printData()
{_printData(pRoot);
}template<class Data>
inline void tree<Data>::_printData(node<Data>* p)
{if (!isEndNode(p)){if (p->pLeft){_printData(p->pLeft);}if (p->pRight){_printData(p->pRight);}}printf("data:%d\n", p->data);
}

  • 内存释放:析构函数 ~tree() 调用 _destroyData 函数,使用递归方式释放二叉树中所有节点的内存。

cpp

运行

template<class Data>
inline tree<Data>::~tree()
{_destroyData(pRoot);
}template<class Data>
void tree<Data>::_destroyData(node<Data>* curr_root)
{if (!isEndNode(curr_root)){if (curr_root->pLeft){_destroyData(curr_root->pLeft);}if (curr_root->pRight){_destroyData(curr_root->pRight);}}if (SHOW_DEBUG_DATA == 1)printf("已析构%p\n", curr_root);delete curr_root;
}

2. main.cpp 文件

#include"tree.h"
#include<stdio.h>int main()
{tree<int> a;for (int i = 0; i < 200; i++){int r = i % 2 + 1;a.insertChildren(i, r);}a.printData();return 0;
}

main.cpp 文件是程序的入口,创建了一个 tree<int> 类型的对象 a,并使用 for 循环插入 200 个节点,最后调用 printData 函数打印二叉树中的所有数据。

四、测试与运行

将 main.cpp 和 tree.h 文件保存到同一目录下,使用 C++ 编译器进行编译和运行。运行程序后,将看到节点插入和数据打印的输出信息。

五、总结

通过这个简单的二叉树项目,我们学习了如何使用 C++ 实现二叉树的基本操作,包括节点插入、数据打印和内存释放。递归是实现这些操作的关键,它可以使代码更加简洁和易于理解。同时,我们也了解了模板类的使用,它可以提高代码的复用性。希望本文对大家理解二叉树的实现和应用有所帮助。

完整代码

tree.h

#pragma once
#include <cstddef>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//SHOE_DEBUG_DATA == 1时,显示调试输出信息
#define SHOW_DEBUG_DATA 1
//定义结点
template<class Data>
struct node
{Data data;node* parent;node* pLeft;node* pRight;node* brother;node();
};template<class Data>
class tree
{node<Data>* pRoot;
public:tree();~tree();/*在根节点插入数据* @param d 数据* @param position 位置* 1->左孩子位置* 2->右孩子位置* 默认为左孩子*/void insertChildren(Data d, int position = 1);/*在pos节点创建孩子* @param pos 节点位置* @param d 数据* @param position 位置* 1->左孩子位置* 2->右孩子位置* 默认为左孩子*/void insertChildren(node<Data>* pos,Data d, int position = 1);//获取pNodenode<Data>* pNode();void printData();
private://创建节点并返回新节点的指针node<Data>* creatNode(Data d);//判断是不是叶节点bool isEndNode(node<Data>* p);void _printData(node<Data>* p);void _insertNode_L(node<Data>* root, Data data);void _insertNode_R(node<Data>* root, Data data);void _destroyData(node<Data>* curr_root);//析构数据
};template<class Data>
node<Data>::node()//数据初始化
{memset(&data, 0, sizeof(Data));parent=NULL;pLeft=NULL;pRight = NULL;brother=NULL;
}template<class Data>
tree<Data>::tree()//创建节点
{pRoot = new node<Data>;if(SHOW_DEBUG_DATA==1)printf("已向堆区%p申请空间存储tree的根节点\n", pRoot);
}template<class Data>
inline tree<Data>::~tree()
{_destroyData(pRoot);
}template<class Data>
node<Data>* tree<Data>::creatNode(Data d)
{node<Data>* tempNode = new node<Data>;//创建节点 注意内存释放tempNode->data = d;return tempNode;//返回指针
}template<class Data>
inline void tree<Data>::insertChildren(node<Data>* pos, Data d, int position)
{if (pos)//当前根节点不为空{if (position == 1){if (pos->pLeft)_insertNode_L(pos->pLeft, d);else{pos->pLeft = creatNode(d);if (SHOW_DEBUG_DATA == 1)printf("已向堆区%p申请空间存储数据\n", pos->pLeft);}}else if (position == 2){if (pos->pRight)_insertNode_R(pos->pRight, d);else{pos->pRight = creatNode(d);if (SHOW_DEBUG_DATA == 1)printf("已向堆区%p申请空间存储数据\n", pos->pRight);}}}
}template<class Data>
node<Data>* tree<Data>::pNode()
{return pRoot;
}template<class Data>
void tree<Data>::printData()
{_printData(pRoot);
}template<class Data>
void tree<Data>::insertChildren(Data d, int position)
{if (pRoot)//当前根节点不为空{if (position == 1){if (pRoot->pLeft)_insertNode_L(pRoot->pLeft, d);else{pRoot->pLeft = creatNode(d);if (SHOW_DEBUG_DATA == 1)printf("已向堆区%p申请空间存储数据\n",pRoot->pLeft);}}else if (position == 2){if (pRoot->pRight)_insertNode_R(pRoot->pRight, d);else{pRoot->pRight = creatNode(d);if (SHOW_DEBUG_DATA == 1)printf("已向堆区%p申请空间存储数据\n", pRoot->pRight);}}}
}template<class Data>
inline bool tree<Data>::isEndNode(node<Data>* p)
{return !(p->pLeft || p->pRight);
}template<class Data>
inline void tree<Data>::_printData(node<Data>* p)
{if (!isEndNode(p))//针对有孩子的,需要递归{if (p->pLeft)//只要有孩子就不忙析构{_printData(p->pLeft);}if (p->pRight)//只要有孩子就不忙析构{_printData(p->pRight);//递归处理}}printf("data:%d\n", p->data);
}template<class Data>
void tree<Data>::_insertNode_L(node<Data>* root, Data data)
{if (root)//当前的节点有孩子{if (root->pLeft)_insertNode_L(root->pLeft, data);//左孩子有,则再找子孩子else//没有孩子,就直接加{root->pLeft = creatNode(data);if (SHOW_DEBUG_DATA == 1)printf("已向堆区%p申请空间存储数据\n", root->pLeft);}}else//没有孩子{root->pLeft = creatNode(data);}
}template<class Data>
void tree<Data>::_insertNode_R(node<Data>* root, Data data)
{if (root)//当前的节点有孩子{if (root->pRight)_insertNode_L(root->pRight, data);//左孩子有,则再找子孩子else//没有孩子,就直接加{root->pRight = creatNode(data);if (SHOW_DEBUG_DATA == 1)printf("已向堆区%p申请空间存储数据\n", root->pRight);}}else//没有孩子{root->pRight = creatNode(data);}
}template<class Data>
void tree<Data>::_destroyData(node<Data>* curr_root)
{if (!isEndNode(curr_root))//针对有孩子的,需要递归{if (curr_root->pLeft)//只要有孩子就不忙析构{_destroyData(curr_root->pLeft);}if (curr_root->pRight)//只要有孩子就不忙析构{_destroyData(curr_root->pRight);//递归处理}}if (SHOW_DEBUG_DATA == 1)printf("已析构%p\n", curr_root);delete curr_root;
}

以上就是关于这个二叉树项目的详细介绍,欢迎大家在评论区留言交流。

http://www.dtcms.com/wzjs/115836.html

相关文章:

  • 公司网站建设步骤seo网络优化是什么工作
  • 网站建设项目规划书社团宣传优化网站搜索
  • 天津网站制作计划鞍山做网站的公司
  • 建设机械网站制作最好的seo外包
  • 合肥网站建设xbuw网络营销学什么内容
  • 药品网站前置审批seo思维
  • 杭州房地产网站建设外国网站怎么进入
  • 长沙环路建设开发有限公司网站企业如何建站
  • 网站文章的作用重庆白云seo整站优化
  • 刘家窑做网站的公司seo排名点击工具
  • bootstrap 购物网站 导航菜单越秀seo搜索引擎优化
  • wordpress表情按钮搜索引擎优化论文3000字
  • 怎么自己免费做网站微信朋友圈广告投放代理
  • 白小姐生肖做特网站软件制作
  • 利用c 做网站关键词优化排名公司
  • 医疗网站被黑后可以做排名网络营销策划方案范文
  • 地产网站怎么做马鞍山网站seo
  • 深圳集团网站建设win10系统优化软件哪个好
  • 代理网站下载查权重
  • 建设局工程网站seo短视频网页入口引流下载
  • 网站制作教程书籍学校seo推广培训班
  • 网站建设开发公司哪家好百度seo关键词排名s
  • 专业做网站的公司网络营销概述ppt
  • 海口网站建设哪家好做网站的公司哪家好
  • b2b网站建设费用口碑营销例子
  • 网页设计网站制作收获手机如何制作一个网页链接
  • 济南网站建设方案服务上海最新发布
  • 网校网站模板谷歌seo关键词优化
  • 做视频网站要什么软件下载现在有哪些培训学校
  • php 建网站学seo哪个培训好