© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.

Slides:



Advertisements
Similar presentations
 Definition of B+ tree  How to create B+ tree  How to search for record  How to delete and insert a data.
Advertisements

Chapter 4: Trees Part II - AVL Tree
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
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.
Chapter 14 Multi-Way Search Trees
Chapter 23 Multi-Way Search Trees. Chapter Scope Examine 2-3 and 2-4 trees Introduce the concept of a B-tree Example specialized implementations of B-trees.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
Chapter 15 B External Methods – B-Trees. © 2004 Pearson Addison-Wesley. All rights reserved 15 B-2 B-Trees To organize the index file as an external search.
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.
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.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
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,
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
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?
Preliminaries Multiway trees have nodes with greater than two children. Multiway trees of order k have nodes with most k children Trees –For all.
Homework #3 Due Thursday, April 17 Problems: –Chapter 11: 11.6, –Chapter 12: 12.1, 12.2, 12.3, 12.4, 12.5, 12.7.
Tirgul 6 B-Trees – Another kind of balanced trees.
CSC 213 – Large Scale Programming. Today’s Goals  Review a new search tree algorithm is needed  What real-world problems occur with old tree?  Why.
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.
Index Structures for Files Indexes speed up the retrieval of records under certain search conditions Indexes called secondary access paths do not affect.
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.
Tree.
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,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
B-Trees And B+-Trees Jay Yim CS 157B Dr. Lee.
COSC 2007 Data Structures II Chapter 15 External Methods.
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 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.
Starting at Binary Trees
File Organization and Processing Week Tree Tree.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
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.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
 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
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Balanced Search Trees (partial) Chapter 29 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall - Edited by Nadia Al-Ghreimil.
Binary Search Trees Chapter 7 Objectives
TCSS 342, Winter 2006 Lecture Notes
Extra: B+ Trees CS1: Java Programming Colorado State University
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Chapter Trees and B-Trees
Chapter Trees and B-Trees
(edited by Nadia Al-Ghreimil)
Multiway Trees Searching and B-Trees Advanced Tree Structures
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
2-3-4 Trees Red-Black Trees
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
(edited by Nadia Al-Ghreimil)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing and Using Data Structures Third Edition John Lewis & Joseph Chase

1-2 © 2010 Pearson Addison-Wesley. All rights reserved. 1-2 Chapter Objectives Examine 2-3 and 2-4 trees Introduce the generic concept of a B-tree Examine some specialized implementations of B-trees

1-3 © 2010 Pearson Addison-Wesley. All rights reserved. 1-3 Multi-way Search Trees In a multi-way search tree, –each node may have more than two child nodes –there is a specific ordering relationship among the nodes In this chapter, we examine three forms of multi- way search trees –2-3 trees –2-4 trees –B-trees

1-4 © 2010 Pearson Addison-Wesley. All rights reserved Trees A 2-3 tree is a multi-way search tree in which each node has zero, two, or three children A node with zero or two children is called a 2-node A node with zero or three children is called a 3-node A 2-node contains one element and either has no children or two children –Elements of the left sub-tree less than the element –Elements of the right sub-tree greater than or equal to the element

1-5 © 2010 Pearson Addison-Wesley. All rights reserved Trees A 3-node contains two elements, one designated as the smaller and one as the larger A 3-node has either no children or three children If a 3-node has children then –Elements of the left sub-tree are less than the smaller element –The smaller element is less than or equal to the elements of the middle sub-tree –Elements of the middle sub-tree are less then the larger element –The larger element is less than or equal to the elements of the right sub-tree

1-6 © 2010 Pearson Addison-Wesley. All rights reserved Trees All of the leaves of a 2-3 tree are on the same level Thus a 2-3 tree maintains balance

1-7 © 2010 Pearson Addison-Wesley. All rights reserved. 1-7 A 2-3 tree

1-8 © 2010 Pearson Addison-Wesley. All rights reserved. 1-8 Inserting Elements into a 2-3 Tree All insertions into a 2-3 tree occur at the leaves –The tree is searched to find the proper leaf for the new element Insertion has three cases –Tree is empty (in which case the new element becomes the root of the tree) –Insertion point is a 2-node –Insertion point is a 3-node

1-9 © 2010 Pearson Addison-Wesley. All rights reserved. 1-9 Inserting Elements into a 2-3 Tree The first of these cases is trivial with the element inserted into a new 2-node that becomes the root of the tree The second case occurs when the new element is to be inserted into a 2-node In this case, we simply add the element to the leaf and make it a 3-node

1-10 © 2010 Pearson Addison-Wesley. All rights reserved Inserting 27

1-11 © 2010 Pearson Addison-Wesley. All rights reserved Insertion into a 2-3 Tree The third case occurs when the insertion point is a 3-node In this case –the 3 elements (the two old ones and the new one) are ordered –the 3-node is split into two 2-nodes, one for the smaller element and one for the larger element –the middle element is promoted (or propagated) up a level

1-12 © 2010 Pearson Addison-Wesley. All rights reserved Insertion into a 2-3 Tree The promotion of the middle element creates two additional cases: –The parent of the 3-node is a 2-node –The parent of the 3-node is a 3-node If the parent of the 3-node being split is a 2-node then it becomes a 3-node by adding the promoted element and references to the two resulting two nodes

1-13 © 2010 Pearson Addison-Wesley. All rights reserved Inserting 32

1-14 © 2010 Pearson Addison-Wesley. All rights reserved Insertion into a 2-3 Tree If the parent of the 3-node is itself a 3-node then it also splits into two 2-nodes and promotes the middle element again

1-15 © 2010 Pearson Addison-Wesley. All rights reserved Inserting 57

1-16 © 2010 Pearson Addison-Wesley. All rights reserved Inserting 25

1-17 © 2010 Pearson Addison-Wesley. All rights reserved Removing Elements from a 2-3 Tree Removal of elements is also made up of three cases: –The element to be removed is in a leaf that is a 3-node –The element to be removed is in a leaf that is a 2-node –The element to be removed is in an internal node

1-18 © 2010 Pearson Addison-Wesley. All rights reserved Removing Elements from a 2-3 Tree The simplest case is that the element to be removed is in a leaf that is a 3-node In this case the element is simply removed and the node is converted to a 2-node

1-19 © 2010 Pearson Addison-Wesley. All rights reserved Removal from a 2-3 tree (case 1)

1-20 © 2010 Pearson Addison-Wesley. All rights reserved Removing Elements from a 2-3 Tree The second case is that the element to be removed is in a leaf that is a 2-node This creates a situation called underflow We must rotate the tree and/or reduce the tree’s height in order to maintain the properties of the 2-3 tree

1-21 © 2010 Pearson Addison-Wesley. All rights reserved Removing Elements from a 2-3 Tree This case can be broken down into four subordinate cases The first of these subordinate cases (2.1) is that the parent of the 2-node has a right child that is a 3-node In this case, we rotate the smaller element of the 3-node around the parent

1-22 © 2010 Pearson Addison-Wesley. All rights reserved Removal from a 2-3 tree (case 2.1)

1-23 © 2010 Pearson Addison-Wesley. All rights reserved Removing Elements from a 2-3 Tree The second of these subordinate cases (2.2) occurs when the underflow cannot be fixed through a local rotation but there are 3-node leaves in the tree In this case, we rotate prior to removal of the element until the right child of the parent is a 3- node Then we follow the steps for our previous case (2.1)

1-24 © 2010 Pearson Addison-Wesley. All rights reserved Removal from a 2-3 tree (case 2.2)

1-25 © 2010 Pearson Addison-Wesley. All rights reserved Removal of Elements from a 2-3 Tree The third of these subordinate cases (2.3) occurs when none of the leaves are 3-nodes but there are 3-node internal nodes In this case, we can convert an internal 3-node to a 2-node and rotate the appropriate element from that node to rebalance the tree

1-26 © 2010 Pearson Addison-Wesley. All rights reserved Removal from a 2-3 tree (case 2.3)

1-27 © 2010 Pearson Addison-Wesley. All rights reserved Removing Elements from a 2-3 Tree The fourth subordinate case (2.4) occurs when there not any 3-nodes in the tree This case forces us to reduce the height of the tree To accomplish this, we combine each the leaves with their parent and siblings in order If any of these combinations produce more than two elements, we split into two 2-nodes and promote the middle element

1-28 © 2010 Pearson Addison-Wesley. All rights reserved Removal from a 2-3 tree (case 2.4)

1-29 © 2010 Pearson Addison-Wesley. All rights reserved Removing Elements from a 2-3 Tree The third of our original cases is that the element to be removed is in an internal node As we did with binary search trees, we can simply replace the element to be removed with its inorder successor

1-30 © 2010 Pearson Addison-Wesley. All rights reserved Removal from a 2-3 tree (case 3)

1-31 © 2010 Pearson Addison-Wesley. All rights reserved Trees 2-4 Trees are very similar to 2-3 Trees adding the characteristic that a node can contain three elements A 4-node contains three elements and has either no children or 4 children The same ordering property applies as 2-3 trees The same cases apply to both insertion and removal of elements as illustrated on the following slides

1-32 © 2010 Pearson Addison-Wesley. All rights reserved Insertions into a 2-4 tree

1-33 © 2010 Pearson Addison-Wesley. All rights reserved Removals from a 2-4 tree

1-34 © 2010 Pearson Addison-Wesley. All rights reserved B-Trees Both 2-3 trees and 2-4 trees are examples of a larger class of multi-way search trees called B-trees We refer to the maximum number of children of each node as the order of a B- Tree Thus 2-3 trees are 3 B-trees and 2-4 trees are 4 B-trees

1-35 © 2010 Pearson Addison-Wesley. All rights reserved B-Trees B-trees of order m have the following properties:

1-36 © 2010 Pearson Addison-Wesley. All rights reserved A B-tree of order 6

1-37 © 2010 Pearson Addison-Wesley. All rights reserved Motivation for B-trees B-trees were created to make the most efficient use possible of the relationship between main memory and secondary storage For all of the collections we have studied thus far, our assumption has been that the entire collections exists in memory at once

1-38 © 2010 Pearson Addison-Wesley. All rights reserved Motivation for B-trees Consider the case where the collection is too large to exist in primary memory at one time Depending upon the collection, the overhead associated with reading and writing from files and/or swapping large segments of memory in and out could be devastating

1-39 © 2010 Pearson Addison-Wesley. All rights reserved Motivation for B-trees B-trees were designed to flatten the tree structure and to allow for larger blocks of data that could then be tuned so that the size of a node is the same size as a block on secondary storage This reduces the number of nodes and/or blocks that must be accessed, thus improving performance

1-40 © 2010 Pearson Addison-Wesley. All rights reserved B*-trees A variation of B-trees called B*-trees were created to solve the problem that the B-tree could be half empty at any given time B*-trees have all of the same properties as B- trees except that, instead of each node having k children where m/2 ≤ k ≤ m, in a B*-tree, each node has k children where (2m–1)/3 ≤ k ≤ m This means that each non-root node is at least two-thirds full

1-41 © 2010 Pearson Addison-Wesley. All rights reserved B+-trees Another potential problem for B-trees is sequential access B+-trees provide a solution to this problem by requiring that each element appear in a leaf regardless of whether it appears in an internal node By requiring this and then linking the leaves together, B+-trees provide very efficient sequential access while maintaining many of the benefits of a tree structure

1-42 © 2010 Pearson Addison-Wesley. All rights reserved A B+-tree of order 6

1-43 © 2010 Pearson Addison-Wesley. All rights reserved Implementation Strategies for B- trees A good implementation strategy for B-trees is to think of each node as a pair of arrays –An array of m-1 elements –An array of m children Then, if we think of the tree itself as one large array of nodes, then the elements stored in the array of children would simply be integer indexes into the array of nodes