Lecture 16 Multiway Search Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures
Ordered Maps So far List/Array BST AVL Tree
Lets try multiple children… 30 25 40 50 75
Multiple keys at each node! 30 50 25 40 75
How many keys do we need at a node to have d-way search tree?
Confusion Points 1. nodes 2. keys 3. children
Multiway search tree with degree d! internal nodes have at least 2 children and at max d children each node stores upto d-1 items
In-order traversal of multiway tree 30 50 25 29 40 75 80 14 20 26 27
Search 12
Search 24
What is the time complexity of search in a d-way search tree?
O(h*log d)
2-4 Tree a node can have 2 to 4 children all leaf nodes are at the same level
Search complexity? O(h)
Min & Max height (h)?
Minimum number of entries? n≥2h+1-1 h≤log2(n+1)-1
Maximum number of entries? n≤4h+1-1 h≥log4(n+1)-1
log4(n+1)-1 ≤ h ≤ log2(n+1)-1 1/2log2(n+1) ≤ h+1 ≤ log2(n+1) O(log n) 2-4 Tree Height log4(n+1)-1 ≤ h ≤ log2(n+1)-1 1/2log2(n+1) ≤ h+1 ≤ log2(n+1) O(log n)
Insert
Case1: Node has empty space 21 23 40 29 7 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 24 26 28 30 33 37 39
Case2: node is already full split the node 29 7 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 26 28 30 33 37 39 40
Promote one key to the parent! 7 13 22 32 3 8 10 18 25 35 1 2 4 5 6 9 11 12 14 15 20 21 23 24 33 37 39 40 26 28 29 30
Promote one key to the parent! Split the parent! 13 22 32 3 8 10 18 25 28 35 1 2 9 11 12 14 15 20 21 23 24 33 37 39 40 4 5 6 7 26 29 30
Eventually, we may have to create a new root! 5 13 22 32 3 8 10 18 25 28 35 1 2 9 11 12 14 15 20 21 23 24 33 37 39 40 4 6 7 26 29 30
Complexity of insert? o(log n) O(log n) to find the node O(log n) node splits in the worst Node split takes constant time o(log n)
Delete
Claim: We always delete key in the leaf node! 13 5 22 3 8 10 18 25 28 1 2 9 11 12 14 15 20 23 24 4 6 7 26 29 30
Case 1: leaf node has at least 2 keys Delete 21 13 5 22 3 8 10 18 25 28 1 2 9 11 12 14 15 20 21 23 24 4 6 7 26 29 30
Case 1: leaf node has at least 2 keys Delete 25 13 5 22 3 8 10 18 25 28 1 2 9 11 12 14 15 20 23 24 4 6 7 26 29 30
Case 2: leaf node has only 1 key Delete 20 borrow from sibling 13 5 22 3 8 10 18 24 28 1 2 9 11 12 14 15 20 23 4 6 7 26 29 30
What if sibling has only 1 key? Delete 23 Merge siblings 13 5 22 3 8 10 15 24 28 1 2 9 11 12 14 18 23 4 6 7 26 29 30
What if the parent has only 1 key? Delete 18 Cascaded merge 13 5 22 3 8 10 15 28 1 2 9 11 12 14 18 4 6 7 29 30 24 26
2-4 Trees The height of a (2,4) tree is O(log n). Search, insertion and deletion each take O(log n) . Pretty fundamental to the ideas of advanced trees such as Red-Black trees.