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)链式储存二叉树

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

template<typename Object>
struct bitTree
{
Object data;
bitTree<Object>*lchild, rchild;
};

5、遍历

What is  遍历?

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

前序遍历:

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

中序遍历

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

后序遍历

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

层序遍历

一层一层遍历

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

//建立二叉树,并输出每个字符所在层数
#include<iostream>
using namespace std; typedef struct BitNode
{
char data;
struct BitNode *lchild, *rchild;
}BitNode, *BitTree; //创建一棵二叉树,约定用户遵照前序遍历的方式遍历
void CreateBitTree(BitTree *T)
{
char c;
cin>>c; if(c=='-')
{
*T=NULL;
}
else
{
*T=new BitNode();
(*T)->data=c;
CreateBitTree(&((*T)->lchild));
CreateBitTree(&(*T)->rchild));
} } //访问二叉树结点的具体操作
void visit(char data, int level)
{
cout<<data<<" in "<< level << endl;
} //前序遍历二叉树
void PreOrderTraversal(BitTree T, int level)
{
if(T)
{
visit(T->data,level);
PreOrderTraversal(T->lchild,level+1);
PreOrderTraversal(T->rchild,level+1);
}
} //后续遍历删除二叉树
void PostOrderTracersalDelete(BitTree T)
{
if(T)
{
PostOrderTracersalDelete(T->lchild);
PostOrderTracersalDelete(T->rchild);
delete T;
}
} void play(char data)
{
cout<<"haha,MidOrderTraversal "<<data<<endl;
} //中序遍历二叉树
void MidOrderTraversal(BitTree T)
{
if(T)
{
MidOrderTraversal(T->lchild);
play(T->data);
MidOrderTraversal(T->rchild);
}
} int main()
{
int level=1;
BitTree T=NULL; CreateBitTree(&T); //We have to pass the reference
PreOrderTraversal(T,level);
MidOrderTraversal(T);
PostOrderTracersalDelete(T); return 0;
}

  

  

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. android中edittext被键盘挡住问题

    最近开始新项目,做注册页时候由于ui布局问题,edittext被键盘挡住了. 在stackoverflow上找了一遍,有提到在对应activity中设置windowSoftInputMode, 例如: ...

  2. 循序渐进看Java web日志跟踪(2)-Java日志API认识

    接触过Java的朋友应该都会知道,java的开源框架百花齐放,实现同样的功能,总能找到几个强大的开源框架来进行选择.在日志方面,Java同样不逊色.除了JDK本身自带的简单的日志工具,java还有如l ...

  3. HOJ———丢手绢

    hide handkerchief Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  4. CODE[VS]-寻找子串位置-字符串处理-天梯青铜

    题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述 ...

  5. iOS中定时器NSTimer的使用/开启与关闭

      一.只调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5  ...

  6. 关于js的那些事儿

    什么是JS? JavaScript是一种基于对象(Object)和事件驱动(Event Driven),并具有相对安全性的客户端脚本语言.同时也是一种广泛用户客户端Web开发的脚本语言,常用来给HTM ...

  7. python 之遍历目录树(可匹配输出特定后缀的文件)

    涉及到的模块有os, fnmatch:1.通过os模块中的方法获取dir.subdir.files,通过os.path.join可拼接成完整路径: 2.fnmatch主要通过fnmatch.fnmat ...

  8. 微信小程序文档解读(一)--api提供支持有哪些

    本文重点在于小程序API提供的微信功能支持及获取用户信息的解读,具体的用法和调用不在本文讨论范围之内,文章基于20161222版文档解读 API官方文档原文链接 小程序API官方定义: 框架提供丰富的 ...

  9. FD.io VPP 技术Neutron VNF vRouter 实现

    在OpenStack Neutron中主要有三种网络设备,路由器(Router),负载均衡器(LB)以及VPN,其中Router作为基础网络设备起到连接子网到子网.内网到外网的作用.不同子网之间的访问 ...

  10. mybatis处理特殊符号

    当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...