以下内容源于慕课网的学习整理,如有侵权,请告知删除。

树存在概念中,是对数组或者链表的一种操作方式的概念。

一、与树有关的一些基础概念

(1)树

  • 有限节点的集合;

(2)度

  • 某个节点的直接孩子数目;

(3)叶节点

  • 终端节点

(4)祖先

  • 所有在它之上的节点

(5)深度

  • 节点的深度(节点所处的位置)
  • 树的深度(整棵树的深度)

(6)二叉树

  • 所有节点的度都小于等于2

(7)二叉树的遍历

  • 前中后,是针对“根”来说的。

(8)作用实例

  • 人机对战

二、二叉树的数组实现

1、换算公式

  • 父节点下标*2+1,得到,父节点的左孩子节点的下标;
  • 父节点下标*2+2,得到,父节点的右孩子节点的下标

2、示意图

  • 括号里面表示的是索引,或者说节点序号,外面的是数据。
  • 慕课网中只是按照这个图,来进行查找(遍历查找)、插入(已经确定在哪个位置上出入了)、删除(删除后赋值为0)等操作。

三、二叉树的链表实现

1、节点要素

  • 数据,左孩子指针,右孩子指针,父指针,索引(这里用来表征节点的序号)

2、删除元素

  • 要求把对应的子节点也删除掉;
  • 也可能要求只删除该点,然后该点的孩子节点指向该点的父节点;

3、前序遍历(根左右)

4、中序遍历(左根右)

首先是左526,然后是根0,接着是右897;

然后对526进行同样的操作,即左是2,根是5,右是6,则排序结果是256;

同理对897进行同样的操作,即左是9,根是8,右是7,则排序结果是987;

最后合成为256 0 987。

5、后序遍历(左右根)

首先是左526,然后是右897,接着是根0;

然后对526进行同样的操作,即左是2,右是6,根是5,则排序结果是265;

同理对897进行同样的操作,即左是9,右是7,根是8,则排序结果是978;

最后合成为265  978 0。

6、编码实现

  • 树类、节点类;
  • 节点类包含五要素:数据,左孩子指针,右孩子指针,父指针,索引(这里用来表征节点的序号)
  • 前序遍历(中序遍历、后序遍历就调换相应的位置即可)。
  • 对树的遍历操作,落实在根节点调用遍历函数。

node.h

#ifndef  NODE_H
#define NODE_H
class Node{
public :Node();int index;int data;Node *pLNOde;Node *pRNode;Node *pParent;Node *SerchNode(int index);void deleteNode();void Preorder();void Midorder();void Postorder();
};
#endif

node.cpp

#include "Node.h"
#include <iostream>
using namespace std;Node::Node()
{index=0;data=0;pLNOde=NULL;pRNode=NULL;pParent=NULL;
};Node* Node::SerchNode(int index)
{Node *temp=NULL;if (this->index==index)return this;if (this->pLNOde!=NULL){  temp=this->pLNOde->SerchNode(index);if (temp!=NULL){return temp;}}if (this->pRNode!=NULL){temp=this->pRNode->SerchNode(index);if (temp!=NULL){return temp;}}return NULL;
};void Node::deleteNode()
{if(this->pLNOde!=NULL) {this->pLNOde->deleteNode();}if (this->pRNode!=NULL){this->pRNode->deleteNode();}if (this->pParent!=NULL){if (this->pParent->pLNOde==this){this->pParent->pLNOde=NULL;}if (this->pParent->pRNode==this){this->pParent->pRNode=NULL;}}delete this;
};void Node::Preorder()
{cout<<this->data<<" "<<this->index<<endl;if (this->pLNOde!=NULL){this->pLNOde->Preorder();}if (this->pRNode!=NULL){this->pRNode->Preorder();}
};void Node::Midorder()
{if (this->pLNOde!=NULL){this->pLNOde->Midorder();}cout<<this->data<<" "<<this->index<<endl;if (this->pRNode!=NULL){this->pRNode->Midorder();}
};void Node::Postorder()
{if (this->pLNOde!=NULL){this->pLNOde->Postorder();}if (this->pRNode!=NULL){this->pRNode->Postorder();}cout<<this->data<<" "<<this->index<<endl;
};

tree.h

#ifndef  Tree_H
#define Tree_H
#include "Node.h"class Tree
{
public:Tree();~Tree();Node *SerchNode(int index);//查找索引为index的那个节点,并返回指向该节点的指针bool addNode(int index,int direction,Node *node);//添加,在索引为index的节点上,添加一个节点node//左右方向由direction决定bool deleteNode(int index,Node *node);//删除void Preorder();//前序void Midorder();//中序void Postorder();//后序
private:Node *p_node;};#endif

tree.cpp

#include "Tree.h"
#include "Node.h"
#include <iostream>
using namespace std;Tree::Tree()
{p_node=new Node();
};Tree::~Tree()
{deleteNode(0,NULL);//这里调用的是tree中的删除节点函数,从根节点(0)开始
};Node *Tree::SerchNode(int index)
{return p_node->SerchNode(index);};bool Tree::deleteNode(int index,Node *node)
{Node *temp=SerchNode(index);if (temp!=NULL){if (node!=NULL)//传入的Node可以为null。是null时,表明不需要把要删除的节点的数据保存。{node->index=temp->index;node->data=temp->data;}temp->deleteNode();return true;}elsereturn false;
};bool Tree::addNode(int index,int direction,Node *node)
{Node *temp=SerchNode(index);if (temp){Node *NewNode=new Node();if (NewNode==NULL){return false;}NewNode->data=node->data;NewNode->index=node->index;if (direction==0){temp->pLNOde=NewNode;NewNode->pParent=temp;}if (direction==1){NewNode->pParent=temp;temp->pRNode=NewNode;}return true;}return false;
};void Tree::Preorder()
{p_node->Preorder();
}void Tree::Midorder()
{p_node->Midorder();
}void Tree::Postorder()
{p_node->Postorder();
}

test.cpp

#include "Node.h"
#include "Tree.h"
#include <iostream>using namespace std;
/*0(0)1(1)      2(2)5(3)  7(4)  6(5)   9(6)*/
int main()
{Tree *p=new Tree;//这里的p是指向根节点的指针Node *n2=new Node;n2->data=1;n2->index=1;Node *n3=new Node;n3->data=2;n3->index=2;Node *n4=new Node;n4->data=3;n4->index=3;Node *n5=new Node;n5->data=4;n5->index=4;Node *n6=new Node;n6->data=5;n6->index=5;Node *n7=new Node;n7->data=6;n7->index=6;Node *n8=new Node;n8->data=8;n8->index=8;Node *n9=new Node;n9->data=9;n9->index=9;  Node *n10=new Node;n10->data=10;n10->index=10;Node *n11=new Node;n11->data=11;n11->index=11;p->addNode(0,0,n2);p->addNode(0,1,n3);p->addNode(1,0,n4);p->addNode(1,1,n5);p->addNode(2,0,n6);p->addNode(2,1,n7);
//  p->addNode(4,0,n8);
//  p->addNode(4,1,n9);
//  p->addNode(6,0,n10);
//  p->addNode(6,1,n11);Node *n18=new Node;n18=p->SerchNode(10);if (n18!=NULL){cout<<"index:"<<n18->index<<endl;}//Node *n12=new Node;//p->deleteNode(6,n12);p->Preorder();cout<<"========================="<<endl;p->Postorder();cout<<"========================="<<endl;p->Midorder();delete p;p=NULL;system("pause");return 0;
}

数据结构探险——树篇相关推荐

  1. 数据结构探险——图篇

    以下内容源于慕课网的学习整理,如有侵权,请告知删除. 1.图的相关概念         2.图的存储结构 第一种是用数组表达,第二三种用链表来表示有向图,最后一种链表来表示无向图. (1)邻接矩阵(有 ...

  2. 数据结构探险——栈篇

    以下内容源于慕课网的学习整理,如有侵权,请告知删除. 1.栈要素 栈底(不变).栈顶(随着入栈和出栈而改变) 2.栈机制的实现 (1)栈的相关机制 判断栈的空满: 入栈.出栈: 遍历栈: 清除栈内容: ...

  3. 数据结构探险——队列篇

    以下内容源于慕课网的学习整理,如有侵权,请告知删除. 1.什么是队列? (1)先入先出的模型(FIFO). (2)队头,队尾. (3)细分为普通队列,环形队列. 普通队列存在的问题 如果买票者不动,售 ...

  4. 数据结构探险系列—栈篇-学习笔记

    数据结构探险-栈篇 什么是栈? 古代栈就是牲口棚的意思. 栈是一种机制:后进先出 LIFO(last in first out) 电梯 栈要素 空栈.栈底,栈顶.没有元素的时候,栈顶和栈底指向同一个元 ...

  5. 数据结构——小白入门篇

    数据结构--小白入门篇 浅谈学习心得 我为什么想要学数据结构? 在计算机界有这样一个万能公式:数据结构 + 算法 = 程序. 在如今这计算机引领风骚的时代,不学数据结构,你凭什么想要做时代的弄潮儿:所 ...

  6. 数据结构(终极线段树篇)

    数据结构(终极线段树篇) 摘要: 问题的提出:如何解决多样化的区间操作问题? solve:线段树!!! 关键字: 线段树,可持久化线段树,权值线段树,线段树森林,动态开点线段树,区间操作,线段树应用. ...

  7. 数据结构探险之图篇(上)理论篇

    数据结构探险之图篇 什么是图? 如下图:无向图 & 有向图 箭头分方向. 无向图中每条认为有来有回两条线 无向图&有向图 图中的概念: 有向图中的概念 结点称为顶点. 之间的线称为弧. ...

  8. 数据结构显示树的所有结点_您需要了解的有关树数据结构的所有信息

    数据结构显示树的所有结点 When you first learn to code, it's common to learn arrays as the "main data struct ...

  9. 数据结构之树:树的介绍——9

    数据结构之树,介绍篇 树的基本定义 介绍:树(tree)是计算机中非常重要的数据结构,它的外形看起来像一颗倒挂着的的树,使用树这种结构可以描述生活中很多的事物,如族谱,单位的组织架构,xml,html ...

最新文章

  1. 人力资源部如何运用OKR?看三大层面最新OKR模板
  2. Kotlinkotlin二进制与十六进制之间的转化
  3. java nio.Buffer的属性变化
  4. graphicsmagick im4java,GraphicsMagick+im4java 图片处理
  5. pdh光端机相关知识介绍
  6. 【Java Saves!】Session 2:我的意图
  7. 树莓派 4b 可执行文件 无法双击运行_树莓派01 - 树莓派系统安装
  8. Android Excel 解析 xls 和 xlsx,方法也可以很简单
  9. addClass(““).delay().removeClass(““);没有效果的解决方式
  10. 这一篇带你学点儿 Java8 中的流式数据处理
  11. 大学毕业生参考信函提示
  12. 电脑可以连接别的手机热点,唯独连接不上某个手机热点怎么办法?
  13. c语言程序数列问题,数列 (C语言代码)
  14. pycharm新建python项目等问题
  15. Oracle系列之add_months简介以及用法归纳教程
  16. U盘安装win7系统 “详细” 教程
  17. qq营销软件 服务器 虚拟机,QQ营销软件哪个好
  18. ORA-39083:Object type TYPE failed to create with error:OID问题。
  19. oracle列转行求和,Oracle行转列和列转行
  20. word文档添加多级目录,自动生成目录

热门文章

  1. orCAD使用Orcad Capture CIS按Room摆放
  2. 外围功能电路控制 LET′S TRY“嵌入式编程”: 4 of 6
  3. 用JSmooth制作java jar文件的可运行exe文件教程【图文】
  4. Forensic Challenge 9 - Mobile Malware
  5. GPRS底层API(转)
  6. Showdoc 搭建项目 API 文档系统
  7. HIVE ORC 报错ClassCastException
  8. tensorflow源码安装
  9. 课堂作业整理三 (集合:list接口)
  10. Centos6.5升级GCC