1. Definiation

What is Binary Trees?

Collection of node (n>=0) and in which no node can have more than two children.

或为空集,或为有一个根节点和两棵互不相交的二叉树组成

左子树与右子树是有顺序的,不能颠倒

即使是只有一颗子树也要分清是左子树还是右子树

2. property

(1)  若二叉树的层次从1开始,则在二叉树的第i 层最多有2^(i-1)个结点;

(2)高度为k 的二叉树最多有2^k-1个结点;

(3)对任一棵二叉树,如果其叶结点个数为n0,度为2的非叶结点个数为n2,则有   n0=n2+1;

(4)具有n个结点的完全二叉树的高度为[logn]+1

(5)

3. 特殊的二叉树

(1)斜树

一波流,要么往左斜,要么往右斜

(2)满二叉树

一颗深度为k且有2^k-1个节点

叶子只能出现在最后一层

(3)完全二叉树

对一颗有n个节点的二叉树编号,如果编号为i的节点与满二叉树的节点编号相同,则这棵树称为完全二叉树

最下层叶子节点一定集中在左部连续

if倒数第二层有叶子节点,则一定在右部连续。

if结点度为一,则一定只有左孩子

同样结点的二叉树,完全二叉树深度是最小的

满二叉树一定是完全二叉树

4、二叉树的储存结构

(1)顺序储存二叉树

完全二叉树

层序遍历,可以用数组表示逻辑结构

一般二叉树

不存在的结点就用^表示

但如果是斜树呢?

显然浪费太多的空间

要考虑用链式存储结构

(2)链式储存二叉树

二叉树最多有两个孩子,设计一个数据域和两个指针域,叫做二叉链表

  1. template<typename Object>
  2. struct bitTree
  3. {
  4. Object data;
  5. bitTree<Object>*lchild, rchild;
  6. };

5、遍历

What is  遍历?

二叉树的遍历(traversing binary tree)从根结点出发,按照某种次序依次访问二叉树中所有结点,使得每个结点有且只被访问一次。

前序遍历:

若二叉树为空,则空操作返回,否则先访问根结点,然后前序遍历左子树,再前序遍历右子树。

中序遍历

若树为空,则空操作返回,否则从根结点开始(注并不是先访问根结点),中序遍历根结点的左子树,然后访问根结点,最后中序遍历右子树

后序遍历

若树为空,则空操作返回,否则从左到右线叶子后结点的方式遍历访问左右子树,最后访问根结点

层序遍历

一层一层遍历

6、二叉树的建立与遍历算法

  1. //建立二叉树,并输出每个字符所在层数
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. typedef struct BitNode
  6. {
  7. char data;
  8. struct BitNode *lchild, *rchild;
  9. }BitNode, *BitTree;
  10.  
  11. //创建一棵二叉树,约定用户遵照前序遍历的方式遍历
  12. void CreateBitTree(BitTree *T)
  13. {
  14. char c;
  15. cin>>c;
  16.  
  17. if(c=='-')
  18. {
  19. *T=NULL;
  20. }
  21. else
  22. {
  23. *T=new BitNode();
  24. (*T)->data=c;
  25. CreateBitTree(&((*T)->lchild));
  26. CreateBitTree(&(*T)->rchild));
  27. }
  28.  
  29. }
  30.  
  31. //访问二叉树结点的具体操作
  32. void visit(char data, int level)
  33. {
  34. cout<<data<<" in "<< level << endl;
  35. }
  36.  
  37. //前序遍历二叉树
  38. void PreOrderTraversal(BitTree T, int level)
  39. {
  40. if(T)
  41. {
  42. visit(T->data,level);
  43. PreOrderTraversal(T->lchild,level+1);
  44. PreOrderTraversal(T->rchild,level+1);
  45. }
  46. }
  47.  
  48. //后续遍历删除二叉树
  49. void PostOrderTracersalDelete(BitTree T)
  50. {
  51. if(T)
  52. {
  53. PostOrderTracersalDelete(T->lchild);
  54. PostOrderTracersalDelete(T->rchild);
  55. delete T;
  56. }
  57. }
  58.  
  59. void play(char data)
  60. {
  61. cout<<"haha,MidOrderTraversal "<<data<<endl;
  62. }
  63.  
  64. //中序遍历二叉树
  65. void MidOrderTraversal(BitTree T)
  66. {
  67. if(T)
  68. {
  69. MidOrderTraversal(T->lchild);
  70. play(T->data);
  71. MidOrderTraversal(T->rchild);
  72. }
  73. }
  74.  
  75. int main()
  76. {
  77. int level=1;
  78. BitTree T=NULL;
  79.  
  80. CreateBitTree(&T); //We have to pass the reference
  81. PreOrderTraversal(T,level);
  82. MidOrderTraversal(T);
  83. PostOrderTracersalDelete(T);
  84.  
  85. return 0;
  86. }

  

  

Binary Trees的更多相关文章

  1. hdu3240 Counting Binary Trees

    Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  2. [leetcode-617-Merge Two Binary Trees]

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  3. Merge Two Binary Trees

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  4. [LeetCode] Merge Two Binary Trees 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  5. [Swift]LeetCode617. 合并二叉树 | Merge Two Binary Trees

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  6. [Swift]LeetCode823. 带因子的二叉树 | Binary Trees With Factors

    Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...

  7. [Swift]LeetCode894. 所有可能的满二叉树 | All Possible Full Binary Trees

    A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...

  8. [Swift]LeetCode951. 翻转等价二叉树 | Flip Equivalent Binary Trees

    For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...

  9. [LeetCode] Binary Trees With Factors 带因子的二叉树

    Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...

随机推荐

  1. MiniMetro Items

    圈代表居民区三角写字楼等工作区方块是商业区钻石是金融中心五角星是政府 十字是医院 扁扁的旅游景区

  2. 解决IE浏览器“无法显示此网页”的问题

    诊断后提示:远程计算机或设备不接受连接 其他浏览器可以正常使用,QQ什么的也都正常,只有IE不能上网诊断提示:远程计算机或设备将不接受连接 ,网上找了好多方法都行不通.最后发现了这种方法,问题简单解决 ...

  3. Scala 中Null, None, Nothing, Nil

    转自:http://blog.csdn.net/bluejoe2000/article/details/30465175 在scala中这四个类型名称很类似,作用确实完全不同的. None是一个obj ...

  4. iOS GCD基础篇 - 同步、异步,并发、并行的理解

    1.关于GCD - GCD全称是Grand Central Dispatch  - GCD是苹果公司为多核的并行运算提出的解决方案  - GCD会自动利用更多的CPU内核(比如双核.四核)  - GC ...

  5. 一篇顺手的Ubuntu+caffe配置笔记

    主要参考: https://github.com/lbzhang/dl-setup http://ouxinyu.github.io/Blogs/20151108001.html http://www ...

  6. Ecstore安装篇-1.运行系统环境要求

    运行系统环境要求 运行系统环境要求 author :James,jimingsong@vip.qq.com since :2015-03-01 支持的操作系统 支持的WEB服务器 支持的浏览器 支持的 ...

  7. 使用firbug调试程序写更高质量的代码设置方法

    在搜狐浏览器内输入about:config 在搜索栏中输入:strict 双击javascript.options.strict,将值变为true

  8. php,cgi,nginx关系

    nginx是服务器 什么是服务器? 例如:IIS,Apache,Nginx......主要是提供网上浏览网页的服务,应用层使用HTTP协议. CGI,FastCGI CGI全称是"公共网关接 ...

  9. location修改的时候报错解决办法

    location修改的时候直接保存record,或者转换成DbObject都会报错,这样准换之后就好了if(key.equals("location")&&valu ...

  10. servlet之过滤器(转载)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.Filter 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...