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.

Slides:



Advertisements
Similar presentations
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
They’re not just binary anymore!
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
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.
Graphs & Exam Review 3 Chapter 10 – 13 CS211 CS Dept, MHC.
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 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
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.
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.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
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.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CS 367 Introduction to Data Structures Lecture 8.
@ 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)
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
COMP261 Lecture 23 B Trees.
TCSS 342, Winter 2006 Lecture Notes
AA Trees.
Data Structures and Algorithms for Information Processing
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
CSC212 Data Structure - Section AB
Binary Search Tree (BST)
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Tonga Institute of Higher Education
(edited by Nadia Al-Ghreimil)
Data Structures and Algorithms
B Tree Adhiraj Goel 1RV07IS004.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Wednesday, April 18, 2018 Announcements… For Today…
Lecture 26 Multiway Search Trees Chapter 11 of textbook
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
i206: Lecture 14: Heaps, Graphs intro.
CSC212 Data Structure - Section RS
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
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.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
B-Tree Insertions, Intro to Heaps
Tree Representation Heap.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
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.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Priority Queues (Chapter 6.6):
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.
A Robust Data Structure
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
(edited by Nadia Al-Ghreimil)
CSC 143 Binary Search Trees.
Priority Queues (Chapter 6):
CO4301 – Advanced Games Development Week 4 Binary Search Trees
B-Trees.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps.
Presentation transcript:

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 in the Priority Queue project of Chapter 11. The lecture includes the algorithms for adding to a heap (including reheapification upward), removing the top of a heap (including reheapification downward), and implementing a heap in a partially-filled array. Prior to this lecture, the students need a good understanding of complete binary trees. It would also help if they have seen binary search trees and the priority queue class. Data Structures and Other Objects Using C++

The problems of Unbalanced Trees Solution: 1. Periodically balance the tree (Project 9 in Chapter 10 on page 538) 2. Leaves cannot become too deep --- B-tree 1 2 3 4 5

B-Tree Differences compared to BST: 1. B-tree nodes have many more than two children 2. Each node contains more than just a single entry 3. Rules ensure that leaves do not become too deap

B-Tree Rules 1. Root may have as few as 1 entry – every other node has MINIMUM entries 2. Maximum number of entries in a node is twice MINIMUM 3. Entries of each B-tree node are stored in a partially filled array – sorted in increasing order 4. Number of subtrees below a nonleaf node is 1 more than the number of entries in the node

B-Tree Rules 5. For any nonleaf node a. An entry at index i is greater than all the entries in subtree number i of the node b. An entry at entry i is less than all the entries in subtree i+1 of the node 6. Every leaf in a B-tree has the same depth

B-Tree Example B-tree of 10 integers (with MINIMUM set to 1) 6 2 and 4 9 1 3 5 7 and 8 10

B-Tree Illustrations https://www.youtube.com/watch?v=coRJrcIYbF4 https://www.cs.usfca.edu/~galles/visualization/BT ree.html

Set Example Author uses a set implemented as a B-tree as an example

Class Invariant Items in the set are stored in a B-tree Number of entries in root – stored in member variable data_count & number of subtrees stored in member variable child_count root's entries are stored in data[0] through data[data_count - 1] If root has sub-trees – subtrees are stored in sets pointed to by subset[0] through subset[child_count - 1]

Searching Make a local variable i equal to the first index such that data[i] is not less than the target – if no such index exists, then set i equal to data_count, indicating that all of the entries are less than the target if (we found the target at data[i]) return 1; else if (the root has no children) return 0; else return subset[i]->count(target)

Searching Example

Inserting Loose insertion Fix Loose insertion problem later might result in MAXIMUM + 1 entries in the nodes Fix Loose insertion problem later 6, 17 6, 17 4 12 19, 22 4 12 18, 19, 22

Loose Inserting Make a local variable i equal to first index such that data[i] is not less than entry – if no such index exists, then set i to data_count if (we found the new entry at data[i]) 2a. Return false with no further work (since the new entry is already in the set). else if (the root has no children) { 2b. Add the new entry to the root at data[i]. (The original entries at data[i] and afterwards must be shifted right to make room for the new entry.) Return true to indicate that we added the entry. else { 2c. Save the value from this recursive call: subset[i]->loose_insert(entry); Then check whether the root of subset[i] now has an excess entry; if so, then fix that problem. Return the saved value from the recursive call. }

Loose Inserting Example 6, 17 4 12 19, 22 MINIMUM = 1 18 ? 6, 17 6, 17, 19 4 12 18, 19, 22 4 12 18 22

Inserting Example 17 6, 17, 19 4 12 18 22 6, 19 4 12, 18 22

Fixing Child with Excess Entry Split child node into nodes containing MINIMUM entries Pass median entry up to parent 3, 6 33, 40 9, 28 13, 16, 19, 22, 25 1, 2 7, 8 4, 5 11, 12 17, 18 14, 15 20, 21 26, 27 23, 24 31, 32 50, 51 34, 35 MINIMUM = 2 9, 19, 28 3, 6 13, 16 22, 25 33, 40 1, 2 4, 5 7, 8 11, 12 14, 15 17, 18 20, 21 23, 24 26, 27 31, 32 34, 35 50, 51

Deleting From B-Tree Loose erase Fix the problem later might leave the root of an internal subtree with fewer than MINIMUM entries Fix the problem later

Loose Erase From B-Tree Make a local variable i equal to the first index such that data[i] is not less than target. If there is no such index, then set i equal to data_count 4 possibilities: Root has no children & did not find entry Done – entry not in tree Root has no children & found entry Remove the entry and return true Root has children & did not find target Recursive call to subtree[i] Root has children & found the target Replace the target by the largest item from subtree[i]

Loose Erase Example ? 28 MINIMUM = 2 2, 5 33, 40 10, 28 13, 16, 19, 22 0, 1 6, 7, 8 3, 4 11, 12 17, 18 14, 15 20, 21 23, 24, 26 31, 32 50, 51 34, 35 MINIMUM = 2 ? 28 2, 5 33, 40 10, 26 13, 16, 19, 22 0, 1 6, 7, 8 3, 4 11, 12 17, 18 14, 15 20, 21 23, 24 31, 32 50, 51 34, 35

Fix Shortage in Child Case 1: Transfer an extra entry from subtree[i-1] - subset[i-1] has more than MINIMUM number of entries Transfer data[i-1] down to front of subset[i] Transfer final item of subtree[i-1] up to replace data[i- 1] If subtree[i-1] has children, transfer final child of subset[i-1] to front of subtree[i] Case 2: Transfer extra entry from subtree[i+1]- Similar to Case 1

Fix Shortage Example ? 40 MINIMUM = 2 2, 5 33 10, 28 13, 16, 19, 22 0, 1 6, 7, 8 3, 4 11, 12 17, 18 14, 15 20, 21 23, 24, 26 31, 32 34, 35 MINIMUM = 2 ? 40 2. The 22 has come up from the middle child. 1. The 28 has come down from the root. 2, 5 28, 33 10, 22 13, 16, 19 0, 1 6, 7, 8 3, 4 11, 12 17, 18 14, 15 20, 21 23, 24, 26 31, 32 34, 35 3. This child has been moved over.

Fix Shortage in Child Case 3: Combine subtree[i]with subtree[i- 1] Transfer data[i-1] down to end of subtree[i-1] Transfer all items & children from subtree[i] to end of subtree[i-1] Delete node subtree[i] & shift subtree[i+1] leftward to fill gap Case 4: Combine subtree[i]with subtree[i+1]- Similar to Case 3

Fix Shortage Example ? 40 MINIMUM = 2 2, 5 33 10, 28 16, 19 0, 1 6, 7, 8 3, 4 17, 18 14, 15 20, 21 31, 32 34, 35 MINIMUM = 2 ? 40 10 2, 5 16, 19, 28, 33 0, 1 3, 4 6, 7, 8 14, 15 17, 18 20, 21 31, 32 34, 35

Time analysis Insert, remove and search times are roughly proportional to the depth of a tree in binary search trees, heaps and B-trees. Binary search trees suffer, since a tree of n entries could have depth n. Heaps and B-trees have depth proportional to log(n), so the operations for a heap or a B-tree is O(log(n)).

Summary A B-tree is a tree for storing entries in a manner that follows six rules. The tree algorithms that we have seen for binary search trees, heaps, and B-trees all have worse- case time complexity of O(d), where d is the depth of the tree. The depth of a heap or B-tree is never more than O(log n), where n is the number of nodes. A quick summary . . .