Download presentation
Presentation is loading. Please wait.
Published byLeon Campbell Modified over 9 years ago
1
INTRODUCTION TO MULTIWAY TREES P. 855 - 873
2
INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often, this is inefficient, particularly when need to use secondary storage (i.e, disk, tape) to store data for quick retrieval - since secondary storage is slower than main memory, need to measure performance also on how many disk access are performed - the trees used must be designed to keep the height of the tree low (since # of disk accesses increases with height of the tree) - several types of trees to be used with disk storage: -2-3-4 trees, B-Trees, and Multiway Trees
3
MULTIWAY SEARCH TREES to review, binary search trees: –each node has at most two children and one value –value in the node partitions the values in the subtrees (all left-children of node < value of node; –all right-children of node > value of node) in Multiway Search Tree of order m: –each node has at most m children –if a node has k children (where k <= m) it has k-1 values in the node these values partition the values in the subtrees –searching of a multiway tree similar to binary tree, except that search over the keys (or values) within a node is required to determine which branch to take –more children mean less depth for a given number of keys (giving less disk accesses) –however, more to search within a node (due to more values in node)
4
MULTIWAY SEARCH TREES - Example of a Multiway Search Tree (order 4): 21 48 71 12 1525 31 415984 91 1 4 813 1416 18 1972 7888 8992 98 99
5
B-TREES - B-Trees are disk-based trees, designed to pack as much info into a block as possible, reducing the number of disk-accesses needed -higher the order of the tree, smaller number of comparisons to find a key - A B-Tree of order m: -multiway search tree -root has at least two subtrees and at most m children, unless it is a leaf -each nonroot and nonleaf node holds k-1 keys, and k pointers to at least m/2 children and at most m children -each leaf node has k-1 keys, where m/2 <= k <= m
6
B-TREES (cont.) -Benefits of B-Trees: 1) tree is height-balanced 2) height of the tree can be smaller than a binary tree 3) all internal nodes except root are at least half full 4) all leaves at same level 5) each node can represent hugh chunks of data 6) updates and searches affect only a few sections of the disk (good performance)
7
B-TREES (cont.) Example of a B-Tree of order 4: 7 2 3 6 0 14 59 1012 13 14 8 11 15
8
B-TREES (cont.) Inserting into a B-Tree: must maintain: 1) height balance 2) all leaves at same level 3) all internal nodes (except root) should be at least half full 4) all leaves should be at least half full when inserts made, tree grows upwards (updates are made from the bottom up) two cases must be checked with each add 1) Is node full? 2) Is parent full?
9
B-TREES (cont.) Algorithm for B-Tree Insert: 1) add the new value to the appropriate leaf 2) if node is now full: a) split node into two nodes: i) let x be the middle value in the node ii) let the two new nodes contain the values x respectively iii) move x up to the parent of the original node b) if parent is now full: i) Go to Step 2 c) if no parent: i) create a new node containing x best way to understand this is with an example
10
B-TREE (Order 5) Example: - Insert: A, G, F, B - Insert: K: - Insert: D, H, M: A B F G A B F G K F G H K MA B D
11
B-TREE (Order 5) Example: - Insert: J: - Insert: E, S, I, R: F J G HA B DK M F J G H IA B D EK M R S
12
B-TREEE (order 5) Example: - Insert: X - Insert: C, L, N, T, U F J R G H IA B D EK MS X C F J R G H IA BK L M NS T U XD E
13
B-TREE (order 5) Example: - Insert: P C F G H IA BK LS T U XD E J M R N P
14
B-TREE Discussion: - when a node is split, the two new nodes are only half full, so future insertions will likely not require splits - since the middle is pushed upwards on a split, the tree tends to balance itself, regardless of the order of insertions - if the order of the tree is even (i.e, 4), then the node must be split into two unequal sized groups - the larger group can be either the left- or right-node (left-biased or right-biased)
15
B-TREES – DELETING NODES - reverses insertion, merging nodes if needed to maintain a minimun size - Steps: -A) Deleting a key from a non-leaf -1) Copy the immediate successor (or predecessor) of the key into its place -2) Delete the successor (or predecessor) from its original spot -B) Delete a key from a leaf: -1) Delete the key, shifting other keys over if needed -2) Examine the result: -a) If node is still at least half-full, done -b) if node less than half full: -1) pull key from left or right sibling with more than minimun # of keys -2) if no sibling with more than minimum -merge node with a sibling, apply previous rules to maintain size
16
B-TREES (SUMMARY) depth of a B-Tree is at most O(log m/2 n), when all nodes have m/2 values or more worst case for a search is then O(log M) studies have shown that order 3 or 4 are optimal values for disk storage and retrieval for main memory storage if using B-Trees for Disk storage, larger M values have shown to be more optimal larger amounts of data can be grouped together and retrieved at the same access speed) if higher values of the B-Tree can be stored in cache memory, disk search performance can be further increased
17
QUESTIONS? READ GRAPHS SECTION P. 711 - 717
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.