1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.

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.
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.
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?
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
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.
1 Database indices Database Systems manage very large amounts of data. –Examples: student database for NWU Social Security database To facilitate queries,
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.
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?
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.
Binary Trees Chapter 6.
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.
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.
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.
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 6.
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,
CSE AU B-Trees1 B-Trees CSE 373 Data Structures.
Multi-way Trees. M-way trees So far we have discussed binary trees only. In this lecture, we go over another type of tree called m- way trees or trees.
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.
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
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
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
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é,
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
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. 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
CS 367 Introduction to Data Structures Lecture 8.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
COMP261 Lecture 23 B Trees.
Unit 9 Multi-Way Trees King Fahd University of Petroleum & Minerals
Multiway Search Trees Data may not fit into main memory
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
B+-Trees.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Chapter Trees and B-Trees
B-Trees © Dave Bockus Acknowledgements to:
Chapter Trees and B-Trees
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
B- Trees D. Frey with apologies to Tom Anastasio
B- Trees D. Frey with apologies to Tom Anastasio
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Binary Search Trees.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
B- Trees D. Frey with apologies to Tom Anastasio
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
B-Trees.
Data Structures Using C++ 2E
Presentation transcript:

1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10

2 Multi-way Search Trees of order m (m-way search trees) Generalization of BSTs Each node has at most m children If k is number of values at a node, then node has at most k+1 children (actually exactly m references, but some may be null) Tree is ordered BST is a 2-way search tree v1v1 v2v2 v3v3 v4v4 v5v5 keys<v 1 v 2 < keys<v 3 keys>v 5... ADS2 Lecture17 m-way trees

Examples A 3-way tree ADS2 Lecture17 M = 3

4 Examples A 4-way tree ADS2 Lecture17 M = 4

5 Searching in an m-way tree Similar to that for BST To search for value x in node (pointed to by) V containing values (v 1,…,v k ) : – if V=null, we are done (x is not in the tree) – if x<v 1, search in V’s left-most subtree – if x>v k, search in V’s right-most subtree, – if x=v i, for some 1  i  k, we are done (x has been found) – if v i  x  v i+1 for some 1  i  k-1, search the subtree between v i and v i+1 v 1 v 2 …v i v i+1 … v k V ADS2 Lecture17 m-way trees

6 Example search for ADS2 Lecture17 m-way trees

NOTE: inorder traversal is appropriate/defined m-way trees

8 Insertion for an m-way tree Similar to insertion for BST Remember, for an m-way tree can have at most m-1 values at each node To add value x, continue as for search until we reach a node (pointed to by V) containing (v 1,…,v k ) (where k  m-1) and can’t continue If V is full and x<v 1 then the left subtree must be empty, so create a new (left-most) child for V and place x as its first value. If V is full and v i < x < v i+1 then the subtree between v i and v i+1 must be empty, so create a new child for V between v i and v i+1 and place x as its first value. If V is full and x>v k then the right subtree must be empty, so create a new (right-most) child for V and place x as its first value If V is not full then add x to V so that values of V remain ordered. ADS2 Lecture17 m-way trees

9 Examples Create the 4-way tree formed by inserting the values 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 in order ADS2 Lecture17 m-way trees

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 12

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 11,12

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 8,11,12

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 8,11,12 14

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 8,11,12 149

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 8,11,

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 8,11, ,3

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 8,11,12 149,102,3

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 8,11,12 149,102,3,5

M=4 Insert 12, 11, 8, 14, 9, 3, 2, 10, 5, 16 8,11,12 14,169,102,3,5

21 Node of an m-way tree Each node contains Integer size (indicating how many values present) A reference to the left-most child A sequence of m-1 value/reference pairs Inorder Traversal Left subtree traversal, first value, first right subtree traversal, next value, next right subtree traversal etc. v 1 v 2 v 3 v 4 v 5 keys<v 1 v 2 < keys<v 3 keys<v 5... v 1 v 2 v 3 v 4 v 5 keys<v 1 v 2 < keys<v 3 keys>v 5... ADS2 Lecture17 m-way trees

m could be really big a node could contain a tree (a bstree or an avl tree) we might search within node using binary search nodes might correspond to large regions of disc space we want to minimise slooooow disc access think BIG

balanced m-way trees

24 Balanced m-way trees (B-trees) Like BSTs, m-way trees can become very unbalanced Of particular importance when we want to use trees to process data on secondary storage like disks where access is costly We use a special type of m-way tree (B-tree) which ensures balance: all leaves are at the same depth Here we need to check 5 nodes to find value 55 but only 2 to find value 35 ADS2 Lecture17

25 B-Trees Motivation If we want to store large amounts of data, may need to store it on disk Number of times we have to access disk to retrieve data becomes important A disk access is very expensive compared to a typical computer instruction Number of disk accesses dominates running time Secondary memory (disk) divided into equal-sized blocks (e.g. 512, 2048, 4096 or 8192 bytes) Basic I/O operation transfers contents of one disk block to/from main memory Our goal: to devise m-way search tree which minimises disk access (and exploits disk block read) ADS2 Lecture17

26 10 years old!

27 A B-trees is: An m-way search tree designed to conserve space and be reasonably well balanced Each node still has at most m children but: – Root is either a leaf or has between 2 and m children, and contains at least one value – All nonleaf nodes (except root) have at least m/2 if even, at least (m-1)/2 if odd – All leaves are same depth values ADS2 Lecture17

28 Comparison of B-Trees with binary search trees Comparison with binary search trees: (1) Multi-branched so depth is smaller. Search is faster because there are fewer nodes on a path from root to leaf. (2) Well balanced so the performance of search etc is about optimum. Complexity is logarithmic (like AVL trees..) (3) processing a node takes longer because it has more values. ADS2 Lecture17

29 Examples A B-tree of order 5: ADS2 Lecture17

30 Examples A B-tree of order 3: ADS2 Lecture17

Examples Not a B-tree All leaves must be at same depth

32 Insertion Like insertion for general m-way search tree, but need to preserve balance condition To add value x, continue as for search until we reach a node (pointed to by ) V containing (v 1,…,v k ) (where k  m-1) and can’t continue. If we were to add x to V in order. If V would not overflow, go ahead and add x If V would overflow, add x and split V into 3 parts: Left: first (m-1)/2 values Middle: (m-1)/2 +1 th value Right: last (m-1)/2 values Promote Middle to parent node, with children Left and Right Nb. Assume m is odd. Otherwise Left: first m/2 values Right: last m-2/2 values “Middle”: m/2 +1 th value. ADS2 Lecture17

33 Example To add 74 to this B Tree of order 5 ADS2 Lecture17

34 Example To add 74 to this B-Tree of order 5, would reach node V. Adding 74 would give (ordered) values Causing V to overflow. V ADS2 Lecture17

35 Example To add 74 to this B-Tree of order 5, would reach node V. Adding 74 would give (ordered) values Causing V to overflow. V Promote median to parent node, with children containing 73,74 and 77,78 respectively ADS2 Lecture17 split

36 But what if the parent overflows? If the parent overflows, repeat the procedure (upwards) If the root overflows, create a new root with Middle its only value and Left and Right as its children ADS2 Lecture17 overflow

37 Example add 18 would cause V to overflow: V ADS2 Lecture17 overflow

Example L R 17 add 18 would cause V to overflow: V ADS2 Lecture17 split v produce L and R elevate 17 to parent overflow

39 Example L R L R add 18 would cause V to overflow: V cont. overleaf ADS2 Lecture17 split v produce L and R elevate 17 to parent split parent overflow

40 Example contd L R L R ADS2 Lecture17 overflow

trees A B-tree guarantees that insertion, membership and deletion take logarithmic time. For storing a set it is best to use a B-tree of small order to minimise work at each node (assuming memory resident) Commonly used are 2-4 B-trees (order 4) In general, a 2-m tree has order m (all non-root nodes have 2,3,..,m children) ADS2 Lecture17

2_m Tree An implementation and An example with m=3 X CBA

2_m tree (m=3) m = 3 a node contains at most 2 pieces of data and then branches 3 ways a node contains at least one piece of data and then branches 2 ways it is a 2-3 tree m = 4 a node contains at most 3 pieces of data an then branches 4 ways a node contains at least one piece of data and then branches 2 ways it is a 2-4 tree X CBA

2_m tree (m=3) m = 3 a node contains at most 2 pieces of data and then branches 3 ways a node contains at least one piece of data and then branches 2 ways it is a 2-3 tree m = 4 a node contains at most 3 pieces of data an then branches 4 ways a node contains at least one piece of data and then branches 2 ways it is a 2-4 tree This is null X CBA

2_m tree (m=3) data (the top row in the picture) an ArrayList actually contains the stuff that’s in a node X CBA

2_m tree (m=3) left (the bottom row in the picture) an ArrayList pointers to children X CBA This is null

2_m tree (m=3) left (the bottom row in the picture) an ArrayList pointers to children X CBA This is null Oops! Should have 4 blocks!

2_m tree (m=3) NOTE: we do not show parent link m is the maximum branching factor X CBA

2_m tree (m=3) Note: There are m+1 data and left entries m data entries used m+1 left entries used A null data entry is treated as ∞ this simplifies overflow X CBA

2_m tree (m=3) left.get(i) points to a child with values less that data.get(i) let n = data.size() data.get(n-1) == null left.get(n-1) points to a node with all entries greater than this node consider data.get(n-1) as infinity X CBA 47 Less than 4Less than 7 Greater than 7

2_m tree (m=3) left.get(i) points to a child with values less that data.get(i) let n = data.size() data.get(n-1) == null left.get(n-1) points to a node with all entries greater than this node consider data.get(n)-1 as infinity X CBA 47 Less than 4Less than 7 Less than ∞

NOTE: a node is a leaf if data[0] == null X CBA 47 Less than 4Less than 7 Greater than 7 2_m tree (m=3)

Another view 2_m tree (m=3) X CBA

Another other view (bracket notation) 2_m tree (m=3) X CBA

Split A An example of an insertion leading to a split

Split A An example of an insertion leading to a split X CBA

Split A Insertion resulting in overflow Node contains 3 entries (only 2 allowed) X CBA

Split A Create a new node A’ X CBA X BA’AC

Split A Create a new node A’ insert largest element in A into A’ X CBA X BA’AC

Split A Create a new node A’ insert largest element in A to A’ insert largest element in A into parent X CBA X BA’AC

Split A Create a new node A’ insert largest element in A to A’ insert largest element in A into parent update left & parent pointers inorder X CBA X BA’AC

Split A Another view (post split)

Split A Another other view

Split X We should of course now split the parent! See following code

Code & Demo

Download and run

EXAMPLE: Method toString is an inorder traversal

EXAMPLE: Method isPresent … like in a bstree

split() … by example, overflow in an interior node

split() … we have added data to V (interior node), have an overflow and must split U V 2_m tree (m=3)

U is the parent of V 2_m tree (m=3) U V

V is this node 2_m tree (m=3) U V

Create new node W 2_m tree (m=3) U VW

If V has no parent U then create one and make it the root 2_m tree (m=3) U VW

Add last (largest) element in V into W and carry over left pointers (note: no longer a tree!) 2_m tree (m=3) U VW

If V isn’t a leaf then update parents of children passed over to W (not shown) 2_m tree (m=3) U VW

New node W’s parent is U (not shown) 2_m tree (m=3) U VW

Remove from V the data passed to W 2_m tree (m=3) U VW

Insert largest element in V into its parent U 2_m tree (m=3) U VW

V’s largest child is then its second largest element (a bit of a hack to simplify next step) 2_m tree (m=3) U VW

Remove from V the element passed up to U 2_m tree (m=3) U VW

If parent of V (that is U) has overflowed … then split U 2_m tree (m=3) U VW

2_m tree deletion Removal from a 2_m tree See Goodrich & Tamassia Chapter 10, pages 460 to 463

Download the code

fin