They’re not just binary anymore!

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
B-Trees. Motivation for B-Trees Index structures for large datasets cannot be stored in main memory Storing it on disk requires different approach to.
Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 19: B-Trees: Data Structures for Disk.
Btrees21 B-trees: The rest of the story. btrees22 Review of B-tree rules All nodes except root must have at least MINIMUM data entries No node may exceed.
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
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.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
B-Trees Chapter 9. Limitations of binary search Though faster than sequential search, binary search still requires an unacceptable number of accesses.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
Trees Main and Savitch Chapter 10. Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
B+ Tree What is a B+ Tree Searching Insertion Deletion.
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.
ICS 220 – Data Structures and Algorithms Week 7 Dr. Ken Cosh.
Spring 2006 Copyright (c) All rights reserved Leonard Wesley0 B-Trees CMPE126 Data Structures.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
External Searching: B-Trees Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
B-Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Lecture1 introductions and Tree Data Structures 11/12/20151.
CompSci 100E 39.1 Memory Model  For this course: Assume Uniform Access Time  All elements in an array accessible with same time cost  Reality is somewhat.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CompSci Memory Model  For this course: Assume Uniform Access Time  All elements in an array accessible with same time cost  Reality is somewhat.
 B-tree is a specialized multiway tree designed especially for use on disk  B-Tree consists of a root node, branch nodes and leaf nodes containing the.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Binary search trees Outline This topic covers binary search trees: –Abstract Sorted Lists –Background –Definition and examples –Implementation: Front,
COMP261 Lecture 23 B Trees.
Data Structures and Algorithms for Information Processing
CSC212 Data Structure - Section AB
Binary Search Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
(edited by Nadia Al-Ghreimil)
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
B-Tree Insertions, Intro to Heaps
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Multiway Trees Searching and B-Trees Advanced Tree Structures
(edited by Nadia Al-Ghreimil)
Mark Redekopp David Kempe
B-Trees.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Presentation transcript:

They’re not just binary anymore! B-trees: They’re not just binary anymore! btrees

The balance problem Binary search trees provide efficient search mechanism only if they’re balanced Balance depends on the order in which nodes are added to a tree This tree is balanced because data arrived in this order: 4, 2, 6, 1, 5, 3, 7 If data arrive in this order: 7, 6, 5, 4, 3, 2, 1 …result is this btrees

One possible solution: B-trees B-tree nodes hold data B-tree nodes have (many) more than 2 children Each node contains more than one data entry Set of rules governs behavior of B-trees btrees

B-tree rules Two constants need to be defined to determine the number of entries stored in a node: MINIMUM: every node (other than root) has at least MINIMUM entries MAXIMUM: twice the value of minimum btrees

B-tree rules Rule 1: Root may have as few as one entry; every other node has at least MINIMUM entries Rule 2: Maximum number of entries in a node is MAXIMUM (2 * MINIMUM) Rule 3: Each node of a B-tree contains a partially-filled arrays of entries, sorted from smallest to largest btrees

B-tree rules Rule 4: The number of subtrees below a non-leaf node is one more than the number of entries in the node example: if a node has 10 entries, it has 11 children entries in subtrees are organized according to rule #5 btrees

B-tree rules Rule 5: For any non-leaf node: an entry at index n is greater than all entries in subtree n of the node; an entry at index n is less than all entries in subtree n+1 of the node Rule 6: Every leaf has the same depth btrees

Example B-tree For this node, count=2, MINIMUM=2 children=3 MAXIMUM=4 btrees

Using a B-tree to implement the Set ADT Characteristics of a set: similar to bag (container holding a collection of items) each item is unique; bag can contain more than one instance of a particular value, but a set only contains one btrees

Set operations construct/destruct clear: remove all items from a set insert: add one item remove: deletes one item contains: returns true if set contains target value is_empty: returns true if set is empty btrees

Set class definition template <class item> class Set { public: Set(const Set& source); ~Set( ); void operator = (const Set& source); void clear( ); void insert (const item& entry); void remove (const item& target); bool contains (const item& target) const; bool is_empty ( ) const {return count = = 0;} btrees

Set class definition private: // constants needed for B-tree implementation enum {MINIMUM = 200}; enum {MAXIMUM = 400}; // all member variables describe root node int count; // # of data items stored item data[MAXIMUM+1]; // stores root’s data entries int children; // # of children root has Set* subset[MAXIMUM+2]; // array of pointers to root’s children // each subset is itself a Set object; // each one is the root node of a smaller Set btrees

Set class definition // private section continued -- helper functions bool is_leaf( ) const {return children = = 0;} void loose_insert (const item& entry); // adds entry to set; may result in one // extra entry in root node of the subset void loose_remove (const item& target); // removes entry from set; may result in // subset’s root node being short one data entry void remove_largest(item& removed); void fix_excess (int x); // restores B-tree after loose_insert void fix_shortage (int x); // restores B-tree after loose_remove }; btrees

Invariant for the Set class Items in the set are stored in a B-tree; each child node is the root of a smaller B-tree A tally of the number of items in the root node is kept in member variable count The items in the root node are stored in the data array in data[0] … data[count-1] If the root has subtrees, they are stored in sets pointed to by pointers in the subset array in subset[0] … subset[children-1] btrees

Searching for item in a B-tree Check for target in root; if found there, return true If target isn’t found in root, and root has no children, return false If root has children but doesn’t contain target, make recursive call to search the subtree that could contain the target btrees

Implementation of Set member function contains( ) template <class item> bool Set<item>::contains (const item& target) const { int t; for (t=0; (t<count) && (data[t]<target); t++); if ((t < count) && data[t] == target) return true; if (is_leaf()) return false; return subset[t]->contains(target); } btrees

Inserting an item into a B-tree Easiest method: relax the rules! Perform “loose” insertion: allow the root node to end up with one entry too many After loose insertion, can split root node if necessary, creating new root node and increasing height of the tree btrees

Insertion example 0 3 1 2 4 1 0 2 1 3 5 0 2 4 6 MINIMUM = 1 0 3 1 2 4 1 0 2 1 3 5 0 2 4 6 MINIMUM = 1 MAXIMUM = 2 Data entered in this order: 0,1,2,3,4,5,6,7,8 3 5 4 6 1 2 3 1 5,7 0 2 4 6 8 3 1 5 0 2 4 6 Continue adding entries, splitting nodes and growing tree upward when necessary Child node has too many entries; Split node in two, sending middle entry up to root Entries exceed MAXIMUM; split node and grow tree upward 7 8 Regardless of data entry order, tree will remain balanced btrees

Functions needed for insertion Public insert function: performs “loose” insertion; if loose insertion results in excess entries in a child node, calls a function to grow the tree upward Private functions loose_insert and fix_excess are called by the public function btrees

Loose insertion Loose insertion does most of the work of inserting a value: finds slot where value should go, saving index; if correct slot not found in root, index set to root’s count value if index is within root’s data array, and root has no children, shift entries to the right and add new entry, incrementing count if root has children make recursive call on subset at index btrees

Fixing nodes with excess entries Loose insertion can result in a node containing one too many entries A node with an excess will always have an odd number of entries - the middle entry is pushed up to the parent node, and the remaining entries, along with any subsets, are split between the existing child and a new child btrees

Fix_excess function Called by loose_insert when a child node is involved Called by insert when action of loose_insert causes there to be an excess entry in the root node (of the entire tree) btrees

Removing an item from a B-tree Again, simplest method involves relaxing the rules Perform “loose” erase -- may end up with an invalid B-tree: might leave root of entire tree with 0 entries might leave root of subtree with less than MINIMUM entries After loose erase, restore B-tree btrees

To be continued ... btrees