​树形结构是一种重要的非线性数据结构。其中树和二叉树最为常用,直观看来树是以分支关系定义的层次结构。树形结构是我们平时比较熟悉的,比如文件夹目录、公司组织关系等。在计算机领域也得到广泛的应用,编译程序就是以树来表示源程序的语法结构。

二叉树是一种特殊的树形结构,他的特点是每个节点至多只有两颗子树,并且,子树有左右之分,顺序不能颠倒。

树形结构里边还有很多的知识点,我不在这里做文字上的解释,这里只是针对二叉树的相关特点,用C++分别实现基于数组和基于链表两种方式的二叉树,以及实现二叉树的三种遍历方式。
基于数组的二叉树基本实现:

#ifndef _CTREE_H_
#define _CTREE_H_
#include<iostream>using namespace std;
//基于数组的二叉树基本实现
class Tree
{public:explicit Tree(int size, int* pRoot){m_iSize = size;m_pTree = new int[m_iSize];memset(m_pTree, 0, sizeof(int)*m_iSize);m_pTree[0] = *pRoot;}~Tree(){delete[]m_pTree;m_pTree = NULL;}int* SearchNode(int index){if (index<0 && index>m_iSize){return NULL;}return &m_pTree[index];}//direction 1:left 2:rightbool AddNode(int index, int direction, int* pNode){if (index<0 && index>m_iSize){return false;}if (m_pTree[index] == 0){return false;}if (index * 2 + direction > m_iSize){return false;}m_pTree[index * 2 + direction] = *pNode;return true;}bool DeleteNode(int index, int* pNode){if (index<0 && index>m_iSize){return false;}*pNode = m_pTree[index];m_pTree[index] = 0;return true;}void Traverse(){for (int i = 0; i < m_iSize; i++){cout << m_pTree[i] << " ";}cout << endl;}
private:int* m_pTree;int m_iSize;
};
#endif // !_CTREE_H_

基于链表的二叉树实现:

class TreeNode
{
public:int index; //坐标char data; //数据TreeNode *pLChild; //左节点TreeNode *pRChild; //右节点TreeNode *pParent; //父节点
public:TreeNode() : index(0), data('0'), pLChild(NULL), pRChild(NULL), pParent(NULL) {}TreeNode(int iNodeIndex, char val) : index(iNodeIndex), data(val), pLChild(NULL), pRChild(NULL), pParent(NULL) {}~TreeNode(){//if (pLChild)//{//   delete pLChild;//   pLChild = NULL;//}//if (pRChild)//{//  delete pRChild;//   pRChild = NULL;//}//if (pParent)//{//  delete pParent;//   pParent = NULL;//}}//TreeNode(const TreeNode& node)//{//   index = node.index;//  data = node.data;//    delete pLChild;//   if (node.pLChild)// {//     pLChild = new TreeNode(node.pLChild->data, node.pLChild->index);//   }// delete pRChild;//   if (node.pRChild)// {//     pRChild = new TreeNode(node.pRChild->data, node.pRChild->index);//   }// delete pParent;//   if (node.pParent)// {//     pParent = new TreeNode(node.pParent->data, node.pParent->index);//   }//}//TreeNode& operator=(const TreeNode& node) //{//  if (this == &node)//  {//     return *this;// }// index = node.index;//  data = node.data;//    delete pLChild;//   if (node.pLChild)// {//     pLChild = new TreeNode(node.pLChild->data, node.pLChild->index);//   }////   delete pRChild;//   if (node.pRChild)// {//     pRChild = new TreeNode(node.pRChild->data, node.pRChild->index);//   }// delete pParent;//   if (node.pParent)// {//     pParent = new TreeNode(node.pParent->data, node.pParent->index);//   }// return *this;//}TreeNode* SearchNode(int iNodeIndex){if (index == iNodeIndex){return this;}TreeNode *temp = NULL;if (pLChild){if (pLChild->index == iNodeIndex){return pLChild;}else{temp = pLChild->SearchNode(iNodeIndex);if (temp){return temp;}}}if (pRChild){if (pRChild->index == iNodeIndex){return pRChild;}else{temp = pRChild->SearchNode(iNodeIndex);if (temp){return temp;}}}return NULL;}void DeleteNode(){if (pLChild != NULL){pLChild->DeleteNode();}if (pRChild != NULL){pRChild->DeleteNode();}if (pParent != NULL){if (this == pParent->pLChild)pParent->pLChild = NULL;if (this == pParent->pRChild)pParent->pRChild = NULL;}delete this;}//先序遍历 根左右void PreOrderTraversal(){cout << data << " ";if (pLChild){pLChild->PreOrderTraversal();}if (pRChild){pRChild->PreOrderTraversal();}}//中序遍历 左根右void InOrderTraversal(){if (pLChild){pLChild->InOrderTraversal();}cout << data << " ";if (pRChild){pRChild->InOrderTraversal();}}//后序遍历 左右根void PostOrderTraversal(){if (pLChild){pLChild->PostOrderTraversal();}if (pRChild){pRChild->PostOrderTraversal();}cout << data << " ";}//禁止拷贝、赋值
private:TreeNode(const TreeNode& node);TreeNode& operator=(const TreeNode& node);
};//基于链表的二叉树基本实现和遍历
class CTree
{
public:CTree(TreeNode *pNode = NULL){//创建树默认先创建根节点m_pRoot = new TreeNode();if (!m_pRoot){throw "根节点申请内存失败";return;}if (pNode){m_pRoot->index = 0;m_pRoot->data = pNode->data;}else{m_pRoot->index = 0;m_pRoot->data = '0';}}~CTree(){m_pRoot->DeleteNode();}TreeNode* SearchNode(int index){return m_pRoot->SearchNode(index);}bool AddNode(int index, int direction, TreeNode *pNode){if (!pNode){cout << "插入失败!新增的节点值为空。" << endl;return false;}TreeNode *temp = SearchNode(index);if (!temp){cout << pNode->data << "插入失败!找不到传入下标对应的父节点。" << endl;return false;}TreeNode *node = new TreeNode();if (!node){cout << pNode->data << "插入失败!新的节点申请内存失败。" << endl;return false;}node->index = pNode->index;node->data = pNode->data;node->pParent = temp;if (1 == direction){temp->pLChild = node;}else if (2 == direction){temp->pRChild = node;}else{cout << pNode->data << "插入失败!。direction参数错误:1为左节点,2为右节点" << endl;}return true;}bool DeleteNode(int index, TreeNode *pNode){TreeNode *temp = SearchNode(index);if (!temp){cout << "删除失败!找不到传入下标对应的节点。" << endl;return false;}if (pNode){pNode->index = temp->index;pNode->data = temp->data;}temp->DeleteNode();return true;}//先序遍历-递归void Recursive_PreOrderTraversal(){m_pRoot->PreOrderTraversal();}//中序遍历-递归void Recursive_InOrderTraversal(){m_pRoot->InOrderTraversal();}//后序遍历-递归void Recursive_PostOrderTraversal(){m_pRoot->PostOrderTraversal();}//先序遍历-非递归void PreOrderTraversal(){int stackTop = -1;TreeNode* nodeStack[10];TreeNode *pMove = /*new TreeNode(m_pRoot->index, m_pRoot->data)*/m_pRoot;while (stackTop != -1 || pMove){while (pMove){cout << pMove->data << " ";nodeStack[++stackTop] = pMove;pMove = pMove->pLChild;}if (stackTop != -1){pMove = nodeStack[stackTop];stackTop--;pMove = pMove->pRChild;}}}//中序遍历-非递归void InOrderTraversal(){int stackTop = -1;TreeNode* nodeStack[10];TreeNode *pMove = /*new TreeNode(m_pRoot->index, m_pRoot->data)*/m_pRoot;while (stackTop != -1 || pMove){//while (pMove){nodeStack[++stackTop] = pMove;pMove = pMove->pLChild;}if (stackTop != -1){pMove = nodeStack[stackTop--];cout << pMove->data << " ";pMove = pMove->pRChild;}}}//后序遍历-非递归void PostOrderTraversal(){int stackTop = -1;TreeNode* nodeStack[10];TreeNode* pMove = /*new TreeNode(m_pRoot->index, m_pRoot->data)*/m_pRoot;TreeNode* pLastVisit = NULL;while (pMove){nodeStack[++stackTop] = pMove;pMove = pMove->pLChild;}while (stackTop != -1){pMove = nodeStack[stackTop--];if (pMove->pRChild == NULL || pMove->pRChild == pLastVisit){cout << pMove->data << " ";pLastVisit = pMove;}else{nodeStack[++stackTop] = pMove;pMove = pMove->pRChild;while (pMove){nodeStack[++stackTop] = pMove;pMove = pMove->pLChild;}}}}
private:TreeNode *m_pRoot; //根节点
};#endif // !_CTREE_H_

测试二叉树结构图:

测试主函数main.cpp实现:

#include "CTree.hpp"
#include <iostream>
using namespace std;
int main(int argc, char**argv)
{TreeNode nodeA(0, 'A');TreeNode nodeB(1, 'B');TreeNode nodeC(2, 'C');TreeNode nodeD(3, 'D');TreeNode nodeE(4, 'E');TreeNode nodeF(6, 'F');TreeNode nodeG(9, 'G');CTree *tree = new CTree(&nodeA);tree->AddNode(0, 1, &nodeB);tree->AddNode(0, 2, &nodeC);tree->AddNode(1, 1, &nodeD);tree->AddNode(1, 2, &nodeE);tree->AddNode(2, 2, &nodeF);tree->AddNode(4, 1, &nodeG);cout << "递归遍历: " << endl;cout << "先序遍历 根左右: " << endl;tree->Recursive_PreOrderTraversal();cout << endl;cout << "中序遍历 左根右: " << endl;tree->Recursive_InOrderTraversal();cout << endl;cout << "后序遍历 左右根: " << endl;tree->Recursive_PostOrderTraversal();cout << endl;cout << "非递归遍历: " << endl;cout << "先序遍历 根左右: " << endl;tree->PreOrderTraversal();cout << endl;cout << "中序遍历 左根右: " << endl;tree->InOrderTraversal();cout << endl;cout << "后序遍历 左右根: " << endl;tree->PostOrderTraversal();cout << endl;delete tree;system("pause");return 0;
}

测试结果:

--|END|--

欢迎搜索个人WX公众号“IT集装箱”加关注,获取更多IT技术知识分享!

安卓如何实现多级结构树_数据结构-树(树基本实现C++)相关推荐

  1. python树结构_数据结构的树存储结构

    之前介绍的所有的数据结构都是线性存储结构.本章所介绍的树结构是一种非线性存储结构,存储的是具有"一对多"关系的数据元素的集合. (A)                       ...

  2. mysql索引用trie树_数据结构与算法之美【完整版】

    资源目录: ├─01-开篇词 (1讲) │ ├─00丨开篇词丨从今天起,跨过"数据结构与算法"这道坎.html │ ├─00丨开篇词丨从今天起,跨过"数据结构与算法&qu ...

  3. 心中有“树”:数据结构之树详解

    文章目录 前言 (一)树的基础定义与表示 1 树的定义 2 树的图示 3 树的逻辑结构表示法 (二)二叉树 1 二叉树定义 2 二叉树示意图 3 程序实现 (1)节点定义 (2)二叉树的先序遍历 (3 ...

  4. 大数据可视化陈为智慧树_知到智慧树_大数据可视化_大学课后答案

    知到智慧树_大数据可视化_大学课后答案 更多相关问题 用下列方法不能将NaCl和Na2CO3两种溶液区分开的是()A.向两种溶液中分别滴加醋酸B.向两种溶液中分别滴加稀硫 为了除去KCl中少量的MgS ...

  5. python数据结构 树_Python数据结构——AVL树的实现

    既然,我们已经证明,保持 AVL 树的平衡将会使性能得到很大的提升,那我们看看如何在程序中向树插入一个新的键值.因为所有的新键是作为叶节点插入树的,而新叶子的平衡因子为零,所以我们对新插入的节点不作调 ...

  6. 链表list(链式存储结构实现)_数据结构知否知否系列之 — 线性表的顺序与链式存储篇(8000 多字长文)...

    从不浪费时间的人,没有工夫抱怨时间不够. -- 杰弗逊 线性表是由 n 个数据元素组成的有限序列,也是最基本.最简单.最常用的一种数据结构. 作者简介:五月君,Nodejs Developer,热爱技 ...

  7. 怎么用python实现哈夫曼树_数据结构-哈夫曼树(python实现)

    好,前面我们介绍了一般二叉树.完全二叉树.满二叉树,这篇文章呢,我们要介绍的是哈夫曼树. 哈夫曼树也叫最优二叉树,与哈夫曼树相关的概念还有哈夫曼编码,这两者其实是相同的.哈夫曼编码是哈夫曼在1952年 ...

  8. mysql数据库设计与应用答案智慧树_知到智慧树_MySQL数据库设计与应用_答案完整...

    知到智慧树_MySQL数据库设计与应用_答案完整 更多相关问题 [B11]A.carry onB.linger onC.set inD.log in 数据库的三级模式结构中,模式也称为A.逻辑模式B. ...

  9. mysql目录树_无限级目录树+记忆节点状态(PHP+mysql)

    借鉴 网友 iuhxq 的设计制作而成的目录树,在此感谢 iuhxq 的代码对我大帮助. 特点: 1.无限级节点. 2.直接产生html代码,容易修改. 3.目录清楚,类似于资源管理器,(csdn论坛 ...

最新文章

  1. 12、OpenCV实现图像的空间滤波——图像平滑
  2. android recovery 模块知识需求汇总
  3. [密码学] 消息认证码基础
  4. PC微信逆向:分析发送xml名片call
  5. MII/MDIO接口详解(转)
  6. bzoj 1058: [ZJOI2007]报表统计
  7. 原码,反码,补码的表示范围总结
  8. 微软office即点即用服务能关闭吗?_电脑预装的正版office激活前就被卸载了怎么找回?...
  9. js获取image中src属性的方法语句
  10. 深职院c语言考试试卷,深职院第1章C语言基础与入门教学.ppt
  11. 不完全遍历Csrss进程中的句柄表
  12. Windows系统设置双网卡同时上内外网
  13. java远程桌面_java – 实现远程桌面共享解决方案
  14. Web前端大作业——城旅游景点介绍(HTML+CSS+JavaScript) html旅游网站设计与实现
  15. 程序员使用谷歌搜索的十种技巧
  16. Mybatis|CURD|配置详解|注解开发|多点查询|动态sql|缓存
  17. 【C认证】对标名企技术标准,大厂不是梦
  18. python矩阵运算函数_Numpy 常用矩阵计算函数
  19. B3U3 Text A Language Focus 共32词
  20. MAC PS用图片一个区域的颜色替换另一地方

热门文章

  1. Python爬虫2-GET_POST与开发者工具
  2. 吴裕雄--天生自然 高等数学学习:高阶偏导数
  3. Ubuntu dns
  4. P1979 [NOIP]华容道
  5. webpack入门之简单例子跑起来
  6. STM32GPIO管脚设置
  7. struts2配置详解
  8. UICollectionView之网络图片解析
  9. Android include 标签
  10. SLAM之特征匹配(一)————RANSAC-------OpenCV中findFundamentalMat函数使用的模型