Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16 Multiway Search Trees

Similar presentations


Presentation on theme: "Lecture 16 Multiway Search Trees"— Presentation transcript:

1 Lecture 16 Multiway Search Trees
Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures

2 Ordered Maps So far List/Array BST AVL Tree

3 Lets try multiple children…
30 25 40 50 75

4 Multiple keys at each node!
30 50 25 40 75

5 How many keys do we need at a node to have d-way search tree?

6 Confusion Points 1. nodes 2. keys 3. children

7 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

8 In-order traversal of multiway tree
30 50 25 29 40 75 80 14 20 26 27

9 Search 12

10 Search 24

11 What is the time complexity of search in a d-way search tree?

12 O(h*log d)

13 2-4 Tree a node can have 2 to 4 children
all leaf nodes are at the same level

14 Search complexity? O(h)

15 Min & Max height (h)?

16 Minimum number of entries?
n≥2h+1-1 h≤log2(n+1)-1

17 Maximum number of entries?
n≤4h+1-1 h≥log4(n+1)-1

18 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)

19 Insert

20 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

21 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

22 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

23 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

24 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

25 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)

26 Delete

27 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

28 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

29 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

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

31 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

32 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

33 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.


Download ppt "Lecture 16 Multiway Search Trees"

Similar presentations


Ads by Google