Chapter 18: B-Trees Example: M Note: Each leaf has the same depth D H

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

B Tree.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 5 B-Trees.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Tirgul 6 B-Trees – Another kind of balanced trees Some notes regarding Home Work.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
COMP 171 Data Structures and Algorithms Tutorial 9 B-Trees.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
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.
October 10, Algorithms and Data Structures Lecture IX Simonas Šaltenis Nykredit Center for Database Research Aalborg University
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
November 12, Algorithms and Data Structures Lecture IX Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures – Week #6 Special Trees. January 14, 2016Borahan Tümer, Ph.D.2 Outline Adelson-Velskii-Landis (AVL) Trees Splay Trees B-Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Binary Search Trees What is a binary search tree?
B-Tree Michael Tsai 2017/06/06.
Multiway Search Trees Data may not fit into main memory
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
B-Trees Example: Comp 750, Fall 2009 M Note: Each leaf
Chapter 18 B-Trees Lee, Hsiu-Hui
Lecture 16 Multiway Search Trees
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Data Structures and Algorithms
B-Trees -Stephen R. Covey
B Tree Adhiraj Goel 1RV07IS004.
Haim Kaplan and Uri Zwick November 2014
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Lecture 26 Multiway Search Trees Chapter 11 of textbook
Red-Black Trees.
Data Structures – Week #6
CS 583 Analysis of Algorithms
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
CMSC 341 (Data Structures)
Ch. 12: Binary Search Trees Ming-Te Chi
(2,4) Trees (2,4) Trees (2,4) Trees.
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
B-Tree Presenter: Jun Tao.
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?
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
CS 583 Analysis of Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Lecture 7 Algorithm Analysis
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
(2,4) Trees (2,4) Trees (2,4) Trees.
Red-Black Trees.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Algorithms and Data Structures Lecture IX
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
Red-Black Trees CS302 Data Structures
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
B-Trees B-trees are characterized in the following way:
Presentation transcript:

Chapter 18: B-Trees Example: M Note: Each leaf has the same depth D H Q T X B C F G J K L N P R S V W Y Z Node x has the following fields: n[x] -- number of keys stored in x. key1[x]  key2[x]  …  keyn[x][x] -- the n[x] keys. leaf[x] -- Boolean. if internal: n[x] + 1 pointers to children, c1[x], c2[x], …, cn[x]+1[x]. Let ki = any key in subtree rooted at ci[x]. Then, k1  key1[x]  k2  key2[x]  …  keyn[x][x]  kn[x]+1.  t  2 called the minimum degree. x  root  t –1  n[x]  2t–1 x = root  n[x]  2t–1 Comp 750, Fall 2009

Application: Disk Accesses Each node is stored as a page. Page size determines t. t is usually large Implies branching factor is large, so height is small. Example: Holds over one billion keys. Height is only 2, so can find any key with only two disk accesses (compare to red-black trees, where the branching factor is 2). Note: Disk accesses dominate performance in this application. 1001 1000 … 1000 1000 1000 1000 1000 … 1000 Comp 750, Fall 2009

Height of a B-Tree Theorem 18.1: Let n = the number of keys in T, n  1, t  2, h = height of T. Then, Proof: Let T be of height h. The number of nodes is minimized when root has 1 key and all other nodes have t–1 keys. This gives us 2ti-1 nodes at depth i, 1  i  h, and 1 node at depth 0. Hence, Comp 750, Fall 2009

B-Tree Operations Search: Create: Insert and Delete: (logtn) disk accesses. O(t logtn) CPU time. Create: O(1) disk accesses. O(1) CPU time. Insert and Delete: O(logtn) disk accesses. In the code that follows, we use: Disk-Read: To move node from disk to memory. Disk-Write: To move node from memory to disk. We assume root is in memory. Comp 750, Fall 2009

Search Search(x, k) i := 1; while i  n[x] and k > keyi[x] do i := i + 1 od; if i  n[x] and k = keyi[x] then return(x,i) fi; if leaf[x] then return NIL else DiskRead(ci[x]); Search(ci[x]) fi Search(root[T], k) returns (y,i) s.t. keyi[y] = k or NIL if no such key. Worst-case: (logtn) disk reads. (t logtn) CPU time. Comp 750, Fall 2009

Creating an Empty Tree Create(T) x := Allocate-Node(); leaf[x] := true; n[x] := 0; Disk-Write(x); root[T] := x To create a nonempty tree, first create an empty tree, then insert nodes. Splitting is fundamental to insert. Comp 750, Fall 2009

Splitting Applied to a “full” child of a “nonfull” parent. “full”  2t–1 keys. Example: (t=4) keyi [x] keyi [x] keyi-1[x] keyi+1 [x] keyi-1[x] x x Split … N W … … N S W … y = ci[x] y = ci[x] z = ci+1[x] P Q R S T U V P Q R T U V T1 T2 T3 T4 T5 T6 T7 T8 T1 T2 T3 T4 T5 T6 T7 T8 Comp 750, Fall 2009

Split-Child Split-Child(x, i, y) z := Allocate-Node(); leaf[z] := leaf[y]; n[z] := t–1; for j := 1 to t–1 do keyj[z] := keyj+t[y] od; if not leaf[y] then for j := 1 to t do cj[z] := cj+t[y] od fi; n[y] := t–1; for j := n[x] + 1 downto i+1 do cj+1[x] := cj[x] ci+1[x] := z; /* Continued */ for j := n[x] downto i do keyj+1[x] := keyj[x] od; keyi[x] := keyt[y]; n[x] := n[x] + 1; Disk-Write(y); Disk-Write(z); Disk-Write(x) (t) CPU time. O(1) disk writes. Comp 750, Fall 2009

Insert First, modify tree (if necessary) to create room for new key. Insert(T, k) r := root[T]; if n[r] = 2t–1 then s := Allocate-Node(); root[T] := s; leaf[s] := false; n[s] := 0; c1[s] := r; Split-Child(s, 1, r); Insert-Nonfull(s, k) else Insert-Nonfull(r, k) fi First, modify tree (if necessary) to create room for new key. Then, call Insert-Nonfull(). Example: root[T] s H root[T] r r A D F H L N P A D F L N P T1 T2 T3 T4 T5 T6 T7 T8 T1 T2 T3 T4 T5 T6 T7 T8 Comp 750, Fall 2009

Insert-Nonfull Insert-Nonfull(x, k) i := n[x]; if leaf[x] then while i  1 and k < keyi[x] do keyi+1[x] := keyi[x]; i := i–1 od; keyi+1[x] := k; n[x] := n[x] + 1; Disk-Write(x) else /* not leaf[x] */ while i  1 and k < keyi[x] do i := i–1 od; i := i + 1; Disk-Read(ci[x]); if n[ci[x]] = 2t–1 then Split-Child(x, i, ci[x]); if k > keyi[x] then i := i + 1 fi fi; Insert-Nonfull(ci[x], k) Worst Case: (t logtn) CPU time. (logtn) disk writes. Comp 750, Fall 2009

Insert Example t = 3 G M P X A C D E J K N O R S T U V Y Z Insert B A B C D E J K N O R S T U V Y Z Comp 750, Fall 2009

Insert Example (Continued) G M P X A B C D E J K N O R S T U V Y Z Insert Q G M P T X A B C D E J K N O Q R S U V Y Z Comp 750, Fall 2009

Insert Example (Continued) G M P T X A B C D E J K N O Q R S U V Y Z Insert L P G M T X A B C D E J K L N O Q R S U V Y Z Comp 750, Fall 2009

Insert Example (Continued) G M T X A B C D E J K L N O Q R S U V Y Z Insert F P C G M T X A B D E F J K L N O Q R S U V Y Z Comp 750, Fall 2009

Deletion Main Idea: Recursively descend tree. Ensure any non-root node x that is considered has at least t keys. May have to move key down from parent. Comp 750, Fall 2009

Deletion Cases Case 0: Empty root -- make root’s only child the new root. x c1[x] Case 1: k in x, x is a leaf -- delete k from x. x x leaf leaf … k … … …  t keys  t–1 keys Comp 750, Fall 2009

Deletion Cases (Continued) Case 2: k in x, x internal. x not a leaf … k … y z Subcase A: y has at least t keys -- find predecessor k´ of k in subtree rooted at y, recursively delete k´, replace k by k´ in x. x x not a leaf … k … … k´… y y  t keys  t keys pred of k k´ Comp 750, Fall 2009

Deletion Cases (Continued) Subcase B: z has at least t keys -- find successor k´ of k in subtree rooted at z, recursively delete k´, replace k by k´ in x. x x not a leaf … k … … k´… z z  t keys  t keys succ of k k´ Subcase C: y and z both have t–1 keys -- merge k and z into y, free z, recursively delete k from y. x x not a leaf not a leaf … k … … … y z y y’s keys, k, z’s keys t–1 keys t–1 keys 2t–1 keys Comp 750, Fall 2009

Deletion Cases (Continued) Case 3: k not in internal node. Let ci[x] be the root of the subtree that must contain k, if k is in the tree. If ci[x] has at least t keys, then recursively descend; otherwise, execute 3.A and 3.B as necessary. Subcase A: ci[x] has t–1 keys, some sibling has at least t keys. recursively descend x x not a leaf … … k1 … … k2 ci[x] ci[x] … k2 k1 … t–1 keys t keys k k Comp 750, Fall 2009

Deletion Cases (Continued) Subcase B: ci[x] and sibling both have t–1 keys. recursively descend x x not a leaf … … … … k1 ci[x] ci[x] ci+1[x] ci[x]’s keys, , ci+1[x]’s keys k1 t–1 keys t–1 keys 2t–1 keys k k Comp 750, Fall 2009

Delete Example P t = 3 C G M T X A B D E F J K L N O Q R S U V Y Z Delete F (Case 1) P C G M T X A B D E J K L N O Q R S U V Y Z Comp 750, Fall 2009

Delete Example (Continued) C G M T X A B D E J K L N O Q R S U V Y Z Delete M (Case 2.A) P C G L T X A B D E J K N O Q R S U V Y Z Comp 750, Fall 2009

Delete Example (Continued) C G L T X A B D E J K N O Q R S U V Y Z Delete G (Case 2.C) P C L T X A B D E J K N O Q R S U V Y Z Comp 750, Fall 2009

Delete Example (Continued) C L T X A B D E J K N O Q R S U V Y Z Delete D (Case 3.B) C L P T X A B E J K N O Q R S U V Y Z Comp 750, Fall 2009

Delete Example (Continued) C L P T X A B E J K N O Q R S U V Y Z Case 0 C L P T X A B E J K N O Q R S U V Y Z Comp 750, Fall 2009

Delete Example (Continued) C L P T X A B E J K N O Q R S U V Y Z Delete B (Case 3.A) E L P T X A C J K N O Q R S U V Y Z Comp 750, Fall 2009