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.

Slides:



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

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.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
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.
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.
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter Trees and B-Trees.
Other time considerations Source: Simon Garrett Modifications by Evan Korth.
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?
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
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.
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.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
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.
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.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
Tirgul 6 B-Trees – Another kind of balanced trees.
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.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
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.
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.
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.
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.
1 B-Trees & (a,b)-Trees CS 6310: Advanced Data Structures Western Michigan University Presented by: Lawrence Kalisz.
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.
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.
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.
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.
Comp 335 File Structures B - Trees. Introduction Simple indexes provided a way to directly access a record in an entry sequenced file thereby decreasing.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
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.
CSC 213 Lecture 8: (2,4) Trees. Review of Last Lecture Binary Search Tree – plain and tall No balancing, no splaying, no speed AVL Tree – liberté, égalité,
© 2004 Goodrich, Tamassia Trees
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
 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.
Internal and External Sorting External Searching
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
COMP261 Lecture 23 B Trees.
AA Trees.
B-Trees B-Trees.
Multiway Search Trees Data may not fit into main memory
B-Trees B-Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
B+-Trees.
B+-Trees.
B+-Trees.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Trees 4 The B-Tree Section 4.7
Chapter Trees and B-Trees
Chapter Trees and B-Trees
B+-Trees (Part 1).
Other time considerations
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
CSIT 402 Data Structures II With thanks to TK Prasad
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
B-Trees.
B-Trees.
Presentation transcript:

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 an entire data structure in the main memory of a computer. ◦ What if we have more data than can fit in main memory? ◦ Meaning that we must have the data structure reside on disk. ◦ The rules of the game change, because the Big-Oh model doesn’t apply if all operations are not equal.

Motivation of Multi-way Trees Main memory vs. disk ◦ Disk Accesses are incredibly expensive. ◦ 1 disk access is worth about 4,000,000 instructions. ◦ (See the book for derivation) ◦ So we’re willing to do lots of calculations just to save disk accesses.

Motivation of Multi-way Trees For example: ◦ Suppose we want to access the driving records of the citizens of Florida.  10 million items.  Assume doesn’t fit in main memory.  Assume in 1 sec, can execute 25 million instructions or perform 6 disk accesses. The Unbalanced Binary tree would be a disaster. In the worst case, it has linear depth and could require 10 mil disk accesses. An AVL Tree In the typical case, it has a depth close to log N, log 10 mil ≈ 24 disk accesses, requiring 4 sec.

The point is… Reduce the # of disk accesses to a very small constant,  Such as 3 or 4  And we are willing to write complicated code to do this, because in comparison machine instructions are essentially free.  As long as we’re not ridiculous. We cannot go below log N using a BST ◦ Even an AVL Solution?? ◦ More branching, less height. ◦ Multiway Tree

Multiway Tree (or B-tree in book) Multiway tree is similar to BST ◦ In a BST  We need 1 key to decide which of 2 branches to take. ◦ In an Multiway tree  We need M-1 keys to decide which branch to take, where M is the order of the Multiway tree.  Also need to balance,  Otherwise like a BST it could degenerate into a linked list. ◦ Here is a Multiway tree of Order 5:

Specifications of a MultiwayTree If a node contains k items (I 1, I 2.. I k ), it will contain k+1 subtrees. Specifically subtrees S 1 thru S k _15 _ _22 28 _ ___1___ 345_345_ 78__78__ _ _ _ _ __ _ _ _ _ 30 __ _ _ _ 55 _ _ _ _ _ I1I1 I2I2 I3I3 S1S1 S2S2 S3S3 S k+1 All values in S 1 < I 1. All values in S k+1 >I k. All values in S 3 < I 3, but ≥ I 2. All values in S 2 < I 2, but ≥ to I 1.

2-4 Trees ◦ Specific type of multitree. 1)Every node must have in between 2 and 4 children. (Thus each internal node must store in between 1 and 3 keys) 2)All external nodes (the null children of leaf nodes) and leaf nodes have the same depth.

Example of a 2-4 tree _15 _ _22 28 _ ___1___ 345_345_ 78__78__ _ _ _ _ __ _ _ _ _ 30 __ _ _ _ 55 _ _ _ _ _

Insert Insert 4 into the 2-4 tree below. Compare 4 to vals in root node _15 _ _22 28 _ __1__ 5__5__ 8__8__ 12 _ _ 17 _ _ 21 __ __ 26 _ _ __ ______ 45 _ _ 55 _ _ _ 4 < 10, 4 goes in the subtree to left of > 3 and 4 < 7, 4 goes in the subtree to right of 3 and left of 7 45_45_

Problems with Insert What if the node that a value gets inserted into is full? ◦ We could just insert the value into a new level of the tree. ◦ BUT then not ALL of the external nodes will have the same depth after this. Insert 18 into the following tree: _ 37_37_ _ _

Problems with Insert What if the node that a value gets inserted into is full? ◦ We could just insert the value into a new level of the tree. ◦ BUT then not ALL of the external nodes will have the same depth after this. Insert 18 into the following tree: _ The node has too many values. You can send one of the values to the parent (the book’s convention is to sent the 3 rd value. 37_37_ 22 _ _

Problems with Insert What if the node that a value gets inserted into is full? ◦ We could just insert the value into a new level of the tree. ◦ BUT then not ALL of the external nodes will have the same depth after this. Insert 18 into the following tree: _ 22 _ _ Moving 17, forces you to “split” the other three values. 18 __ __ 37_37_

Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: _5_ _ _ _ 25 __ __

Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: _5_ _ _ 25 __ __ Using the rule from before, let’s move 12 up.

Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: _5_ _ _ 17 __ __ Using the rule from before, let’s move 14 up. 25 __ __

Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: _5_ _ _ 25 __ __ Now this has too many parent nodes AND subtrees! We can just repeat the process and make a new root. 17 __ __

Other Problems with Insert In the last example, the parent node was able to “accept” the 17. ◦ What if the parent root node becomes full? Insert 12 into the 2-4 Tree below: _5_ _ _ 17 __ __ Now this has too many parent nodes AND subtrees! We can just repeat the process and make a new root. 25 __ __ 20 30

Deletion from a 2-4 Tree Delete a non-leaf value ◦ Replace that value with the largest value in its left subtree  Or smallest value in its right subtree.

Deletion from a 2-4 Tree Delete a leaf node ◦ In the standard case:  a value can simply be removed from a leaf node that stores more than one value.  Requires no structural change _57_ __ 35 _ _ _7_ __ 35 _ _ Delete 5

Deletion from a 2-4 Tree BUT what if the leaf node has ONLY one value? ◦ If you get rid of it,  then it would violate the 2-4 property that all leaf nodes MUST be on the same height of the tree. Break up into 2 cases: 1)An adjacent sibling has more than one value stored in its node. 2)An adjacent sibling does NOT have more than one value stored in its node, and a fusion operation MUST be performed.

Deletion from a 2-4 Tree Case 1: ◦ Consider deleting 5 from the following tree:  An adjacent sibling has more than one value stored in its node. Take the 10 to replace the 5, And then simply replace the 10 with the smallest value in its right subtree. ◦ This is okay, since there is more than one value at this subtree __5__ __ 35 _ _ _ _ __ 17 _ __ 35 _ _ _ Delete 5

Deletion from a 2-4 Tree Case 2: ◦ If an adjacent sibling does NOT have more than one value stored in its node, and a fusion operation MUST be performed. ◦ The fusion operation is a little more difficult since it may result in needing another fusion at a parent node. We have 3 child nodes when we should have 4. Thus we can drop a value, we will drop __5__ 15 _ __ 35 _ _ 25 _ _ _ 15 _ __ 35 _ _ 25 _ _ _ __ 35 _ _ 25 _ _ Delete 5 Fuse empty node with 15

Deletion from a 2-4 Tree Case 2: ◦ If an adjacent sibling does NOT have more than one value stored in its node, and a fusion operation MUST be performed. ◦ The fusion operation is a little more difficult since it may result in needing another fusion at a parent node. We have 3 child nodes when we should have 4. Thus we can drop a value, we will drop _ __ 35 _ _ 25 _ _ __ 35 _ _ 25 _ _ Drop one parent into fused node

Examples on the Board