Docsity

Indexing with 2-3 Trees: A Shallow Tree Strategy for Efficient Record Management, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Indexing with 2-3 Trees: A Shallow Tree Strategy for Efficient Record Management and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

Indexing Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science — University of San Francisco – p.1/

Indexing

Often, when managing records, we want to do more thanjust find a record that matches a particular key. Find all records in a range, in order. Sort by one of several keys Also, records may not all fit into memory Department of Computer Science — University of San Francisco – p.2/

Indexing

Some Possible Data Structures: Sorted List Find / Find in Range: fast Add / Remove: slow Unsorted List / Hash Table Add, Find, Remove fast: (hash) Find in Range: slow Binary Search Tree All operations are fast (O(lg n)) if the tree is balanced Trees look like a good choice - how to keep thembalanced? Department of Computer Science — University of San Francisco – p.4/

Storing Records on Disk

So far, we’ve assumed our entire data structure can fit intomain memory. What if it’s too large? Need to minimize the number of disk accesses Speaking practically, reading from the disk is much slower than reading from memory RAM: 50 nanoseconds disk: 5 milliseconds (100,000 times slower) Strategies Keep tree as shallow as possible Store records with adjacent keys in adjacent blockson disk Department of Computer Science — University of San Francisco – p.5/

Example 2-3 Tree

6 16 3 8 13 18 7 11 12 14 17 2 2 5 Department of Computer Science — University of San Francisco – p.7/

Finding in 2-3 Trees

How can we find an element in a 2-3 tree? Department of Computer Science — University of San Francisco – p.8/

Inserting into 2-3 Trees

Always insert at the leaves To insert an element: Find the leaf where the element would live, if it was inthe tree Add the element to that leaf Department of Computer Science — University of San Francisco – p.10/

Inserting into 2-3 Trees

Always insert at the leaves To insert an element: Find the leaf where the element would live, if it was inthe tree Add the element to that leaf What if the leaf already has 2 elements? Department of Computer Science — University of San Francisco – p.11/

Splitting Nodes

5 6 7 7 5 6 Department of Computer Science — University of San Francisco – p.13/

Splitting Nodes

4 1 2 5 6 7 Too manyelements Department of Computer Science — University of San Francisco – p.14/

Splitting Nodes

Department of Computer Science — University of San Francisco – p.16/

Splitting Root

When we split the root: Create a new root Tree grows in height by 1 Department of Computer Science — University of San Francisco – p.17/

2-3 Tree Example

Inserting elements 1-9 (in order) into a 2-3 tree 1 2 Department of Computer Science — University of San Francisco – p.19/

2-3 Tree Example

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

Too many keys,need to split

Department of Computer Science — University of San Francisco – p.20/