CompSci 100 27.1 Memory Model  For this course: Assume Uniform Access Time  All elements in an array accessible with same time cost  Reality is somewhat.

Slides:



Advertisements
Similar presentations
Chapter 4: Trees Part II - AVL Tree
Advertisements

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
They’re not just binary anymore!
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
1 B trees Nodes have more than 2 children Each internal node has between k and 2k children and between k-1 and 2k-1 keys A leaf has between k-1 and 2k-1.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter Trees and B-Trees.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 19: B-Trees: Data Structures for Disk.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
1 B-Trees Disk Storage What is a multiway tree? What is a B-tree? Why B-trees? Comparing B-trees and AVL-trees Searching a B-tree Insertion in a B-tree.
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.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
B-Trees and B+-Trees Disk Storage What is a multiway tree?
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Preliminaries Multiway trees have nodes with greater than two children. Multiway trees of order k have nodes with most k children Trees –For all.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
Splay Trees and B-Trees
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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.
More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store.
Storage CMSC 461 Michael Wilson. Database storage  At some point, database information must be stored in some format  It’d be impossible to store hundreds.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
Oct 29, 2001CSE 373, Autumn External Storage For large data sets, the computer will have to access the disk. Disk access can take 200,000 times longer.
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
B-Trees and Red Black Trees. Binary Trees B Trees spread data all over – Fine for memory – Bad on disks.
COSC 2007 Data Structures II Chapter 15 External Methods.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Starting at Binary Trees
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
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.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
B-Trees ( Rizwan Rehman) Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
1 CSE 326: Data Structures Trees. 2 Today: Splay Trees Fast both in worst-case amortized analysis and in practice Are used in the kernel of NT for keep.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
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.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Multiway Search Trees Data may not fit into main memory
Balanced Binary Search Trees
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Chapter Trees and B-Trees
Chapter Trees and B-Trees
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
CPSC-310 Database Systems
Wednesday, April 18, 2018 Announcements… For Today…
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.
B- Trees D. Frey with apologies to Tom Anastasio
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.
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?
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
B-Trees.
Presentation transcript:

CompSci Memory Model  For this course: Assume Uniform Access Time  All elements in an array accessible with same time cost  Reality is somewhat different  Memory Hierarchy (in order of decreasing speed)  Registers  On (cpu) chip cache memory  Off chip cache memory  Main memory  Virtual memory (automatically managed use of disk)  Explicit disk I/O  All but last managed by system  Need to be aware, but can do little to manipulate others directly  Promote locality?

CompSci Cost of Disk I/O  Disk access 10,000 to 100,000 times slower than memory access  Do almost anything (almost!) in terms of computation to avoid an extra disk access  Performance penalty is huge  B-Trees designed to be used with disk storage  Typically used with database application  Many different variations  Will present basic ideas here  Want broad, not deep trees  Even log N disk accesses can be too many

CompSci External Methods  Disk use requires special consideration  Timing Considerations (already mentioned)  Writing pointers to disk?  What do values mean when read in at a different time/different machine?  General Properties of B-Trees  All Leaves Have Same Depth  Degree of Node > 2  (maybe hundreds or thousands)  Not a Binary Tree, but is a Search Tree  There are many implementations...  Will use examples with artificially small numbers to illustrate

CompSci Rules of B-Trees  Rules 1. Every node (except root) has at least MINIMUM entries 2. The MAXIMUM number of node entries is 2*MINIMUM 3. The entries of each B-tree are stored, sorted 4. The number of sub-trees below a non-leaf node is one greater than the number of node entries 5. For non leaves:  Entry at index k is greater than all entries in sub-tree k  Entry at index k is less than all entries in sub-tree k+1 6. Every leaf in a B-tree has the same depth

CompSci Example Example B Tree (MAX = 2) [6] * * [2 4] [9] * * * * * [1] [3] [5] [7 8] [10]

CompSci Search in B-Tree  Every Child is Also the Root of a Smaller B-Tree  Possible internal node implementation class BTNode { //ignoring ref on disk issue int myDataCount; int myChildCount; KeyType[] myKeys[MAX+1]; BTNode[] myChild[MAX+2] }  Search: boolean isInBTree(BTNode t, KeyType key); 1. Search through myKeys until myKeys[k] >= key 2. If t.myData[k] == key, return true 3. If isLeaf(t) return false 4. return isInBtree(t.myChild[k])

CompSci Find Example Example Find in B-Tree (MAX = 2) Finding 10 [6 17] * * * [4] [12] [19 22] * * * * * * * [2 3] [5] [10] [16] [18] [20] [25]

CompSci B-Tree Insertion  Insertion Gets a Little Messy  Insertion may cause rule violation  “Loose” Insertion (leave extra space) (+1)  Fixing Excess Entries  Insert Fix  Split  Move up middle  Height gained only at root  Look at some examples

CompSci Insertion Fix  ( MAX = 4 ) Fixing Child with Excess Entry [9 28] BEFORE * * * [3 4] [ ] [33 40] * * * * * * * * * * * * [2 3][4 5][7 8][11 12][14 15][17 18][20 21][23 24][26 27][31 32][34 35][50 51] [ ] AFTER * * * * [3 4] [13 16] [22 25] [33 40] * * * * * * * * * * * * [2 3][4 5][7 8][11 12][14 15][17 18][20 21][23 24][26 27][31 32][34 35][50 51]

CompSci Insertion Fix (MAX= 2) Another Fix [6 17] BEFORE * * * [4] [12] [ ] [ ] STEP 2 * [ ] * * * * [4] [12] [18] [22] [ ] STEP 1 * * * * [4] [12] [18] [22] [17] AFTER * * [6] [19] * * * * [4] [12] [18] [22]

CompSci B-Tree Removal  Remove  Loose Remove  If rules violated: Fix o Borrow (rotation) o Join  Examples left to the “reader”

CompSci B-Trees  Many variations  Leaf node often different from internal node  Only leaf nodes carry all data (internal nodes: keys only)  Examples didn’t distinguish keys from data  Design to have nodes fit disk block  The Big Picture  Details can be worked out  Can do a lot of computation to avoid a disk access

CompSci Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree is balanced if height of left and right subtree differ by no more than one, recursively for all nodes.  (Height of empty tree is -1)  Examples

CompSci Balanced Binary Search Trees  Keeping BSTrees Balanced  Keeps find, insert, delete O(log(N)) worst case.  Pay small extra amount at each insertion (and deletion) to keep it balanced  Several Well-known Systems Exist for This  AVL Trees  Red-Black Trees ...  Will look at AVL Trees

CompSci AVL Trees  AVL Trees  Adelson-Velskii and Landis  Discovered ways to keep BSTrees Balanced  Insertions  Insert into BST in normal way  If tree no longer balanced, perform a “rotation”  Rotations restore balance to the tree

CompSci AVL Trees  Single Rotation  An insertion into the left subtree of the left child of tree  Adapted from Weiss, pp // Used if it has caused loss of balance) // (Also used as part of double rotation operations) Tnode rotateWithLeftChild(TNode k2) //post: returns root of adjusted tree { TNode k1 = k2.left; k2.left = k1.right; k1.right = k2; return k1; }

CompSci AVL Trees  Single Rotation

CompSci AVL Trees k2 k1 k2 C A A B BC  Single Rotation  Also: mirror image before after

CompSci AVL Trees  Single Rotation  Mirror image case TNode rotateWithRightChild(TNode k2) //post: returns root of adjusted tree { TNode k1 = k2.right; k2.right = k1.left; k1.left = k2; return k1; }

CompSci AVL Tree  Double Rotation  An insertion into the right subtree of the left child of tree  Adapted from Weiss, p 57 // Used after insertion into right subtree, k2, // of left child, k1, of k3 (if it has caused // loss of balance) TNode doubleRotateWithLeftChild(TNode k3) //post: returns root of adjusted tree { k3.left = rotateWithRightChild(k3.left); return rotateWithLeftChild(k3); }

CompSci AVL Tree  Double Rotation

CompSci AVL Trees k3 k1 k3 D A A B B C  Double Rotation  Also: mirror image before after k2 C D

CompSci AVL Tree  Double Rotation  An insertion into the right subtree of the left child of tree  Adapted from Weiss, p 571 // Mirror Image TNode doubleRotateWithRightChild(TNode k3) //post: returns root of adjusted tree { k3.right = rotateWithLeftChild(k3.right); return rotateWithRightChild(k3); }

CompSci AVL Trees  Deletions can also cause imbalance  Use similar rotations to restore balance  Big Oh?