二叉树类型

二叉树类型

满二叉树 (full binary tree):

A Binary Tree is a full binary tree if every node has 0 or 2 children

完全二叉树 (complete binary tree):

A Binary Tree is a Complete Binary Tree if all the levels are completely filled except possibly the last level and the last level has all keys as left as possible

完美二叉树 (perfect binary tree):

A Binary tree is a Perfect Binary Tree in which all the internal nodes have two children and all leaf nodes are at the same level.

平衡二叉树 (Balanced binary tree):

A binary tree is balanced if the height of the tree is O(Log n) where n is the number of nodes. For Example, the AVL tree maintains O(Log n) height by making sure that the difference between the heights of the left and right subtrees is at most 1. Red-Black trees maintain O(Log n) height by making sure that the number of Black nodes on every root to leaf paths is the same and there are no adjacent red nodes. Balanced Binary Search trees are performance-wise good as they provide ** O(log n) ** time for search, insert and delete.

衰减树形(A degenerate or pathological tree)

A Tree where every internal node has one child. Such trees are performance-wise same as linked list.

二叉搜索树 (Binary Search Tree)

A node-based binary tree data structure which has the following properties:

  • The left subtree of a node contains only nodes with keys lesser than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • The left and right subtree each must also be a binary search tree.

Binary Search Tree

AVL tree (Adelson-Velsky and Landis)

In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property.Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.

B树(B-tree)

B树和平衡二叉树的不同之处是:B树属于多叉树又名平衡多路查找树(查找路径不止两个),数据库索引技术里大量使用着B树和B+树的数据结构。

注意: 有文章把B树和B-tree理解成了两种不同类别的树,其实这两个是同一种树

B树的构建规则:
(1)排序方式:所有节点关键字是按递增次序排列,并遵循左小右大原则;

(2)子节点数:非叶节点(根节点和枝节点)的子节点数 >1、且子节点数量<=M 、且M>=2,空树除外(注:M阶代表一个树节点最多有多少个查找路径,M=M路,当M=2则是2叉树,M=3则是3叉);

(3)关键字数:枝节点的关键字数量大于等于ceil(m/2)-1个且小于等于M-1个(注:ceil()是个朝正无穷方向取整的函数 如ceil(1.1)结果为2);

(4)所有叶子节点均在同一层、叶子节点除了包含了关键字 和 关键字记录的指针外,也有指向其子节点的指针只不过其指针地址都为null对应下图最后一层节点的空格子;

我们一个实际的例子来理解B树(这里为了理解方便我就直接用实际字母的大小来排列C>B>A)

B+树

B+树是在B树的基础上又一次的改进,其主要对两个方面进行了提升,一方面是查询的稳定性,另外一方面是在数据排序方面更友好。

B+树构建规则
(1)B+树的非叶子节点不保存具体的数据,而只保存关键字的索引,而所有的数据最终都会保存到叶子节点。因为所有数据必须要到叶子节点才能获取到,所以每次数据查询的次数都一样,这样一来B+树的查询速度也就会比较稳定,而B树的查找过程中,不同的关键字查找的次数很有可能都是不同的(有的数据可能在根节点,有的数据可能在最下层的叶节点),所以在数据库的应用层面,B+树就显得更合适。

(2)B+树叶子节点的关键字从小到大有序排列,左边结尾数据都会保存右边节点开始数据的指针。因为叶子节点都是有序排列的,所以B+树对于数据的排序有着更好的支持。

(3)非叶子节点的子节点数=关键字数(来源百度百科)(根据各种资料 这里有两种算法的实现方式,另一种为非叶节点的关键字数=子节点数-1(来源维基百科),虽然他们数据排列结构不一样,但其原理还是一样的Mysql 的B+树是用第一种方式实现);

B+树和B树的对比
1、B+树查询速度更稳定:B+所有关键字数据地址都存在叶子节点上,所以每次查找的次数都相同所以查询速度要比B树更稳定。

2、B+树天然具备排序功能:B+树所有的叶子节点数据构成了一个有序链表,在查询大小区间的数据时候更方便,数据紧密性很高,缓存的命中率也会比B树高。

3、B+树全节点遍历更快:B+树遍历整棵树只需要遍历所有的叶子节点即可,而不需要像B树一样需要对每一层进行遍历,这有利于数据库做全表扫描。

B树相对于B+树的优点是,如果经常访问的数据离根节点很近,而B树的非叶子节点本身存有关键字和数据,所以在查询这种数据检索的时候会要比B+树快。

B*树

很显然,B树又是对B+数的再一次改进,在B+树的构建过程中,为了保持树的平衡,节点的合并拆分是比较耗费时间的,所以B树就是在如何减少构建中节点合并和拆分的次数,从而提升树的数据插入、删除性能。

B树构建规则
相对于B+树B
的不同之处如下:

(1)首先是关键字个数限制问题,B+树初始化的关键字初始化个数是cei(m/2),b树的初始化个数为(cei(2/3m))

(2)B+树节点满时就会分裂,而B*树节点满时会检查兄弟节点是否满(因为每个节点都有指向兄弟的指针),如果兄弟节点未满则向兄弟节点转移关键字,如果兄弟节点已满,则从当前节点和兄弟节点各拿出1/3的数据创建一个新的节点出来;

B树 与B+树对比
在B+树的基础上因其初始化的容量变大,使得节点空间使用率更高,而又存有兄弟节点的指针,可以向兄弟节点转移关键字的特性使得B
树额分解次数变得更少;

5、 总结

1、相同思想和策略

从平衡二叉树、B树、B+树、B*树总体来看它们的贯彻的思想是相同的,都是采用二分法和数据平衡策略来提升查找数据的速度;

2、不同的方式对树的不断优化

1、首先,为了保证树的节点均匀分布,所以在二叉树的基础上加上了平衡算法,就有了平衡二叉树。

2、为了减少树的高度,所以B树一个节点下面可以添加N个子节点,然后每个节点的大小限制在磁盘块容量大小,让节点只需要通过一次IO就能读取到所有数据,通过增加节点存储的数据减少了树的高度,而节点的数据变多并没有让IO次数变多。

3、B+树在B树的基础上,在查询的稳定性 和排序方面进行了优化,因为B+树所有的数据都会保存到叶子节点,然后所有叶子节点本身是有序的。

4、B*树为了减少 树在构建过程中节点的拆分、合并次数,所以在每个节点上都保存了旁边节点的指针,在节点需要进行拆分、合并时,优先从旁边节点挪数据,从而减少构建过程中节点拆分、合并的次数,提升了树的构建性能。

posted on 2021-06-02 09:37  Ultraman_X  阅读(51)  评论(0编辑  收藏  举报

导航