Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two and at most d children and stores d -1 data items (k i, D i ) Rule: Number of children = 1 + number of data items in a node k 1 D 1 k 2 D 2... k d-1 D d-1 child 1 child 2 child child d d is the degree or order of the tree
Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two and at most d children and stores d -1 data items (k i, D i ) An internal node storing keys k 1 k 2 k d-1 has d children v 1 v 2 v d such that k 1 k 2... k d-1 < k 1 > k 1 < k 2 > k 2 < k > k d-2 < k d-1 > k d-1 (2,4) Trees 2
Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two and at most d children and stores d -1 data items (k i, D i ) An internal node storing keys k 1 k 2 k d-1 has d children v 1 v 2 v d such that By convenience we add sentinel keys k 0 = - and k d = k 0 = - k 1 k 2... k d-1 k d = < k 1 > k 1 < k 2 > k 2 < k > k d-2 < k d-1 > k d-1 (2,4) Trees
Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two and at most d children and stores d -1 data items (k i, D i ) An internal node storing keys k 1 k 2 k d-1 has d children v 1 v 2 v d such that By convenience we add sentinel keys k 0 = - and k d = The leaves store no items and serve as placeholders 11 24 2 6 8 15 27 2 0 (2,4) Trees 4
Multi-Way Search Tree 27 41 5 20 2 2 9 47 57 59 6 1 50 1 17 (2,4) Trees 5
Multi-Way Search Tree 27 41 5 15 2 2 9 47 57 59 6 1 50 1 17 Not a multiway search tree (2,4) Trees 6
Multi-Way Inorder Traversal We can extend the notion of inorder traversal from binary trees to multi-way search trees An inorder traversal of a multi-way search tree visits the keys in increasing order 11 24 2 6 8 15 27 2 0 Inorder traversal: 2, 6, 8, 11, 15, 24, 27, 0, 2 (2,4) Trees 7
Data Structures for Multi-Way Search Trees k 0 = - k 1 k 2... k d-1 k d = child 1 child 2 child child d node null D 1 D 2... D d-1 null data - k 1 k 2... k d-1 keys ch1 ch 2 ch ch d children 0 1 2 d-1 (2,4) Trees 8
Data Structures for Multi-Way Search Trees k 0 = - k 1 k 2... k d-1 k d = child 1 child 2 child child d null D 1 D 2... D d-1 null data - k 1 k 2... k d-1 keys ch1 ch 2 ch ch d children 0 1 2 d-1 Secondary data structures (2,4) Trees 9
Multi-Way Searching Similar to search in a binary search tree Example: search for 1 27 41 5 20 2 2 9 47 57 59 6 1 50 (2,4) Trees 10
Multi-Way Searching Similar to search in a binary search tree Example: search for 46 27 41 5 20 2 2 9 47 57 59 6 1 50 (2,4) Trees 11
Multi-Way Searching Algorithm get(r,k) In: Root r of a multiway search tree, key k Out: data for key k or null if k not in tree if r is a leaf then return null else { } Use binary search to find the index i such that r.keys[i] k < r.keys[i+1] if k = r.keys[i] then return r.data[i] else return get(r.child[i],k) (2,4) Trees 12
Multi-Way Searching Algorithm get(r,k) In: Root r of a multiway search tree, key k Out: data for key k or null if k not in tree if r is a leaf then return null else { } Use binary search to find the index i such that r.keys[i] k < r.keys[i+1] if k = r.keys[i] then return r.data[i] else return get(r,r.child[i]) c operations Ignoring recursive calls: c 1 log d + c 2 operations (2,4) Trees 1
Time Complexity of get Operation c1 + c2 log d c1 + c2 log d c1 + c2 log d... c f(n) = c + (c1 + c2 log d) height of tree is O(log d height of tree) (2,4) Trees 14
Smallest Operation c c c k s... c f(n) = c height of tree is O(height of tree) (2,4) Trees 15
Successor Operation 27 41 5 20 2 2 9 47 57 59 6 1 50 1 17 smallest value in subtree successor (27) (2,4) Trees 16
Successor Operation 27 41 5 20 2 2 9 47 57 59 6 1 50 1 17 successor (17) (2,4) Trees 17
Successor Operation 27 41 5 20 2 2 9 47 57 59 6 1 50 1 17 Time complexity: get + smallest: O(log d height), or get + travel up: O(log d height) so time complexity is O(log d height) (2,4) Trees 18
Put Operation 27 41 5 20 2 2 9 47 57 59 6 1 50 1 17 insert (51) Assume degree = 4 (2,4) Trees 19
Put Operation 27 41 5 20 2 2 9 47 51 57 59 6 1 50 1 17 Time Complexity: Find node to store new data: O(log d height) Add data to node: O(d) time complexity: O(d+log d height) (2,4) Trees 20
Remove Operation 27 41 5 20 2 2 9 47 57 59 6 1 50 1 17 remove(50) (2,4) Trees 21
Remove Operation 27 41 5 20 2 2 9 47 57 59 6 1 50 1 17 remove(50) (2,4) Trees 22
Remove Operation 27 41 5 20 2 2 9 47 57 59 6 1 1 17 remove(50) (2,4) Trees 2
Remove Operation 27 41 5 20 2 2 9 47 57 59 6 1 1 17 remove(27) (2,4) Trees 24
Remove Operation 1 41 5 20 2 2 9 47 57 59 6 1 17 remove(27) (2,4) Trees 25
Remove Operation 1 41 5 20 2 2 9 47 57 59 6 1 17 Time complexity: get + smallest + remove data + delete 2 nodes: O(d+log d height) (2,4) Trees 26