DB_Algorithm_and_Data_Structure_About_BTree

32
数数数数数数数 ——B 树树树 MySQL DBA 数数数

description

数据库算法与数据结构系列(一)——B树相关

Transcript of DB_Algorithm_and_Data_Structure_About_BTree

Page 1: DB_Algorithm_and_Data_Structure_About_BTree

数据结构与算法——B树相关

MySQL DBA 彭立勋

Page 2: DB_Algorithm_and_Data_Structure_About_BTree

• 树的基本概念• 查找树的基本操作• 二叉搜索树• 平衡二叉树• 平衡树算法 (AVL/Treep/Splay)• 红黑树• 线段树• B树原型• B+树

将要介绍什么?

Page 3: DB_Algorithm_and_Data_Structure_About_BTree

树的基本概念

• 树是 n(n>=0)个结点的有限集。• 在任意一棵非空树中:• (1)有且仅有一个特定的称为根的结点;• (2)当 n>1时 ,其余结点可分为 m(m>0)个互不相交的有限集 T1,T2,...Tm,其中每一个集合本身又是一棵树 ,并且称为根的子树 .

• 补充:从图来定义,树是一个无环的连通无向图。

Page 4: DB_Algorithm_and_Data_Structure_About_BTree

查找树的基本操作( 1)

• SEARCH(S,k):返回有限集 S中一个关键字为 k的元素的指针 x。即 key[x]=k。

• INSERT(S,x):将由 x指向的元素添加到 S。• DELETE(S,x):将由 x指向的元素从 S中删除。

• MINIMUM(S):返回全序集 S中具有最小关键字的元素指针。

• MAXIMUM(S):返回全序集 S中具有最大关键字的元素指针。

Page 5: DB_Algorithm_and_Data_Structure_About_BTree

查找树的基本操作( 2)

• SUCCESSOR(S,x)(后继):返回全序集 S中比 x大的下一个元素的指针。

• PREDECESSOR(S,x)(前驱):返回全序集 S中比 x小的上一个元素的指针。

Page 6: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 定义:任取一颗子树,根节点都比左节点大(小),比右结点小(大)。递归定义

Page 7: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 搜索二叉查找树(复杂度 O(LogN))• TREE-SEARCH (x, k)• 1 if x= NIL or k = key[x]• 2 then return x• 3 if k < key[x]• 4 then return TREE-SEARCH(left[x], k)• 5 else return TREE-SEARCH(right[x], k)

Page 8: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 搜索的例子

Page 9: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 获取最大 /最小关键字元素(复杂度O(LogN))

• TREE-MINIMUM (x) /*TREE-MAXIMUM(x)*/• 1 while left[x] ≠ NIL /* while right[x] ≠ NIL*/• 2 do x ← left[x] /* do x ← right[x]*/• 3 return x

Page 10: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 获取前驱 /后继(复杂度 O(LogN))• TREE-SUCCESSOR(x)• 1 if right[x] ≠ NIL• 2 then return TREE-MINIMUM (right[x])• 3 y ← p[x]• 4 while y ≠ NIL and x = right[y]• 5 do x ← y• 6 y ← p[y]• 7 return y

Page 11: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 插入元素(复杂度 O(LogN))• TREE-INSERT(T, z)• 1 y ← NIL• 2 x ← root[T]• 3 while x ≠ NIL• 4 do y ← x• 5 if key[z] < key[x]• 6 then x ← left[x]• 7 else x ← right[x]• 8 p[z] ← y• 9 if y = NIL• 10 then root[T] ← z Tree T was empty⊹• 11 else if key[z] < key[y]• 12 then left[y] ← z• 13 else right[y] ← z

Page 12: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 插入操作的例子

Page 13: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 删除元素(复杂度 O(LogN))• TREE-DELETE(T, z)• 1 if left[z] = NIL or right[z] = NIL• 2 then y ← z• 3 else y ← TREE-SUCCESSOR(z)• 4 if left[y] ≠ NIL• 5 then x ← left[y]• 6 else x ← right[y]• 7 if x ≠ NIL• 8 then p[x] ← p[y]• 9 if p[y] = NIL• 10 then root[T] ← x• 11 else if y = left[p[y]]• 12 then left[p[y]] ← x• 13 else right[p[y]] ← x• 14 if y ≠ z• 15 then key[z] ← key[y]• 16 copy y's satellite data into z• 17 return y

Page 14: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 删除操作的例子

Page 15: DB_Algorithm_and_Data_Structure_About_BTree

二叉查找树

• 可能出现的特例:偏树• 算法蜕化: O(LogN)->O(N)

Page 16: DB_Algorithm_and_Data_Structure_About_BTree

平衡二叉树

• 平衡二叉树本质上是一颗二叉查找树,但是它是严格平衡的,即任一结点的左右子树高度差不超过 1.

Page 17: DB_Algorithm_and_Data_Structure_About_BTree

平衡二叉树

• 为什么要平衡

随机生成的二叉查找树 经过多次插入 /删除操作之后

Page 18: DB_Algorithm_and_Data_Structure_About_BTree

平衡二叉树

• 为什么要任一结点的左右子树高度差不超过 1而不只是根节点

Page 19: DB_Algorithm_and_Data_Structure_About_BTree

平衡树算法 (AVL)

• AVL树,严格平衡的二叉树旋转算法

Page 20: DB_Algorithm_and_Data_Structure_About_BTree

平衡树算法 (AVL)

• 单旋转(左旋 /右旋)

Page 21: DB_Algorithm_and_Data_Structure_About_BTree

平衡树算法 (AVL)

• 双旋转(左右旋,右左旋)

Page 22: DB_Algorithm_and_Data_Structure_About_BTree

平衡树算法 (其他 )

• Splay:伸展树,也是利用旋转的方法,但是只做单旋转。

• Treep:为每个节点附加一个随机的优先级,规定优先级必须满足堆的性质的前提下对关键字执行基本二叉查找树的算法。

Page 23: DB_Algorithm_and_Data_Structure_About_BTree

红黑树

• 红黑树是一种二叉查找树,但在每个节点上增加一个存储位表示结点的颜色,可以使 RED/BLACK。通过对任何一条从根节点到叶子的路径上各个着色方式的限制,红黑树确保没有一条路径会比其他路径长出两倍,因而是接近平衡的。

Page 24: DB_Algorithm_and_Data_Structure_About_BTree
Page 25: DB_Algorithm_and_Data_Structure_About_BTree

线段树

• 使用区间作为节点构成的查找树• 适用于区间统计计算和动态统计

Page 26: DB_Algorithm_and_Data_Structure_About_BTree

B树原型

• B树与红黑树的不同在于, B树的节点可以有许多子节点,所以 B树的分支因子可能很大。

Page 27: DB_Algorithm_and_Data_Structure_About_BTree

B树原型

• 分裂操作:当节点不能再增加元素时,节点会分裂成两个节点

Page 28: DB_Algorithm_and_Data_Structure_About_BTree

插入操作

Page 29: DB_Algorithm_and_Data_Structure_About_BTree

删除操作

Page 30: DB_Algorithm_and_Data_Structure_About_BTree

B+树

• 在叶子节点上添加指向下一个节点的指针• 非叶子节点不保存实际关键字信息

Page 31: DB_Algorithm_and_Data_Structure_About_BTree

数据库算法与数据结构系列

• B树相关• 排序相关• 锁相关• 缓存相关• *操作系统相关 (调度模型 /段页管理 )• *人工智能相关 (遗传算法 / 神经网络 )

Page 32: DB_Algorithm_and_Data_Structure_About_BTree

数据库原理系列

• 笛卡尔积与集合理论• 数据库范式理论• 分布式事务理论