B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Slides:



Advertisements
Similar presentations
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
Advertisements

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
B-Trees. Motivation for B-Trees Index structures for large datasets cannot be stored in main memory Storing it on disk requires different approach to.
CPSC 231 B-Trees (D.H.)1 LEARNING OBJECTIVES Problems with simple indexing. Multilevel indexing: B-Tree. –B-Tree creation: insertion and deletion of nodes.
Other time considerations Source: Simon Garrett Modifications by Evan Korth.
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?
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
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.
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.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
B-Trees Chapter 9. Limitations of binary search Though faster than sequential search, binary search still requires an unacceptable number of accesses.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
B-Trees and B+-Trees Disk Storage What is a multiway tree?
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
CS4432: Database Systems II
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
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.
More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store.
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,
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
1 B-Trees & (a,b)-Trees CS 6310: Advanced Data Structures Western Michigan University Presented by: Lawrence Kalisz.
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
B-Trees And B+-Trees Jay Yim CS 157B Dr. Lee.
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.
B-Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
Comp 335 File Structures B - Trees. Introduction Simple indexes provided a way to directly access a record in an entry sequenced file thereby decreasing.
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.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Marwan Al-Namari Hassan Al-Mathami. Indexing What is Indexing? Indexing is a mechanisms. Why we need to use Indexing? We used indexing to speed up access.
1 Multi-Level Indexing and B-Trees. 2 Statement of the Problem When indexes grow too large they have to be stored on secondary storage. However, there.
 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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
Internal and External Sorting External Searching
COMP261 Lecture 23 B Trees.
B-Trees B-Trees.
B-Trees B-Trees.
B-Trees B-Trees.
B+-Trees.
B+-Trees.
B+-Trees.
B+ Tree.
Trees 4 The B-Tree Section 4.7
B-Trees.
B+-Trees (Part 1).
Other time considerations
B-Trees.
CSIT 402 Data Structures II With thanks to TK Prasad
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
B-Trees.
B-Trees.
Heaps & Multi-way Search Trees
Presentation transcript:

B-TREE

Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t fit? We will have to use disk storage but when this happens our time complexity fails The problem is that Big-Oh analysis assumes that all operations take roughly equal time This is not the case when disk access is involved

Problem with Big `O’ notation Big ‘O’ assumes that all operations take equal time Suppose all data does not fit in memory Then some part of data may be stored on hard disk. CPU speed is in millions of instructions per second  3GHz machines common now Equals roughly 3000 million instructions per seconds Typical disk speeds about 7,200 RPM  Roughly 120 disk accesses per second So accessing disk is incredibly expensive. So we may be willing to do more computation to organize our data better and make fewer disk accesses.

Problem with binary trees There is no guarantee that binary trees will be balanced If stored on disk, we have potentially O(N) disk operations

M-ary Trees Allows up to M children for each node  Instead of max. 2 for binary trees A complete M -ary tree of N nodes has a depth of log M N Example of complete 5-ary tree of 31 nodes

M -ary search tree Similar to binary search tree, except that  Each node has (M-1) keys to decide which of the M branches to follow. Larger M  smaller tree depth But how to make M -ary tree balanced?

B-tree Yet another technique of making a search tree balanced. It is not a binary search tree There are many new ideas. B-tree is used in organising objects in files and it is a part of file structure You will read in detail the application of B- tree in your Database course.

B-tree B-tree is developed with many new ideas.

Let’s see the BST How do you start building the binary search tree for this set of objects? First step to decide who should be the root node? Then decide the root of the left subtree and root of right subtree and soon.

Top-down vs. bottom up Can we think of generating a search tree that is bottom-up. That is, we first decide the leaf-nodes and gradually make the process evolve the root node and subtrees.

Formal Definition Structure of a node  A node contains some keys and some pointers.  Number of pointers in a node is one more than the number of keys.  pointers point to descendants.  The keys are sorted in non-decreasing order.

A node key1key2key3key4key5 This is full node. key1key2key3

Formal Definition B-tree of order m  Every node has a maximum of m child-nodes  A node, other than the root, contains at least  m/2  -1 keys and no more than m-1 keys.  Every node, except the root and the leaves, has at least  m/2  child nodes.  The root has atleast 2 child nodes  All leaf nodes are on the same level.  A nonleaf node with k descendents contains k-1 keys

Let’s build a B-tree

Let’s build a B-tree

Let’s build a B-tree

Now the node is full

24 comes in

Let’s build a B-tree

Let’s build a B-tree

Searching a B-tree By comparing the key we decide which internal node to reach. At each internal node, use a linear search to decide the next descendant.

Constructing a B-tree Add 25 to the tree Exceeds Order. Promote middle and split.

Constructing a B-tree (contd.) 6, 14, 28 get added to the leaf nodes:

Constructing a B-tree (contd.) Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf

Constructing a B-tree (contd.) 7, 52, 16, 48 get added to the leaf nodes

Constructing a B-tree (contd.) Adding 68 causes us to split the right most leaf, promoting 48 to the root

Constructing a B-tree (contd.) Adding 3 causes us to split the left most leaf

Constructing a B-tree (contd.) Add 26, 29, 53, 55 then go into the leaves

Constructing a B-tree (contd.) Add 45 increases the trees level Exceeds Order. Promote middle and split.

Inserting into a B-Tree Attempt to insert the new key into a leaf If this would result in that leaf becoming too big, split the leaf into two, promoting the median key to the leaf’s parent If this would result in the parent becoming too big, split the parent into two, promoting the middle key This strategy might have to be repeated all the way to the top If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher

Delete from a B-tree During insertion, the key always goes into a leaf. For deletion we wish to remove from a leaf. There are three possible ways we can do this:  If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted.  If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non- leaf deleted key’s position.

Delete from a B-tree (2) If a leaf node containing less than the minimum number of keys  CONCATENATE if one of them has more than the min’ number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf  REDISTRIBUTE if neither of them has more than the min’ number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required

Simple leaf deletion Simple leaf deletion Delete 2: Since there are enough keys in the node, just delete it Assuming a 5-way B-Tree, as before...

Simple non-leaf deletion Delete 52 Borrow the predecessor or (in this case) successor 56

Too few keys in node and its siblings Too few keys in node and its siblings Delete 72 Too few keys! Join back together

Too few keys in node and its siblings Too few keys in node and its siblings

Enough siblings Enough siblings Delete 22 Demote root key and promote leaf key

Enough siblings Enough siblings

Analysis of B-Trees The maximum number of items in a B-tree of order m and height h: rootm – 1 level 1m(m – 1) level 2m 2 (m – 1)... level hm h (m – 1) m h+1 – 1So, the total number of items is (1 + m + m 2 + m 3 + … + m h )(m – 1) = [(m h+1 – 1)/ (m – 1)] (m – 1) = m h+1 – 1 When m = 5 and h = 2 this gives 5 3 – 1 = 124

Reasons for using B-Trees When searching tables held on disc, the cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferred  If we use a B-tree of order 101, say, we can transfer each node in one disc read operation  A B-tree of order 101 and height 3 can hold – 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory) If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys)  B-Trees are always balanced (since the leaves are all at the same level), so 2-3 trees make a good type of balanced tree