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.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
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.
They’re not just binary anymore!
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
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.
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.
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.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
COMP261 Lecture 23 B Trees.
TCSS 342, Winter 2006 Lecture Notes
AA Trees.
File Organization and Processing Week 3
CSCE 3110 Data Structures & Algorithm Analysis
B-Trees B-Trees.
Data Structures and Algorithms for Information Processing
B-Trees B-Trees.
B-Trees B-Trees.
CSC212 Data Structure - Section AB
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
UNIT III TREES.
Binary Search Tree (BST)
Chapter 6 Transform-and-Conquer
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Introduction Applications Balance Factor Rotations Deletion Example
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search Tree Chapter 10.
Height Balanced Trees CPSC 335 Dr. Marina Gavrilova Computer Science
Lecture 22 Binary Search Trees Chapter 10 of textbook
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
(edited by Nadia Al-Ghreimil)
Monday, April 16, 2018 Announcements… For Today…
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 6 Transform and Conquer.
Wednesday, April 18, 2018 Announcements… For Today…
Lecture 26 Multiway Search Trees Chapter 11 of textbook
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CS202 - Fundamental Structures of Computer Science II
Tree Representation Heap.
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.
Data Structures & Algorithms
CSIT 402 Data Structures II With thanks to TK Prasad
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
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)
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
CSC 143 Binary Search Trees.
Priority Queues (Chapter 6):
การวิเคราะห์และออกแบบขั้นตอนวิธี
B-Trees.
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.
CS202 - Fundamental Structures of Computer Science II
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
CS202 - Fundamental Structures of Computer Science II
Heaps.
Presentation transcript:

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 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 --- AVL tree 2. Leaves cannot become too deep --- B-tree 1 2 3 4 5

Tree (a) is an AVL tree; tree (b) is not an AVL tree AVL trees Definition An AVL tree is a binary search tree in which, for every node, the difference between the heights of its left and right subtrees, called the balance factor, is at most 1 (with the height of an empty tree defined as -1) Tree (a) is an AVL tree; tree (b) is not an AVL tree

Rotations If a key insertion violates the balance requirement at some node, the subtree rooted at that node is transformed via one of the four rotations. (The rotation is always performed for a subtree rooted at an “unbalanced” node closest to the new leaf.) Single R-rotation Double LR-rotation Single L-rotation Double RL-rotation

General case: Single R-rotation

General case: Double LR-rotation

AVL tree construction - an example Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7

AVL tree construction - an example (cont.)

Analysis of AVL trees h  1.4404 log2 (n + 2) - 1.3277 average height: 1.01 log2n + 0.1 for large n (found empirically) Search and insertion are O(log n) Deletion is more complicated but is also O(log n) Disadvantages: frequent rotations complexity A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)

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 deep

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 non-leaf node is 1 more than the number of entries in the node

B-Tree Rules 5. For any non-leaf 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 Searching for 10, 21

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

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

B-tree construction – an example Construct a B-tree of the list 9, 5, 8, 3, 2, 4, 7

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 remove 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 ? remove 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 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 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 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 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 B- trees.

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, AVL trees, 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, AVL tree, or B-tree is never more than O(log n), where n is the number of nodes. A quick summary . . .