Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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++

2 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

3 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

4 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

5 General case: Single R-rotation

6 General case: Double LR-rotation

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

8 AVL tree construction - an example (cont.)

9 Analysis of AVL trees h  1.4404 log2 (n + 2) - 1.3277
average height: 1.01 log2n 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)

10 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

11 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

12 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

13 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

14 B-Tree Illustrations https://www.youtube.com/watch?v=coRJrcIYbF4
ree.html

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

16 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]

17 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)

18 Searching Example Searching for 10, 21

19 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

20 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. }

21 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

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

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

24 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

25 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]

26 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

27 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

28 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.

29 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

30 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

31 Time analysis Insert, remove, and search times are roughly proportional to the depth of a tree in B- trees.

32 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 . . .


Download ppt "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."

Similar presentations


Ads by Google