220 likes | 226 Views
Learn about B-Trees, their properties, operations like searching and inserting keys, and examples of tree splitting and key deletion.
E N D
Chapter 18 B-Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various materials from the web. Hsiu-Hui Lee
在 B-Tree 中若 node x 有 n[x] keys, 則 x 含有 n[x]+1 個 children. M • B-Tree 性質: D H Q T X B C J K L F G N P R S Y Z V W Hsiu-Hui Lee
Main Memory Secondary Memory ( RAM ) ( disks ) • 典型之 B-Tree 運用情況: 資料量龐大無法全部存在 main memory 處理 B-Tree 之 algorithm 只將部分之資料 copy 至 main memory. Hsiu-Hui Lee
x key1 key2 C3[x] C1[x] C2[x] K2 K3 K1 Definition of B-trees B-Tree T : is a rooted tree with the properties: 1. Every node x has the following fields: a. n[x]:# of keys currently stored in node x b. c. leaf[x] is true if x is a leaf; false if x is an internal node. 2. Each internal node x contains n[x]+1 pointers C1[x] , C2[x] ,… , Cn[x]+1[x] to its children. Leaf nodes have no children ( i.e. its Ci[x] undefined). 3. If kiis any key stored in the subtree with root Ci[x] Hsiu-Hui Lee
All leaves have the same depth, which is the tree’s height h. 5. t: minimum degree of the B-tree. Every node (other than root) must have at least t-1 keys. (have at least t children) Every node can contain at most 2t-1 keys. (have at most 2t children) A node is FULL, if it contains exactly 2t-1 keys. 2-3-4 tree: When t=2, every internal node has either 2, 3, or 4 children Hsiu-Hui Lee
Theorem 18.1 If , then for any n-key B-tree T of height h and minimum degree • The larger the value of t, the smaller the height of the B-tree. Hsiu-Hui Lee
# of nodes 1 1 Proof: 2 t-1 t-1 t t 2t t-1 t-1 t-1 t-1 t t t t 2t2 Hsiu-Hui Lee
Basic operations on B-trees • convention: • Root of the B-tree is always in main memory. • Any nodes that are passed as parameters must already have had a DISK_READ operation performed on them. • Operations: • Searching a B-Tree. • Creating an empty B-tree. • Splitting a node in a B-tree. • Inserting a key into a B-tree. • Deleting a key from a B-tree. Hsiu-Hui Lee
Total CPU time B-Tree-Search(x,k) Hsiu-Hui Lee
Ex. B-Tree-Search(x,R) Hsiu-Hui Lee
Creating an empty B-tree Total CPU time Hsiu-Hui Lee
full Splitting a node Splitting a full node y ( have 2t-1 keys ) around its median key into 2 nodes having (t-1) keys each. Hsiu-Hui Lee
B-Tree-Split-Child(x, i, y) Hsiu-Hui Lee
Insert a key in a B-Tree Hsiu-Hui Lee
Splitting the root is the only way to increase the height of a B-tree. • Unlike a binary search tree, a B-tree increases in height at the top instead of at the bottom. Hsiu-Hui Lee
(a) Initial tree G M P X A C D E J K N O R S T U V Y Z (b) B inserted G M P X G M P T X A B C D E J K N O R S T U V Y Z R S U V (c) Q inserted G M P T X A B C D E J K N O Q R S U V Y Z • Example:Inserting keys into a B-Tree. t=3 Hsiu-Hui Lee
P G M T X (d) L insert A B C D E J K L N O Q R S U V Y Z P (e) F insert C G M T X A B J K L N O Q R S U V Y Z D E F Hsiu-Hui Lee
( x has t keys ) x K delete k from x. • Deleting a key from a B-Tree: 1. K is in x and x is a leaf: 2. K is in x and x is an internal node: a. b. c. if both y,z has t-1 keys. Merge y,z and k into y. Recursively delete k from y. x Recursively delete k’ and replace k by k’ in x. K y k’ x K z k’ x K z y t-1 t-1 Hsiu-Hui Lee 2t-1 K
x Ci[x] k is in this subtree. 3. If K is not in internal node x: a. If Ci[x] has only t-1 keys but has a sibling with t keys b. If Ci[x] and all of Ci[x]’s siblings have t-1 keys, merge ci with one sibling. x • Move a key from x down to Ci[x]. • Move a key from Ci[x]’s sibling to x. • Move an appropriate child to Ci[x] from its sibling. Ci[x] t-1 t x 0 Ci[x] t-1 keys t-1 keys 2t-1 keys 0
t=3 (a) Initial tree P C G M T X • Example:Deleting a key from a B-Tree. A B J K L N O Q R S U V Y Z D E F (b) F delete:case 1 P C G M T X A B J K L N O Q R S U V Y Z D E (c) M delete:case 2a P C G L T X A B J K N O Q R S U V Y Z D E
(d) G deleted:case 2c P C L T X A B N O D E J H Q R S U V Y Z (e) D deleted:case 3b C L P T X N O Q R S U V Y Z A B E J K (e’) tree shrinks in height C L P T X N O Q R S U V Y Z A B E J K (f) B delete:case 3a E L P T X N O Q R S U V Y Z A C J K