分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

二叉树的定义

      二叉树(BinaryTree)是n(n>=0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的、分别称作这个根的左子树右子树的二叉树组成。

二叉树的性质

性质1 二叉树第i层上的结点数目最多为2i-1(i≥1)。
证明:

  用数学归纳法证明。
  归纳基础:i=1时,有2i-1=20=1。因为第1层上只有一个根结点,所以命题成立。
  归纳假设:假设对所有的j(1≤j<i)命题成立,即第j层上至多有2j-1个结点,证明j=i时命题亦成立。
  归纳步骤:根据归纳假设,第i-1层上至多有2i-2个结点。由于二叉树的每个结点至多有两个孩子,故第i层上的结点数至多是第i-1层上的最大结点数的2倍。即j=i时,该层上至多有2×2i-2=2i-1个结点,故命题成立。

性质2 深度为k的二叉树至多有2k-1个结点(k≥1)。
证明:

  在具有相同深度的二叉树中,仅当每一层都含有最大结点数时,其树中结点数最多。因此利用性质1可得,深度为k的二叉树的结点数至多为:
    20+21+…+2k-1=2k-1
  故命题正确。

性质3 在任意-棵二叉树中,若终端结点的个数为n0,度为2的结点数为n2,则n0=n2+1。
证明:

  因为二叉树中所有结点的度数均不大于2,所以结点总数(记为n)应等于0度结点数n0、1度结点数n1和2度结点数n2之和,即

    n=n0+n1+n2 (式子1)

  另一方面,1度结点有一个孩子,2度结点有两个孩子,故二叉树中孩子结点总数是n1+2n2,而树中只有根结点不是任何结点的孩子,故二叉树中的结点总数又可表示为

    n=n1+2n2+1 (式子2)

  由式子1和式子2得到:

    n0=n2+1

二叉树的实现(C++)

#include <iostream>#define NULL 0using namespace std;template<class T>struct BTNode{ T data; BTNode<T> *lChild, *rChild; BTNode(); BTNode(const T &val, BTNode<T> *Childl = NULL, BTNode<T> *Childr = NULL) {  data = val;  lChild = Childl;  rChild = Childr; } BTNode<T>* CopyTree() {  BTNode<T> *l, *r, *n;  if(&data == NULL)  {   return NULL;  }  l = lChild->CopyTree();  r = rChild->CopyTree();  n = new BTNode<T>(data, l, r);  return n; }};template<class T>BTNode<T>::BTNode(){ lChild = rChild = NULL;}template<class T>class BinaryTree{public: BTNode<T> *root; BinaryTree(); ~BinaryTree(); void Pre_Order(); void In_Order(); void Post_Order(); int TreeHeight() const; int TreeNodeCount() const; void DestroyTree(); BTNode<T>* MakeTree(const T &element, BTNode<T> *l, BTNode<T> *r) {  root = new BTNode<T> (element, l, r);  if(root == NULL)  {   cout << "Failure for applying storage address, system will close the process." << endl;   exit(1);  }  return root; }private: void PreOrder(BTNode<T> *r); void InOrder(BTNode<T> *r); void PostOrder(BTNode<T> *r); int Height(const BTNode<T> *r) const; int NodeCount(const BTNode<T> *r) const; void Destroy(BTNode<T> *&r);};template<class T>BinaryTree<T>::BinaryTree(){ root = NULL;}template<class T>BinaryTree<T>::~BinaryTree(){ DestroyTree();}template<class T>void BinaryTree<T>::Pre_Order(){ PreOrder(root);}template<class T>void BinaryTree<T>::In_Order(){ InOrder(root);}template<class T>void BinaryTree<T>::Post_Order(){ PostOrder(root);}template<class T>int BinaryTree<T>::TreeHeight() const{ return Height(root);}template<class T>int BinaryTree<T>::TreeNodeCount() const{ return NodeCount(root);}template<class T>void BinaryTree<T>::DestroyTree(){ Destroy(root);}template<class T>void BinaryTree<T>::PreOrder(BTNode<T> *r){ if(r != NULL) {  cout << r->data << ' ';  PreOrder(r->lChild);  PreOrder(r->rChild); }}template<class T>void BinaryTree<T>::InOrder(BTNode<T> *r){ if(r != NULL) {  InOrder(r->lChild);  cout << r->data << ' ';  InOrder(r->rChild); }}template<class T>void BinaryTree<T>::PostOrder(BTNode<T> *r){ if(r != NULL) {  PostOrder(r->lChild);  PostOrder(r->rChild);  cout << r->data << ' '; }}template<class T>int BinaryTree<T>::NodeCount(const BTNode<T> *r) const{ if(r == NULL) {  return 0; } else {  return 1 + NodeCount(r->lChild) + NodeCount(r->rChild); }}template<class T>int BinaryTree<T>::Height(const BTNode<T> *r) const{ if(r == NULL) {  return 0; } else {  int lh, rh;  lh = Height(r->lChild);  rh = Height(r->rChild);  return 1 + (lh>rh?lh:rh); }}template<class T>void BinaryTree<T>::Destroy(BTNode<T> *&r){ if(r != NULL) {  Destroy(r->lChild);  Destroy(r->rChild);  delete r;  r = NULL; }}void main(){ BTNode<char> *b, *c, *d, *e, *f, *g; BinaryTree<char> a; b = a.MakeTree('F', NULL, NULL); c = a.MakeTree('E', NULL, NULL); d = a.MakeTree('D', NULL, NULL); e = a.MakeTree('C', b, NULL); f = a.MakeTree('B', d, c); g = a.MakeTree('A', f, e); cout << "Pre Order: "; a.Pre_Order(); cout << endl; cout << "In Order: "; a.In_Order(); cout << endl; cout << "Post Order: "; a.Post_Order(); cout << endl; cout << "Tree Height: "; cout << a.TreeHeight(); cout << endl; cout << "The Count of Tree Nodes: "; cout << a.TreeNodeCount(); cout << endl;}// Output:/*Pre Order: A B D E C FIn Order: D B E A F CPost Order: D E B F C ATree Height: 2The Count of Tree Nodes: 6*/

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

数据结构 二叉树相关推荐

  1. 数据结构 -- 二叉树

          这篇文章介绍的是经典的数据结构--二叉树,在这篇文章里介绍了几乎二叉树的所有操作.       二叉树给我们最重要的印象莫过于递归,因为这棵树就是递归的,所以,我在解决各个问题时大部分都用 ...

  2. 数据结构 - 二叉树 - 面试中常见的二叉树算法题

    数据结构 - 二叉树 - 面试中常见的二叉树算法题 数据结构是面试中必定考查的知识点,面试者需要掌握几种经典的数据结构:线性表(数组.链表).栈与队列.树(二叉树.二叉查找树.平衡二叉树.红黑树).图 ...

  3. 数据结构——二叉树的递归算法

    二叉树的结构定义: typedef struct BiNode {TElemType data;struct BiNode *lchild;struct BiNode *rchild; }BiNode ...

  4. 数据结构——二叉树的层次遍历进阶

    之前的一个博客 数据结构--二叉树的层次遍历看完这个,可以简单实现下面的问题 问题: 1.计算二叉树的最大宽度(二叉树的最大宽度是指二叉树所有层中结点个数的最大值. 2.用按层次顺序遍历二叉树的方法, ...

  5. 数据结构----二叉树叶子结点到根节点的高度计算

    数据结构----二叉树叶子结点到根节点的高度计算 代码: #include<stdio.h> #include<stdlib.h> typedef struct bstTree ...

  6. 数据结构 二叉树的存储结构_线程二叉树| 数据结构

    数据结构 二叉树的存储结构 线程二叉树 (Threaded Binary Tree ) A binary tree can be represented by using array represen ...

  7. 二叉树----数据结构:二叉树的三种遍历及习题

    二叉树----数据结构:二叉树的三种遍历,利用递归算法. 关于二叉树的遍历,应用非常广泛,不单单是访问打印结点,还可以进行一系列的操作,如赋值.删除.查找.求二叉树的深度等等. 有递归和非递归两种算法 ...

  8. 数据结构-二叉树入门Go语言实现

    数据结构-二叉树入门Go语言实现 之前我们一直在谈的是一对一的线性结构,可现实中,还有很多一对多的情况需要处理,所以我们需要研究这种一对多的数据结构--"树",考虑它的各种特性,来 ...

  9. 数据结构——二叉树——特点及性质

    数据结构--二叉树--特点及性质 二叉树(Binary Tree)是n(n=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互不相交的.分别称为根结点的左子树和右子树的二 ...

  10. 数据结构——二叉树总结

    数据结构-二叉树总结 写在前面 二叉树遍历 递归实现先.中.后序遍历 非递归遍历 先序非递归 中序非递归 后序非递归 层次遍历 二叉树还原 先序中序建树 后序中序建树 层次中序建树 二叉树应用 二叉查 ...

最新文章

  1. 微服务如何设计一个配置中心
  2. 铁线蕨算法(Adiantum)为低端智能手机提供磁盘加密服务
  3. html css js书写规范
  4. python3上传文件_Python3学习笔记(十八):文件上传和下载
  5. 解决Layui的switch样式显示问题
  6. 皮一皮:不能太自信。。。
  7. 测试CPU品牌和当前工作频率
  8. MySQL Workbench 5.2.45 GA 发布
  9. iOS iCloud云存储数据
  10. SpringBoot实用小技巧之动态设置SpringBoot日志级别 1
  11. 淘宝top平台调用接口响应时间优化
  12. 跟我学习Storm_Storm基本架构
  13. 洛谷P3509 [POI2010]ZAB-Frog
  14. PID控制算法的C语言实现
  15. H3C S5500V2交换机误格式化恢复
  16. linux qt 找不到 lgl,c ++ - Qt:找不到-lGL
  17. 微信开发网页授权获取用户信息
  18. STM32开发(14)----CubeMX配置ADC
  19. win10在几个窗口间切换的快捷键
  20. 安装colmap时报错“METIS_INCLUDE_DIRS-NOTFOUND“

热门文章

  1. Vitamio FAQ(2012-11-20 )
  2. 数据库设计中的14个技巧
  3. 老焦专栏 | 如何做一个有说服力的方案?
  4. Jenkins(1)
  5. node使用npm一句命令停止某个端口号 xl_close_port
  6. P2730 魔板 Magic Squares
  7. Html5 Canvas 学习之路(一)
  8. 破解 Windows 2003终端服务许可证
  9. 阿里云网站80端口无法访问
  10. 开源 java CMS - FreeCMS2.6 模型管理