Docsity

Data Structures and Algorithms: B-Trees - Prof. David J. Galles, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structures and Algorithms: B-Trees - Prof. David J. Galles and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

CS245-2009S-

B-Trees

David Galles Department of Computer Science University of San Francisco

Indexing

Operations: Add an element Remove an element Find an element, using a key Find all elements in a range of key values

Indexing

Generalized Binary Search Trees Each node can store several keys, instead ofjust one Values in subtrees between values insurrounding keys For non leaves, # of children = # of keys + 1 2 6 1 3 4 7

2-3 Trees

Generalized Binary Search Tree Each node has 1 or 2 keys Each (non-leaf) node has 2-3 children hence the name, 2-3 Trees All leaves are at the same depth

Finding in 2-3 Trees

How can we find an element in a 2-3 tree?

Finding in 2-3 Trees

How can we find an element in a 2-3 tree? If the tree is empty, return false If the element is stored at the root, return true Otherwise, recursively find in the appropriatesubtree

Inserting into 2-3 Trees

Always insert at the leaves To insert an element: Find the leaf where the element would live, if itwas in the tree Add the element to that leaf What if the leaf already has 2 elements?

Inserting into 2-3 Trees

Always insert at the leaves To insert an element: Find the leaf where the element would live, if itwas in the tree Add the element to that leaf What if the leaf already has 2 elements? Split!

19-11:

Splitting Nodes

Too

many

elements

19-12:

Splitting Nodes

Promote to parent

Left

child

of

Right childof 6

Splitting Root

When we split the root: Create a new root Tree grows in height by 1

2-3 Tree Example

Inserting elements 1-9 (in order) into a 2-3 tree

2-3 Tree Example

Inserting elements 1-9 (in order) into a 2-3 tree

Too many keys,need to split

2-3 Tree Example

Inserting elements 1-9 (in order) into a 2-3 tree 1 2 3