Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 Multi-way Trees

2 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 of order m. In a binary tree Each node has only one key and Each node has up to two children In a m-way tree Each node hold at least 1 and at most m-1 keys and Each node has at most m children

3 B-Tree An example of m-way tree is called B-trees A B-tree of order m is a multiway search tree with the following properties The root has at least two subtrees unless it is a leaf Except the root and the leaves, every other node has at most m children and at least m/2 children This means every node hold at most m-1 keys and at least (m/2) -1 keys For example, a tree of order m=5, holds at least 2 and at most 4 keys and 5 pointers. Similarly, a tree of order m=8, hold at least 3 and at most 7 keys in each node and has 8 pointers. Every leaf node holds at most m-1 keys and at least (m/2) -1 keys All leaves are on the same level

4 Inserting a key into a B-Tree Some of the differences of B-trees compare to binary trees is that: All leaves are in the last level of the tree. Note that this was not necessarily the case for the binary tree The tree is built bottom-up rather than up to bottom as it is in binary trees In general there are three cases to consider when we insert a element into a B-tree of order m.

5 Insertion into B Tree: Case 1: A key is placed in a leaf that still has some room For example, as shown in the following example, in a B- tree of order 5, a new key, 7, is placed in a leaf, preserving the order of keys in the leaf so that key 8 must be shifted to the right by one position 12 13 8 5 15 12 13 7 5 15 8

6 Insertion into B Tree: Case 2: The leaf in which a key should be placed is full. In this case the leaf is split, creating a new leaf, and half of the keys are moved from the leaf full node to the new leaf. The last key of the old leaf is moved to the parent and a pointer to the new leaf is placed in the parent as well. 12 135215 12 5 2 8 7 15 13 12 6 5 2 8 7 13 15 6 move 78 Want to insert 6

7 Insertion into B Tree: Case 3: Suppose you want to insert a key into a full node Because the node is full, the node is split and the middle key is moved to the parent node as we explained in case 2. What if the parent has no more room? If parent has no more room, we need to split the parent node, create two nodes and move the middle key up the tree into the parent of the parent. If parent of the parent does not exist, we create one If parent of the parent has room, we insert the middle key there If parent of the parent is also full, we repeat the same process again

8 6 12 20 30 234 5 78 1011 1415 18 192123 25 2831333435 6 12 20 30 234 5 78 1011 1314 15 2123252831333435 18 19 Move 6 12 234 5 78 1011 1314 2123252831333435 18 19 Insert 13 15 20 30

9 Algorithm for inserting into B-Tree BTreeInsert(K) Find a leaf node to insert While (true) { Find a proper position in the leaf for K; If there is space in that node Insert K in proper position Return Else split in node in node1 and node2 Distribute keys and pointers evenly between node1 and node2 K = the last key of node1 If node was the root Create a new root as parent of node1 and node2 Put K and pointers to node1 and node2 in the root Return Else node = its parent // now process the parent node if it is full }

10 After inserting 3 8 14 15 2 3 2 8 14 15 After inserting 8, 14, 2, and 15 Step by step of insertion into a B-Tree Insert 8, 14, 2, 15, 3, 1, 16, 6, 5, 27, 37, 18, 25, 7, 13, 20, 22, 23, 24 into a tree of order m = 5

11 After inserting 5 14 15 16 1 2 5 6 3 8 After inserting 1, 16, 6 8 14 15 16 1 2 3 6

12 1 2 5 6 3 8 14 15 16 27 After inserting 37 27 37 1 2 5 6 3 8 After inserting 27 14 15 27 16 27

13 1 2 5 6 3 8 14 15 16 27 After inserting 20 20 18 7 13 27 37 25 1 2 5 6 3 8 14 15 16 27 After inserting 18, 25, 7, 13 27 37 25 18 7 13

14 After inserting 24 1 2 3 8 24 23 27 37 5 6 7 13 14 15 20 18 22 25 16 After inserting 22, 23 1 2 5 6 3 8 14 15 16 27 20 18 7 13 27 37 25 22 23

15 Another example: This time we want to insert a set of numbers into a tree of order m = 4. For order 4, the number of keys in each node is at least 1 and at most 3. Insert 8, 14, 2, 15, 3, 1, 16, 6, 5, 27, 37, 18, 25, 7, 13, 20, 22, 23, 24 After inserting 15 2 14 After inserting 8, 14, and 2 8 2 14 8 15

16 After inserting 3, 1, and 16 2 14 8 15 31 16 After inserting 6 14 8 15 1 16 63 2

17 After inserting 5 14 8 15 1 16 53 2 6 After inserting 27 14 8 15 1 53 2 6 16 27

18 After inserting 37, 13, 12 12 8 15 1 53 2 6 16 27 3713 14 After inserting 20 12 1 53 8 6 16 13 14 27 37 2 20 15

19 Another example: This time we present the B-tree with more detail that shows how the index keys are connected to specific records in the disk. Suppose we want to create indexing using B-tree of order m = 3 for the following employee records in the disk Assuming that the EmpId is unique in the employee table, the best index key can be the EmpId This example shows step by step of creating B-tree of order m=3 and illustrates how the pointers are linked to the records in the disk 2Jack30,000 80Steve32,000 8John50,000 71Nancy55,000 15Rose90,000 63Abdul35,000 90Pat42,000 55Kathy45,000 35Melissa38,000 51Joe39,000 EmpIdNameSalary

20 Insert index for record: 2Jack30,000 2 Null Pointer Before After

21 Insert index for record: 80Steve32,000 2802 Before After

22 Insert index for record: 8John50,000 8 280 2 Before After

23 Insert index for record: 71Nancy55,000 8 27180 8 2 Before After

24 Insert index for record: 15Rose90,000 8 21580 71 8 2 80 Before After

25 Insert index for record: 63Abdul35,000 8 21580 71 63 8 21580 71 Before After

26 Insert index for record: 90Pat42,000 8 21580 71 6390 8 21580 71 63 Before After

27 Insert index for record: 55Kathy45,000 55 871 15 63 80 90 2 8 21580 71 6390 Before After

28 Insert index for record: 35Melissa38,000 55 871 15 63 80 90 2 35 55 871 15 63 80 90 2 Before After

29 Insert index for record: 51Joe39,000 55 871 15 632 80 90 51 35 55 871 15 63 80 90 2 35 Before After

30 55 8 71 15 63 2 80 90 51 35 2Jack30,000 80Steve32,000 8John50,000 71Nancy55,000 15Rose90,000 63Abdul35,000 90Pat42,000 55Kathy45,000 35Melissa38,000 51Joe39,000 EmpIdNameSalary

31 Deleting from a B-tree For the delete operation, there are two general cases: Deleting a key from the leaf Deleting a key from a non-leaf Case 1: Deleting from a leaf node: If after deleting a key K, the leaf is at least half full, simply delete the element

32 If after deleting, the number of keys in the leaf is less than (m/2) -1, causing an underflow: If there is a left or right sibling with the number of keys exceeding the minimal (m/2) -1, then all keys from this leaf and this sibling are redistributed between the two nodes 1 2 5 3 8 14 15 27 7 13 Before deleting 7 1 2 5 3 8 14 15 27 13 After deleting 7

33 If after deleting, the number of keys in the leaf is less than (m/2) -1, causing an underflow: If neither left no right sibling have more than minimal f (m/2) -1, then merge the node with one of the siblings and place proper index in the parent node Before deleting 8 1 2 3 5 13 27 After deleting 8 1 2 5 3 8 14 15 27 13 1415 merge

34 A particular case results in merging a leaf or nonleaf with its sibling when its parent is the root with only one key. In this case, the keys from the node and its sibling, along with the only key of the root, are put in the node which becomes a new root, and both the sibling and the old root nodes are discarded. This is the only case when two nodes disappear at the same time. Also the height of the tree is decreased by one See the next example.

35 1 2 3 13 Before Deleting 8 24 23 27 37 5 8 14 15 20 18 22 25 16 1 2 3 1 is deleted but process continues 24 23 27 37 14 15 20 18 22 25 16 3 13 5 1 2 24 23 27 37 14 15 20 18 13 5 162225 merge After deleting 8

36 Case 2: Deleting from a non-leaf node This can lead to problems with reorganization. Therefore, deleting from a nonleaf node should be reduced to deleting a key from a leaf to make the task simple The key to be deleted is replaced by its immediate predecessor (the successor could also be used) which can only be found in a leaf. This predecessor key is deleted from the leaf based on the algorithm we discussed in case 1

37 3 1 2 24 23 27 37 14 15 20 18 13 5 162225 Before deleting 16 3 1 2 24 23 27 37 14 16 20 18 13 5 152225 Swap 16 and 15 and delete 16 3 1 2 24 23 27 37 14 20 18 13 5 152225 After deleting 16

38 B-Tree delete algorithm Node = Search for the node that contains key K to be deleted; If (node is not a leaf) Find a leaf with the closest successor/predecessor S of K Copy S over K in node; Node = the leaf containing S Delete S from node Else delete K from node; While (1) { If node does not underflow Return else if there is a sibling of node with enough keys (i. e. more than (m/2)-1) Redistribute keys between node and its sibling Return else if node’s parent is the root If the parent has only one key Merge node, its sibling, and parent to form a new root else merge node and its sibling Return else merge node and its sibling node = its parent }

39 B* Tree A “B*-Tree” is a variant of the B-Tree. All the nodes except the root are required to be at least two-third full. More precisely, the number of keys in all nodes except the root in a B*-tree of order m is k where (2m-1/3) <=k <= m-1 In this type of tree, the frequency of node splitting is decreased by delaying a split and when the time comes we split two nodes into three (not one node into two as done in B-tree) Lets see some examples of inserting into a B* tree

40 16 0 1 12 10 18 25 30 2 759 2728 Before inserting 6 After inserting 6 0 1 10 18 25 30 2 65 9 27 28 7 12 16

41 B* Tree – Cont. As shown in the previous slide, the key 6 is to be inserted into the left node which is already full Instead of splitting the node, all keys from this node and its sibling are evenly divided and the median key, key 10, is put into the parent Notice that this not only evenly divides the keys, but also it frees some space in the nodes for more key If the sibling is also full, a split occurs and one new node is created, the keys from the node and its sibling (along with the separating keys from the parent) are evenly divided among three nodes and two separating keys are put into the parent See the next slide for an example.

42 10 0 1 9 8 12 16 27 28 2 657 30 29 1825 Before inserting 4 6 0 1 7 8 12 2 4 910 After inserting 4 18 25 29 30 2728 166 5

43 B+ Tree Basically, a node in a B-tree structure represents one secondary page or a disk block The passing from one node to another node can be a time consuming operation in case we need to do something like in- order traversal or print of the B-tree B+ tree is enhanced form of B-tree that allow us to access data sequentially in a faster manner than using in order traversal In a B-tree, references to data are made from any node of the tree but in a B+ tree, these references are made only from the leaves The internal nodes of a B-tree are indexes to the leaves for fast access to the data

44 B+ Tree Cont. In a B+ tree, the leaves have a different structure than other nodes of the B+ tree and usually they are linked sequentially to form a sequence set so that scanning this list of leaves results in data given in ascending order The reason this is called B+ tree is that The internal nodes (not the leaves) all have the same structure as the B-tree) plus The leaves make a linked list of the keys Thus we can say that B+ tree is a combination of indexes plus a linked list of keys The internal node of B+ tree stores keys, and pointers to the next level nodes The leaves store keys, references to the records in a file, and pointer to the next leaf.

45 Algorithm for inserting into a B+ tree During the insert, when a leaf node is full and a new entry is inserted there, the node overflows and must split Given that the order of the B + -tree is p, the split of a leaf node causes the first p/2 entries (index keys) to remain in the original node and the rest move to the new node A copy of the middle key is placed into the parent node If the parent (non-leaf node) is full and we try to insert a new key there, the parent splits. Half of the nodes stay in the original node, the other half move to the new node, and the middle key is moved (not copied) to the parent node (just like B-tree). This process can be propagated all the way up to the root. In the next example, we go through step by step (with pointer details) of inserting into a B+ tree

46 Example of a B + -Tree of order 3 Example of inserting the index for the following records into a B + -tree 8Jack30,000 5Steve32,000 1John50,000 7Nancy55,000 3Rose90,000 12Abdul35,000 9Pat42,000 6Kathy45,000 EmpIdNameSalary

47 Insert index for record: 8Jack30,000 8 Null Pointer Before After

48 Insert index for record: 5Steve32,000 588 Before After

49 Insert index for record: 1John50,000 5 158 58 Before After

50 Insert index for record: 7Nancy55,000 5 157 8 5 158 Before After

51 Insert index for record: 3Rose90,000 3 57813 5 5 1578 Before After

52 Insert index for record: 12Abdul35,000 3 5781312 8 5 3 57813 5 Before After

53 Insert index for record: 9Pat42,000 3 578139 8 5 12 3 57813 8 5 Before After

54 Insert index for record: 6Kathy45,000 3 57139 8 5 12 Before 3 56139 7 5 128 8 After 7 8

55 8Jack30,000 5Steve32,000 1John50,000 7Nancy55,000 3Rose90,000 12Abdul35,000 9Pat42,000 6Kathy45,000 EmpIdNameSalary 3 6 7 1 3 9 7 5 12 8 8 5

56 Deleting from B+ tree If the deleting of a key does not cause underflow, we just have to make sure other keys are properly sorted Even if the index of the key to be deleted is in the internal node, the index can still be there because it is just a separator 3 57813 5 3 5781 5 After Deleting 3 Before Deleting 3

57 When delete of a node from a leaf causes an underflow, then either the keys from this leaf and the keys of a sibling are redistributed between this leaf and its sibling or the leaf is deleted and the remaining keys are included in the sibling 3 57138 7 5 After Deleting 12 3 5781312 8 5 Before Deleting 12

58 Trie In the previous examples we have used the entire key (not just part of it) to do searching of an index or an element A tree that uses parts of the key to navigate the search is called a trie (pronounced “try”) Each key is a sequence of characters and a trie is organized around these characters rather than entire keys Suppose that all keys are made of 5 letters A, E, I, P and R The next slide shows an example of a trie. For example, search for word “ERIE”, we first check the first level of trie, the pointer corresponding to the first letter of this word “E” is checked Since this pointer is not null, the second level is checked. Again it is not null and we follow the pointer from letter “R” Again other levels are checked till you either find the word or you reach NULL.

59 # A E IP R # A E IP R # A E IP R # A E IP R # A E IP R # A E IP R # A E IP R # A E IP R # A E IP R # A E IP R # A E IP R AraAra AreaArea EraEra EIreEIre IPAIPA A AreAre IREIRE RearRear RepRep PierPier PearPear PeerPeer PerPer EreEre ErieErie


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

Similar presentations


Ads by Google