Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt

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

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Tirgul 6 B-Trees – Another kind of balanced trees Some notes regarding Home Work.
COMP 171 Data Structures and Algorithms Tutorial 9 B-Trees.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
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.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
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.
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.
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.
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,
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
B-Tree.
B+-Tree Deletion Underflow conditions B+ tree Deletion Algorithm
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
COMP261 Lecture 23 B Trees.
B-Trees B-Trees.
B-Tree Michael Tsai 2017/06/06.
Chapter 18: B-Trees Example: M Note: Each leaf has the same depth D H
B-Trees B-Trees.
B-Trees Example: Comp 750, Fall 2009 M Note: Each leaf
Chapter 18 B-Trees Lee, Hsiu-Hui
Extra: B+ Trees CS1: Java Programming Colorado State University
Chapter 11: Multiway Search Trees
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
B+-Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
B+ Tree.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Slide Sources: CLRS “Intro. To Algorithms” book website
(edited by Nadia Al-Ghreimil)
Data Structures and Algorithms
B Tree Adhiraj Goel 1RV07IS004.
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Lecture 26 Multiway Search Trees Chapter 11 of textbook
B-Trees (continued) Analysis of worst-case and average number of disk accesses for an insert. Delete and analysis. Structure for B-tree node.
Slide Sources: CLRS “Intro. To Algorithms” book website
2-3 (or B-) Trees, in lieu of Chapter 18
Data Structures – Week #6
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
CS 583 Analysis of Algorithms
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Ch. 12: Binary Search Trees
B-Tree.
B+-Trees (Part 1).
Other time considerations
B-Tree Presenter: Jun Tao.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CSIT 402 Data Structures II With thanks to TK Prasad
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
(edited by Nadia Al-Ghreimil)
(2,4) Trees (2,4) Trees (2,4) Trees.
CSE 373: Data Structures and Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
B-Trees.
Algorithms and Data Structures Lecture IX
B-Trees.
B-Trees B-trees are characterized in the following way:
Presentation transcript:

Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro. To Algorithms” book website (copyright McGraw Hill) adapted and supplemented

CLRS “Intro. To Algorithms” Ch. 2: B-Trees

Disc rotation speed: 7200 RPM, typical Arm movement: few ms. Total access time for data on disc: 3-9 ms., typical Versus access time in RAM: < 100 ns. B-tree objective: to optimize disc access by using the full amount of information retrieved per disc access, i.e., one page. So: Size of 1 node of the B-tree = 1 page

Definition of B-trees A B-tree T is a rooted tree (root = root[T]) has the properties. Every node x has the following fields n[x], the number of keys currently stored in node x. The n[x] keys themselves, stored in non-decreasing order so that key1[x] ≤ key2[x]≤ … ≤ keyn[x][x] leaf[x], a boolean that is TRUE if x is a leaf and FALSE otherwise. Each internal (=non-leaf) node contains n[x]+1 pointers c1[x], c2[x], …, cn[x]+1[x] to children. Leaf nodes have no such ptrs. The keys keyi[x] separate the ranges of keys stored in each subtree: if ki is any key stored in the subtree with root ci[x] then k1 ≤ key1[x] ≤ k2 ≤ key2[x] ≤ … ≤ keyn[x][x] ≤ kn[x]+1 All leaves have the same depth, which is the tree’s height h. A fixed integer t  2 is called the minimum degree of the B-tree: Every node other than the root must have at least t-1 keys. So, every internal node other than the root has at least t children. If the tree is non-empty the root must have at least one key. Every node can contain at most 2t-1 keys. Therefore, an internal node can have at most 2t children. A node is said to be full if it contains 2t-1 keys.

Th. 18.1: If n  1, then for any n-key B-tree of height h and min degree t  2, h ≤ logt (n+1)/2 ≈ logt n. Proof: At least 2 nodes at depth 1, at least 2t at depth 2, 2t2 at depth 3, …, 2th-1 at depth h. Therefore, n  1 + (t-1) ∑i=1..h 2ti-1 = 1 + 2(t-1)(th -1)/(t-1) = 2th – 1  h ≤ logt (n+1)/2

Searching a B-tree

Creating an empty B-tree

Splitting a Full Node Assumption is that parent is not full. Will be justified!

Inserting into a B-tree

Always want to insert new key into a leaf to avoid having to create new children for an internal node. Because a new key at an internal node means a new child ptr. as well. This is not the case at a leaf.

Deletion Strategy Case 3a Deletion strategy is a mirror-image of insertion. Recall in insertion that before the new key k moves to the next node (in a downward pass starting just before the root), it checks to see if that node is full; if it is full, then the node is first split to make it non-full. This guarantees that when k finally reaches the target leaf node n, then it can be simply inserted because n will not be full. For deletion, as we move downward searching for the key k to be deleted starting at the root, we check if the next node is minimal (= opposite of full = has t – 1 keys). If it is minimal, then the node is first made non-minimal: If there is a non-minimal sibling then take a key and child ptr. from that sibling (if both siblings are non-minimal it doesn’t matter which is chosen) and move them as shown below. (Case 3a) Node x x α x x 1. Move α down from the parent of ci[x] to ci[x]. 2. Move β from the non-minimal sibling of ci[x] up to the parent to take the place of α. 3. Move the child ptr., if there is one, next to β over to ci[x] (note if ci[x] is a leaf, then it and its siblings will contain no ptrs.) Case 3a x x x β x x x ci[x] is minimal Non-minimal sibling Many child ptrs. not drawn to avoid clutter.

Deletion Strategy, cont. If each sibling is minimal, then the node is merged with one sibling (doesn’t matter which) together with the separating key from the parent. (Case 3b) Node x x x α x x Case 3b x x x x x x x x x Left sibling minimal ci[x] is minimal Right sibling minimal Merge Many child ptrs. not drawn to avoid clutter. 1. Copy data (keys/ptrs) from one sibling to ci[x]. 2. Move the separating key α from the parent down (see note) to the median position in ci[x] so that it separates the original keys in ci[x] from the new ones copied in. Note: We can move α down if node x is not the root as x then is guaranteed not minimal; the only exceptional case is when x is the root and α is its only key, in which case the root is deleted and ci[x] becomes the new root and the B-tree becomes shorter. 3. Delete the chosen sibling. Delete the pointer to the chosen sibling. The above merges ci[x] with its sibling and α.

Deletion Strategy, cont. When we reach the node x which actually contains the key k to be deleted there are two possibilities: x is a leaf: simply delete k . By previous step x is guaranteed to be non-minimal so k can indeed be deleted. (Case 1) x is an internal node: we can’t simply delete k even if x is not minimal – we have to replace k with another key to separate the child of x before k and the child after k: If the child y of x that precedes k is non-minimal, then the replacement k’ of k is chosen from the subtree rooted at y. Particularly, k’ is the greatest element of the subtree rooted at y. This replacement k’ is recursively deleted from its original location and put in place of k. (Case 2a) Node x x k x x 1. Delete k’ from its original position. 2. Replace k with k’. Case 2a x x x x x Non-minimal y Subtree rooted at y k’ = largest in subtree

Deletion Strategy, cont. Otherwise, if the child z that follows k is non-minimal, then the replacement k’ of k is chosen from the subtree rooted at z. Particularly, k’ is the smallest element of the subtree rooted at z. This replacement k’ is recursively deleted from its original location and put in place of k. (Case 2b, symmetric to 2a) 1. Delete k’ from its original position. 2. Replace k with k’. Node x x k x x x Case 2b x x x x Non-minimal z Subtree rooted at z k’ = smallest in subtree

Deletion Strategy, cont. Otherwise, if both the child y before k and the child z after k are minimal then merge them with k into one node, so that x loses a child, and then recursively delete k from the new non-minimal node. (Case 2c) Node x x x k x x Case 2c x x x x x x x y is minimal z is minimal Merge Many child ptrs. not drawn to avoid clutter. 1. Copy data (keys/ptrs) from z to y. 2. Move k from the parent down to the median position in y and delete the pointer to z. 3. Delete z. The above merges y with z and k. 4. Delete k from the new y.

3

Problems Ex. 18.1-1 Ex. 18.1-4 Ex. 18.2-1 Ex. 18.2-2 Ex. 18.2-3