B-Tree.

Slides:



Advertisements
Similar presentations
B-Trees. Motivation When data is too large to fit in the main memory, then the number of disk accesses becomes important. A disk access is unbelievably.
Advertisements

Chapter 4: Trees Part II - AVL Tree
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
They’re not just binary anymore!
Tirgul 6 B-Trees – Another kind of balanced trees Some notes regarding Home Work.
1 B trees Nodes have more than 2 children Each internal node has between k and 2k children and between k-1 and 2k-1 keys A leaf has between k-1 and 2k-1.
B-Tree. p2. B-Tree : 性質: 典型之 B-Tree 運用情況: 在 B-Tree 中若 node x 有 x.n keys, 則 x 含有 x.n+1 個 children. M D H J K L F G B C Q T X Y Z V W R S N P Main Memory.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
Chapter 10 m-way 搜尋樹與B-Tree
1 B-Trees Disk Storage What is a multiway tree? What is a B-tree? Why B-trees? Comparing B-trees and AVL-trees Searching a B-tree Insertion in a B-tree.
1 Database indices Database Systems manage very large amounts of data. –Examples: student database for NWU Social Security database To facilitate queries,
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
B-Trees and B+-Trees Disk Storage What is a multiway tree?
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
Tirgul 6 B-Trees – Another kind of balanced trees.
Splay Trees and B-Trees
CSCE 3110 Data Structures & Algorithm Analysis
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
ICS 220 – Data Structures and Algorithms Week 7 Dr. Ken Cosh.
Spring 2006 Copyright (c) All rights reserved Leonard Wesley0 B-Trees CMPE126 Data Structures.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
Storage CMSC 461 Michael Wilson. Database storage  At some point, database information must be stored in some format  It’d be impossible to store hundreds.
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 6.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
CSE373: Data Structures & Algorithms Lecture 15: B-Trees Linda Shapiro Winter 2015.
October 10, Algorithms and Data Structures Lecture IX Simonas Šaltenis Nykredit Center for Database Research Aalborg University
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
COSC 2007 Data Structures II Chapter 15 External Methods.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
CompSci 100E 39.1 Memory Model  For this course: Assume Uniform Access Time  All elements in an array accessible with same time cost  Reality is somewhat.
November 12, Algorithms and Data Structures Lecture IX Simonas Šaltenis Nykredit Center for Database Research Aalborg University
B-trees Eduardo Laber David Sotelo. What are B-trees? Balanced search trees designed for secondary storage devices Similar to AVL-trees but better at.
B-Trees ( Rizwan Rehman) Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
CompSci Memory Model  For this course: Assume Uniform Access Time  All elements in an array accessible with same time cost  Reality is somewhat.
 B-tree is a specialized multiway tree designed especially for use on disk  B-Tree consists of a root node, branch nodes and leaf nodes containing the.
Data Structures – Week #6 Special Trees. January 14, 2016Borahan Tümer, Ph.D.2 Outline Adelson-Velskii-Landis (AVL) Trees Splay Trees B-Trees.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Internal and External Sorting External Searching
October 24, 2005L11.1 Introduction to Algorithms LECTURE (Chapter 18) B-Trees ‧ 18.1 Definition of B-Tree ‧ 18.2 Basic operations on B-Tree ‧ 18.3 Deleting.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
B-Trees Katherine Gurdziel 252a-ba. Outline What are b-trees? How does the algorithm work? –Insertion –Deletion Complexity What are b-trees used for?
More Trees. Outline Tree B-Tree 2-3 Tree Tree Red-Black Tree.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
B-Tree Michael Tsai 2017/06/06.
Chapter 18 B-Trees Lee, Hsiu-Hui
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
B Tree Adhiraj Goel 1RV07IS004.
Lecture 26 Multiway Search Trees Chapter 11 of textbook
B-Tree Presenter: Jun Tao.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Algorithms and Data Structures Lecture IX
Presentation transcript:

B-Tree

Multiway search tree A tree was defined as either an empty structure or a structure whose children are disjoint tree t1,t2,…tk. Each node of this kind of tree can have more than two children. This tree called a multiway tree of order m, or an m-way tree. A Multiway search tree of order m, or an m-way search tree, is a Multiway search tree in which Each node has m children and m-1 keys The keys in each node are in ascending order The keys in the first i children are smaller the ith key The keys in the last m-i children are larger than the ith key M-way search tree  m-way tree Binary search tree  binary tree

Introduction of B-tree B-tree: proposed by Bayer and McCreight 1972 A B-tree operates closely with secondary storage and can be tuned to reduce the impediments imposed by this storage One important property of B-trees is the size of each node which can be made as large as the size of the block. (the basic unit of I/O operations associated with a disk is a block) a B-tree of order t is a multiway search tree.

B-Tree : 性質: 典型之 B-Tree 運用情況: 在 B-Tree 中若 node x 有 n[x] keys, 則 x 含有 n[x]+1 個 children. M D H Q T X B C F G J K L N P R S V W Y Z Main Memory Secondary ( disks ) ( RAM ) 資料量龐大無法全部存在 main memory. 處理 B-Tree 之 algorithm 只將部分之資料 copy 至 main memory.

B-Tree T 之定義: T:rooted tree 並滿足下列: 1. 對每一 node x 含下列之 fields a. n[x]:node x 所含之 key 數. b. 依遞增排列. c. leaf[x] 為 true 若 x 為 leaf false 若 x 不為 leaf. 2. 若 x 為一 internal node 則 x 有 n[x]+1 個 pointers C1[x] , C2[x] ,… , Cn[x]+1[x] 指向某 children. 若 x 為一 leaf. 其 Ci[x] 沒有定義. 3.若 ki 為 Ci[x] 所指之 subtree 中任一 key 則 key1 key2 K3 K2 K1 C1[x] C2[x] C3[x] x

4. 每一 leaf 之高度相同.(all leaves are on the same level.) 5. t:minimum degree of the B-tree. (a B-tree of order 2t) a. node( 除 root ) 有 internal node ( 除 root ) 有 若 則 root 至少有一 key. b. node( 至多 ) 有 a node is FULL, if it contains exactly 2t-1 keys.

Thm: If , then for any n-key B-tree T of height h and minimum degree Proof: 1 1 2 t-1 t-1 t t 2t t-1 t-1 t-1 t-1 t t t t 2t2

A B-tree is not a binary tree because B-tree has many more than two children B-trees may be formulated to store a set of elements or a bag of elements. (a given elements can occur many times in a bag but only once in a set) A B-tree is balanced. Every leaf in a B-tree has the same depth 2-3-4 tree (discussed by Rudolf Bayer): a B-tree of order 4 (min degree=2)

The elements in a B-tree node Rule 1:the root may have as few as one elements (or even no elements if it also has no children); every other node has at least minimum elements Rule 2: the maximum number of elements in a node is twice the value of minimum Rule 3: the elements of each B-tree node are stored in a partially filled array, sorted from the smallest elements (at index 0) to the largest elements (at the final used position of the array) Rule 4: the number of subtrees below a nonleaf node is always one more than the number of the elements in the node. Rule 5: for any leaf node: (1) an element at index i is greater than all the elements in subtree number i of the node, and (b) an element at index i is less than all the elements in subtree number i+1 of the node.

66 88 Each element in subtree number 0 is less than 66 66 88 Subtree number 0 number 1 number 2 Each element in subtree number 0 is less than 66 Each element in subtree number 1 is between 66 and 88. Each element in subtree number 2 is greater than 88

convention: Operations: 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.

B-Tree-Search(x,k): Algorithm: Total CPU time: B-Tree-Search(x,k) { }

B-Tree-Created(T): Algorithm: time: B-Tree-Create(T) { }

B-Tree-Split-Child(x,i,y): Splitting a node in a B-Tree: Keyi-1[x] x Keyi [x] t=4 … N S W … … N W … y=Ci[x] z=Ci+1[x] y=Ci[x] P Q R T1 T2 T4 T3 T U V T5 T6 T8 T7 P Q R S T U V full T1 T2 T3 T4 T5 T6 T7 T8 Splitting a full node y ( have 2t-1 keys ) around its median key keyt[y] into 2 nodes having (t-1) keys each.

Algorithm: B-Tree-Split-Child(x,I,y) { }

B-Tree-Insert(T,k): Insert a key in a B-Tree: Root[T] t=4 S Root[T] H A D F H L N P A D F T1 T2 T4 T3 L N P T5 T6 T8 T7 T1 T2 T3 T4 T5 T6 T7 T8

Algorithm: B-Tree-Insert(T,k) { { B-Tree-Split-Child(S,l,r) B-Tree-Insert-Nonfull(S,k) } }

B-Tree-Insert-Nonfull(x,k): Algorithm: B-Tree-Insert-Nonfull(x,k) { { { B-Tree-Insert-Nonfull(Ci[x],k) } }

Example:Inserting keys into a B-Tree. (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

(d) L insert (e) F insert P G M T X A B C D E J K L N O Q R S U V Y Z C G M T X A B D E F J K L N O Q R S U V Y Z

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 has t keys ) x K delete k from x. x K Recursively delete k’ and replace k by k’ in x. y k’ x K z k’ x K z y t+1 t+1 2t-1 K

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 K Ci[x] k is in this subtree. x Move a key from x down to Ci[x]. Move a key from Ci[x]’s sibling to x. Move an appropiate child to Ci[x] from its sibling. K Ci[x] t-1 t x Ci[x] t-1 keys t-1 keys 2t-1 keys

Example:Deleting a key from a B-Tree. (a) Initial tree P C G M T X A B D E F J K L N O Q R S U V Y Z (b) F delete:case 1 P C G M T X A B D E J K L N O Q R S U V Y Z (c) M delete:case 2a P C G L T X A B D E J K N O Q R S U V Y Z

(e’) tree shrinks in height (d) G deleted:case 2c P C L T X A B D E J H N O Q R S U V Y Z (e) D deleted:case 3b C L P T X A B E J K N O Q R S U V Y Z (e’) tree shrinks in height C L P T X A B E J K N O Q R S U V Y Z (f) B delete:case 3a E L P T X A C J K N O Q R S U V Y Z