Lecture 26 Multiway Search Trees Chapter 11 of

  • Slides: 15
Download presentation
Lecture 26 Multiway Search Trees Chapter 11 of textbook 1. Multiway Tree 2. B-tree

Lecture 26 Multiway Search Trees Chapter 11 of textbook 1. Multiway Tree 2. B-tree © Oxford University Press 2014. All rights reserved.

1. Multiway search tree • m-way (m-ary) search tree is generalization of binary search

1. Multiway search tree • m-way (m-ary) search tree is generalization of binary search tree. Binary search tree is a 2 -way search tree. P 0 K 0 P 1 K 1 P 2 K 2 ……… Pn-1 Kn-1 Pn • In the structure, P 0, P 1, P 2, …, Pn are pointers to the node’s sub-trees and K 0, K 1, K 2, …, Kn-1 are the key values of the node. All the key values are stored in ascending order. That is, Ki < Ki+1 for 0 ≤ i ≤ n-2 – Node has n key values, and n+1 pointers, n+1 <= m – All key values in subtree of Pi are less than Ki – All key values in subtree of Pi+1 are bigger than Ki © Oxford University Press 2014. All rights reserved.

Multiway search tree • A node can have anywhere from 1 to (m-1) values,

Multiway search tree • A node can have anywhere from 1 to (m-1) values, and the number of sub-trees may vary from 0 (for a leaf node) to 1 + i, where i is the number of key values in the node. • m is a fixed upper limit that defines how much key values can be stored in the node. • Out degree of an m-way search tree is at most m. • m-way search tree is a generalization of binary search tree. i. e. when m = 2, a 2 -way search tree is a BST. © Oxford University Press 2014. All rights reserved.

Example of 3 -way search tree 18 9 11 45 27 29 36 30

Example of 3 -way search tree 18 9 11 45 27 29 36 30 54 72 63 81 © Oxford University Press 2014. All rights reserved.

Multiway tree is used in building Unix file system

Multiway tree is used in building Unix file system

Operations on m-way search tree Operations on BST can be generalized to M-way search

Operations on m-way search tree Operations on BST can be generalized to M-way search trees • • Search Insert Delete …. © Oxford University Press 2014. All rights reserved.

2. B-Trees A B-tree of order m is a self-balanced m-way search tree with

2. B-Trees A B-tree of order m is a self-balanced m-way search tree with following properties 1. Every node has at most m children 2. Every node in the B-tree except the root node and leaf nodes have at least (minimum) m⁄2 children. 3. The root node has at least two children if it is not a terminal (leaf) node. 4. All leaf nodes are at the same level. © Oxford University Press 2014. All rights reserved.

Example of B-Tree with m = 4 O O 29 O 32 30 O

Example of B-Tree with m = 4 O O 29 O 32 30 O O O 18 O 27 31 O O O 45 46 O 47 O 49 O O 63 54 O O 59 O 61 O 36 O 39 O O © Oxford University Press 2014. All rights reserved. O O 67 O 72 O

Insertion operation on B-Trees Algorithm Step 1. Search the B tree to find the

Insertion operation on B-Trees Algorithm Step 1. Search the B tree to find the leaf node where the new key value should be inserted. Step 2. If the node is not full, that is it contains less than m-1 key values then insert the new element in the node, keeping the node's elements ordered. Step 3. If the node is full, insert the new value in order into the existing set of keys, split the node at its median into two nodes. note that the split nodes are half full. Insert the median element to its parent’s node. Go to step 2. © Oxford University Press 2014. All rights reserved.

Example of B-Tree insertion Insert 17 https: //webdocs. ualberta. ca/~holte/T 26/ins-b-tree. html

Example of B-Tree insertion Insert 17 https: //webdocs. ualberta. ca/~holte/T 26/ins-b-tree. html

Example of B-Tree insertion Insert 6 © Oxford University Press 2014. All rights reserved.

Example of B-Tree insertion Insert 6 © Oxford University Press 2014. All rights reserved.

Example of B-Tree insertion Insert 21 Insert 67 © Oxford University Press 2014. All

Example of B-Tree insertion Insert 21 Insert 67 © Oxford University Press 2014. All rights reserved.

B-tree Deletion Algorithm Step 1. Locate the leaf node which has to be deleted

B-tree Deletion Algorithm Step 1. Locate the leaf node which has to be deleted Step 2. If the leaf node contains more than minimum number of key values (more than m/2 elements), then delete the value. Step 3. else if the leaf node does not contain even m/2 elements then, fill the node by taking an element either from the left or from the right sibling Step 3. 1 If the left sibling has more than the minimum number of key values (elements), push its largest key into its parent’s node and pull down the intervening element from the parent node to the leaf node where the key is deleted. Step 3. 2 else if the right sibling has more than the minimum number of key values (elements), push its smallest key into its parent node and pull down the intervening element from the parent node to the leaf node where the key is deleted. © Oxford University Press 2014. All rights reserved.

B-tree Deletion Step 4. else if both left and right siblings contain only minimum

B-tree Deletion Step 4. else if both left and right siblings contain only minimum number of elements, then create a new leaf node by combining the two leaf nodes and the intervening element of the parent node (ensuring that the number of elements do not exceed the maximum number of elements a node can have, that is, m). If pulling the intervening element from the parent node leaves it with less than minimum number of keys in the node, then propagate the process upwards thereby reducing the height of the B tree. • To delete an internal node, promote the successor or predecessor of the key to be deleted to occupy the position of the deleted key. This predecessor or successor will always be in the leaf node. So further the processing will be done as if a value from the leaf node has been deleted. © Oxford University Press 2014. All rights reserved.

Example of B-tree Deletion Delete 3 © Oxford University Press 2014. All rights reserved.

Example of B-tree Deletion Delete 3 © Oxford University Press 2014. All rights reserved.