Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Similar documents
Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

CSE 530A. B+ Trees. Washington University Fall 2013

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is?

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

B-Trees & its Variants

Balanced Search Trees

CSCI Trees. Mark Redekopp David Kempe

Chapter 20: Binary Trees

Data Structures and Algorithms

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree

B-Trees. Introduction. Definitions

Multi-way Search Trees

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

CSIT5300: Advanced Database Systems

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key and satisfies the conditions:

CS127: B-Trees. B-Trees

Lecture 3: B-Trees. October Lecture 3: B-Trees

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Algorithms. Deleting from Red-Black Trees B-Trees

Physical Level of Databases: B+-Trees

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

Motivation for B-Trees

B-trees. It also makes sense to have data structures that use the minimum addressable unit as their base node size.

(2,4) Trees. 2/22/2006 (2,4) Trees 1

amiri advanced databases '05

CS F-11 B-Trees 1

Multiway Search Trees

Background: disk access vs. main memory access (1/2)

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Laboratory Module X B TREES

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CS102 Binary Search Trees

B-Trees. Based on materials by D. Frey and T. Anastasio

Main Memory and the CPU Cache

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Material You Need to Know

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1

Problem. Indexing with B-trees. Indexing. Primary Key Indexing. B-trees: Example. B-trees. primary key indexing

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives.

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Chapter 17 Indexing Structures for Files and Physical Database Design

Design and Analysis of Algorithms Lecture- 9: B- Trees

CMPS 2200 Fall 2017 B-trees Carola Wenk

Chapter 12: Indexing and Hashing (Cnt(

B-Trees and External Memory

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

void insert( Type const & ) void push_front( Type const & )

B-Trees and External Memory

IX. Binary Trees (Chapter 10)

CS301 - Data Structures Glossary By

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 12: Indexing and Hashing. Basic Concepts

CS Fall 2010 B-trees Carola Wenk

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Tree-Structured Indexes

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Chapter 12: Indexing and Hashing

Tree-Structured Indexes

Database Systems. File Organization-2. A.R. Hurson 323 CS Building

Tree-Structured Indexes

Trees, Part 1: Unbalanced Trees

Laboratory Module Trees

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CSC 261/461 Database Systems Lecture 17. Fall 2017

18.3 Deleting a key from a B-tree

V Advanced Data Structures

Data Structures Question Bank Multiple Choice

CMSC 341 Lecture 15 Leftist Heaps

Intro to DB CHAPTER 12 INDEXING & HASHING

Introduction. Choice orthogonal to indexing technique used to locate entries K.

CMSC 341 Leftist Heaps

Chapter 11: Indexing and Hashing

Spring 2018 Mentoring 8: March 14, Binary Trees

CMSC 341 Lecture 15 Leftist Heaps

Binary Trees

In the previous presentation, Erik Sintorn presented methods for practically constructing a DAG structure from a voxel data set.

Unit III - Tree TREES

Trees, Binary Trees, and Binary Search Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

V Advanced Data Structures

UNIT III BALANCED SEARCH TREES AND INDEXING

[ DATA STRUCTURES ] Fig. (1) : A Tree

Chapter 12: Indexing and Hashing

THE B+ TREE INDEX. CS 564- Spring ACKs: Jignesh Patel, AnHai Doan

Tree-Structured Indexes

CSCI2100B Data Structures Heaps

Advanced Set Representation Methods

Algorithms. AVL Tree

Transcription:

Large Trees 1 Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Trees can also be used to store indices of the collection of records in a file. In either case, if the collection of records is quite large, the tree may be so large that it is unacceptable to store it all in memory at once. For example, if we have a database file holding 2 30 records, and each index entry requires 8 bytes of storage, a BT holding the index would require 2 30 nodes, each taking 16 bytes of memory (assuming 32-bit pointers), or 16 GB of memory. An alternative would be to store the entire tree in a file on disk, and only load the immediately relevant portions of it into memory

isk Representation 2 It is a relatively simple matter to write any binary tree to a disk file, by representing each tree node by a data record that holds the data element and two file offsets specifying the locations of the children, if any of that node. B C The nodes don't need to be stored in any particular order. Null pointers may be represented by a negative offset. G F H ata lchild rchild 0 24 72 24 B -1 48 48 C -1-1 72 G 96 168 96 120 144 120-1 -1 144 F -1-1 168 H -1-1

isk Representation 3 The problem is that this disk representation will require too many individual disk accesses when processing a typical tree operation, such as a search or a traversal. hy? These tree operations typically require transiting from a node to one or both of its children. But there's no reason that the child nodes will be stored anywhere near the parent node (although we could at least guarantee that siblings are adjacent). ince each node stores only one data value, and the nodes we might well perform one disk access for every node that is accessed during the tree operation. Given the extremely slow nature of disk access, this is unacceptable. ata lchild rchild 0 24 72 24 B -1 48 48 C -1-1 72 G 96 168 96 120 144 120-1 -1 144 F -1-1 168 H -1-1

A B-tree of order m is a multi-way tree such that: - the root has at least two subtrees, unless it is a leaf 4 - each nonroot and nonleaf node holds k 1 data values, and k pointers to subtrees, where m/ 2 k m m/ 2 k - each leaf node holds k 1 data values, where m - all leaves are on the same level - the data values in each node are in ascending order - for all i, the data values in the first i children are less than the i-th data value - for all i, the data values in the last m i children are larger than the i-th data value o, a B-tree is generally at least half full, has a relatively small number of levels, and is perfectly balanced. Typically, m will be fairly large.

B Tree xample 5 A B-tree of order 5: L G A B C F H I J N P R T U X Y Z ince a binary search may be applied to the data values in each node, searching is highly efficient.

B Tree Insertion 6 Insertion follows similar logic to the BT, with the complications that we must search the list of values in each node, and make nodes obey the more complex restriction where k is the number of children the node has. m / 2 k m The basic idea is the same: search for the appropriate leaf, add the new value, then split and promote as necessary. For instance, inserting the values and then X into the B tree at right would cause the right-most leaf to split and the value to be promoted to the root. Then, inserting the value Z would cause the root to split, and the value to be promoted to a new root node. A F G H P T

Insertion xample I 7 Inserting just fills the leaf. A F G H P T Inserting X causes the leaf to overflow. o, we split the leaf and promote the median value, which is, up one level. The node there had room for the new value, so no further splitting occurs. A F G H P T X

Insertion xample II 8 A F G Inserting I just fills the second leaf. H P T Then, inserting J causes the leaf to overflow. o, we split the leaf and promote the median value, which is H, up one level. Now, the parent is full, and so splitting proceeds X A F G H I P T X

Insertion xample III plitting the root sends up: 9 H A F G I J P T X o, the B-tree grows by pushing up a new root, which keeps all leaves at the same level. As you can see here, the root must be an exception to the requirement that each node contains a minimum number of data values, since root-splitting will naturally lead to a new root node holding only one value.

B Tree Insertion Algorithm 10 InsertHelper(al, sroot, upal, upchild, splithappened) { NULL test, on general principles if at leaf { if NTFULL { insert al splithappened = false else { split off new right sibling for sroot set upal to middle value from splitting set upchild to new right sibling splithappened = true return find index Idx of child to descend InsertHelper(al, ptr[idx], upal, upchild, splithappened)... Note: this started as an implementation in C++; adapt to the language of your choice Computer cience ept a Tech August 2006 2006 cuain

B Tree Insertion Algorithm 11... if ( splithappened ) { if NTFULL { insert upal and upchild to sroot splithappened = false else { split off new right sibling for sroot set upal to middle value from splitting set upchild to new right sibling splithappened = true return Computer cience ept a Tech August 2006 2006 cuain

earch Cost 12 If we let key values: q m/ 2 then we can derive an upper bound on the height of the B-tree storing n h log q n 1 1 2 This is very small. For example, if m = 200 and n = 2,000,000 then h <= 4. But don t get too excited by this. The cost of doing a binary search of the data values in a node would be at least log 2 (q), and if we do that at each level in the tree, the total cost would be n 1 n 1 log( q) logq log log n 2 2 eep in mind that the motivation is to find a tree structure that can be efficiently stored to disk, and matching the search cost of a perfectly balanced binary tree is a plus.

Cost of plitting 13 It would seem that the primary concern about the cost of insertion would be the number of splits that must be performed (everything else is essentially analogous to BT insertion). It is possible to show that as n increases, the average probability of a split is approximated by 1 m / 2 1 o, for example, if m = 100 then the probability of a split is about 2%. That shouldn t be surprising. plitting a node is fairly expensive since about half the data values in the node must be moved to a new location, but for typical B-trees it won t be required all that often.

B Tree eletion 14 eletion of a value from a node has an interesting consequence, since the number of children is related to the number of values in the node. For a leaf node, deleting a value may drop the number of data values in the node below the mandatory floor. If that happens, the leaf must borrow a value from an adjacent sibling node if one has a value to spare, or be merged with an adjacent sibling node. But the latter will decrease the number of children the parent node has, and so a value must be moved from the parent node into the merged leaf. Consider deleting T from the B-tree of order 5 below: A F G H T X

15 Removing T from the leaf causes it to "underflow". eletion from a Leaf (one case) A H G F X Neither sibling node has a value to spare. o we must merge with a sibling: X X X

eletion from an Internal Node 16 eleting a value from an internal node is accomplished by reducing it to the former case. enote the value to be deleted by. The immediate predecessor of, which must be in a leaf node, is borrowed to replace the value that is being deleted, and then deleted from the leaf node. Consider deleting from the following B-tree of order 5: A F G H T X

eletion from an Internal Node 17 The immediate predecessor of is the largest value in the right-most leaf below the child that lies to the left of : A F G H T X The immediate predecessor is copied to replace the value being deleted and then removed from the leaf (trivial case this time): A F G H T X

B Tree eletion Algorithm 18 eletehelper(al, sroot, underflowhappened) { NULL test, on general principles search sroot for al or closest predecessor if al does not occur in sroot { eletehelper(al, appropriate child, underflowhappened) if success and underflowhappened { if can borrow { borrow value from appropriate child else { merge appropriate children adjust sroot to account for merge set underflowhappened return else if sroot is a leaf { delete al from sroot set underflowhappened return... Note: this started as an implementation in C++; adapt to the language of your choice Computer cience ept a Tech August 2006 2006 cuain

B Tree eletion Algorithm 19... else { replace al in sroot with closest predecessor eletehelpher(closest predecessor, left subtree from al, underflowhappened) if success and underflowhappened { if can borrow { borrow value from appropriate child else { merge appropriate children adjust sroot to account for merge set underflowhappened return return Computer cience ept a Tech August 2006 2006 cuain

B Tree torage fficiency 20 In a B tree: - nodes are guaranteed to be (essentially) at least 50% full - node could also be only 50% full, wasting half the data space in the nodes - but that "wasted" space is available to service future insertions - analysis and simulation indicates that in typical use a B tree will be about 70% full This expectation of wasted space is a motivation for some variants of the basic B tree.

B* Trees 21 In B* trees: - all nodes except the root are required to be at least 2/3 full rather than 1/2 full - splitting transforms 2 nodes into 3, rather than 1 node into 2 - analysis indicates the average utilization of a B* tree will be about 81% - can be generalized to specify a fill factor of (n+1)/(n+2); a B n tree nuth

B+ Trees 22 In B+ trees: - Internal nodes store only key values and pointers*. - All records, or pointers to records, are stored in leaves. - Commonly, the leaves are simply the logical blocks of a database file index, storing key values and offsets. In this case, many key values will occur twice in the tree, once at an internal node to guide searching, and again in a leaf. - If the leaves are simply an index, it is common to implement the leaf level as a linked list of B tree nodes why? The B+ tree is the most commonly implemented variant of the B-tree family, and the structure of choice for large databases. * In small databases, it is fairly common to use a B-tree as a direct data structure, with nodes storing records.

B Tree Implementation 23 A sample B tree template interface is shown below: template <typename T> class BTreeT { public: BTreeT(unsigned int rder = 3); ~BTreeT(); // some public fns not shown bool Insert(const T& al); bool elete(const T& al); void isplay(ostream& ut) const; T* const Find(const T& Target); const T* const Find(const T& Target) const; private: unsigned int rder; BNodeT<T>* Root; // some helper fns not shown ; The order could be supplied as a non-type template parameter, rather than via the constructor, but you could not then use a local variable when declaring a B tree, so the approach shown here is more flexible from the client perspective. Computer cience ept a Tech August 2006 2006 cuain

B Tree Node Implementation 24 The design of the B tree node type raises some interesting issues: - the node must provide a way to store rder - 1 data values - the node must provide a way to store rder pointers - the node must support binary search of the data values, so dynamically-allocated arrays are appropriate - the node should, perhaps, provide the necessary search function, and functions to remove and add data values (and adjust the pointer array as needed) - the node destructor must destroy the arrays - the node should provide an isfull() function, and perhaps other tests such as underflow, or whether the node has extra values that could be borrowed - the split and merge operations needed for the B tree implementation could be made the responsibility of the node ince leaf nodes do not need pointers, there is a case for having two distinct node types. hile that would save memory (or disk space), the idea will not be pursued here.

B Tree Node Implementation 25 The most common approach seems to be to use one array, of dimension rder 1, to store the data values, and another, of dimension rder, to store the pointers. It is possible to simplify the shifting needed when inserting and removing data values by coalescing the two arrays into one, storing data/pointer pairs. For example: template <typename T> class Pair { public: T ata; BNodeT<T>* Right; // some members perhaps not shown ; The node could then store an array of Pair objects, of dimension rder, using the 0-th cell to store only a pointer (to the left-most child). This approach also simplifies some communication issues when splitting a node. n the other hand, this approach also adds some syntactic complexity. Computer cience ept a Tech August 2006 2006 cuain

B Tree Node on isk 26 f course, the point is that we will store the nodes persistently on disk. o, how will we lay out the node in a file? - write alternating pointer (offset) and data values? - write the key values as a block and the pointers as a block? - what other values are necessary? There are a number of options it will be up to you to pick one. And, of course, the nature of the data values must be taken into account. - fixed-length simple data values? - variable-length or otherwise complex data values - text format or binary format?