Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7.

Similar presentations


Presentation on theme: "© 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7."— Presentation transcript:

1 © 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7

2 2 Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d  1 key-element items (k i, o i ), where d is the number of children For a node with children v 1 v 2 … v d storing keys k 1 k 2 … k d  1  keys in the subtree of v 1 are less than k 1  keys in the subtree of v i are between k i  1 and k i (i = 2, …, d  1)  keys in the subtree of v d are greater than k d  1 The leaves store no items and serve as placeholders 11 24 2 6 815 30 27 32

3 3 Multi-Way Inorder Traversal We can extend the notion of inorder traversal from binary trees to multi-way search trees Namely, we visit item (k i, o i ) of node v between the recursive traversals of the subtrees of v rooted at children v i and v i    1 An inorder traversal of a multi-way search tree visits the keys in increasing order 11 24 2 6 815 30 27 32 13579111319 1517 2461418 812 10 16

4 4 Multi-Way Searching Similar to search in a binary search tree A each internal node with children v 1 v 2 … v d and keys k 1 k 2 … k d  1 k  k i (i = 1, …, d  1) : the search terminates successfully k  k 1 : we continue the search in child v 1 k i  1  k  k i (i = 2, …, d  1) : we continue the search in child v i k  k d  1 : we continue the search in child v d Reaching an external node terminates the search unsuccessfully Example: search for 30 11 24 2 6 815 30 27 32

5 5 2-3-4 Trees A 2-3-4 tree (also known as 2-4 tree) is a multi-way search with the following properties Node-Size Property: every internal node has at least 2 and at most 4 children Depth Property: all the external nodes have the same depth 10 15 24 2 81227 3218 Perfectly balanced! 4-node 3-key node 3-node 2-key node

6 6 Height of a 2-3-4 Tree Theorem: A 2-3-4 tree storing n items has height O(log n) Proof: Let h be the height of a 2-3-4 tree with n items Since there are at least 2 i and at most 4 i items at depth i  0, …, h  1 and no items at depth h, we have Searching in a 2-3-4 tree with n items takes O(log n) time 1 2 2h12h1 0 items 0 1 h1h1 h depth

7 Multi-way Search Trees: Find 7 Find 12 Find 24

8 Update of 2-3-4 Trees Bottom-up approach (in the text book) Find, insert, delete: worse-case O(logn) time Top-down approach Same time complexity with a smaller constant factor Recording by Jonathan Shewchuk 8

9 9 Insert: Bottom-Up We insert a new item (k, o) at the parent v of the leaf reached by searching for k We preserve the depth property but We may cause an overflow (i.e., node v may become a 5-node) Example: inserting key 30 causes an overflow 27 32 35 10 15 24 2 81218 10 15 24 2 812 27 30 32 35 18 v v

10 10 Overflow and Split We handle an overflow at a 5-node v with a split operation: let v 1 … v 5 be the children of v and k 1 … k 4 be the keys of v node v is replaced nodes v' and v"  v' is a 3-node with keys k 1 k 2 and children v 1 v 2 v 3  v" is a 2-node with key k 4 and children v 4 v 5 key k 3 is inserted into the parent u of v (a new root may be created) The overflow may propagate to the parent node u 15 24 12 27 30 32 35 18 v u v1v1 v2v2 v3v3 v4v4 v5v5 15 24 32 12 27 30 18 v'v' u v1v1 v2v2 v3v3 v4v4 v5v5 35 v"v"

11 More Insert Example 11

12 Cascading Splits 12

13 Examples of Bottom-Up Insert Insert 5, 6, 7, 4, 3, 2, 1, 8, 9, 10 into a 2-3 tree. Walkthrough Insert 9, 7, 6, 1, 3, 2, 5, 4, 8, 10 13

14 More Examples Animation of 2-3 trees OpenDSA 14

15 Insert: Top-Down Steps Walks down tree in search of k. Whenever a 3-key node is encountered, split it by placing the middle key in the parent node. (Note that the parent has at most 2 keys, so it has room for the third.) 15 7 1,3,4 9 3, 7 1 9 4 Split 3-key

16 Example of Top-Down Insert 16 20, 40, 50 14 70, 79 32 43 10 18 2533 42 47 57, 62, 66 74 81 Insert 60 20 14 62, 70, 79 32 43 10 18 2533 42 47 57 74 81 50 40 66 Insert 60 here!

17 Observations on Insert Why we split 3-key nodes To make sure there is room for new key in leaf To make room for any key that’s kicked upstairs Sometimes insertion increases depth of tree by creating a new root, which is the only way a 2-3-4 tree can increase in height. 17

18 18 Analysis of Bottom-Up Insert Algorithm put(k, o) 1.We search for key k to locate the insertion node v 2.We add the new entry (k, o) at node v 3. while overflow(v) if isRoot(v) create a new empty root above v v  split(v) Let T be a 2-3-4 tree with n items Tree T has O(log n) height Step 1 takes O(log n) time because we visit O(log n) nodes Step 2 takes O(1) time Step 3 takes O(log n) time because each split takes O(1) time and we perform O(log n) splits Thus, an insertion in a (2,4) tree takes O(log n) time

19 19 Bottom-Up Deletion We reduce deletion of an entry to the case where the item is at the node with leaf children Otherwise, we replace the entry with its inorder successor (or, equivalently, with its inorder predecessor) and delete the latter entry Example: to delete key 24, we replace it with 27 (inorder successor) 27 32 35 10 15 24 2 81218 32 35 10 15 27 2 81218

20 20 Underflow and Fusion Deleting an entry from a node v may cause an underflow, where node v becomes a 1-node with one child and no keys To handle an underflow at node v with parent u, we consider two cases Case 1: the adjacent siblings of v are 2-nodes Fusion operation: we merge v with an adjacent sibling w and move an entry from u to the merged node v' After a fusion, the underflow may propagate to the parent u 9 14 2 5 710 u v 9 10 14 u v'v'w 2 5 7 Single key Many fusions possible

21 21 Underflow and Transfer To handle an underflow at node v with parent u, we consider two cases Case 2: an adjacent sibling w of v is a 3-node or a 4-node Transfer operation: 1. we move a child of w to v 2. we move an item from u to v 3. we move an item from w to u After a transfer, no underflow occurs 4 9 6 82 u vw 4 8 62 9 u vw Only one transfer during deletion

22 More Delete Examples (2,4) Trees22

23 Propagating Fusions 23

24 Delete: Top Down Steps for deleting key k Find key k  If it’s in leaf, remove it  It it’s internal node, replace it with entry with next higher key (always a leaf) Idea During search, eliminate 1-key nodes (except the root) so key can be removed from leaf w/o emptying it 24 It’s like “always delete a leaf node”

25 Rule 1 for Top-Down Delete Rule 1: Transfer When 1-key node (except root) is met, borrow a key from an adjacent sibling 25 2, 4 1 5,6,7 Transfer Subtrees have to be adjusted too! 3 2, 5 1 6,7 3, 4 Rule 1 is no good for this guy (No double rotation)

26 Rule 2 for Top-Down Delete Rule 2: Fusion If no adjacent sibling has more than 1 key, borrow a key from parent (Note that parent except root has at least 2 keys.) 26 2, 4 1 5 Fuse Subtrees have to be adjusted too! 3 4 1, 2, 3 5

27 Rule 3 for Top-Down Delete Rule 3: Fusion with root If parent is root and contains only one key, and sibling has only one key  Fuse into a 3-key node as the new root (and tree depth decrease by one) 27 2 1 3 Fuse 1, 2, 3

28 Example of Top-Down Delete Delete 40 28 20 14 62, 70, 79 32 43 10 18 2533 42 47 57, 60 74 81 50 40 66

29 Example of Top-Down Delete Delete 40 29 14 62, 70, 79 32 43 10 18 2533 42 47 57, 60 74 81 66 20, X, 50 Place holder to be filled with 42

30 Example of Top-Down Delete Delete 40 30 14 70, 79 32 43, 50 10 18 2533 42 47 57, 60 74 81 66 20, X, 62 Place holder to be filled with 42 Uplifted node Drop-down node Adjusted subtree

31 Example of Top-Down Delete Delete 40 31 14 70, 79 32 50 10 18 2533 42, 43, 47 57, 60 74 81 66 20, X, 62 Now we can move 42 to X

32 Example of Top-Down Delete Final result after deleting 40 32 14 70, 79 32 50 10 18 2533 43, 47 57, 60 74 81 66 20, 42, 62

33 33 Analysis of Bottom-Up Delete Let T be a 2-3-4 tree with n items Tree T has O(log n) height In a deletion operation We visit O(log n) nodes to locate the node from which to delete the entry We handle an underflow with a series of O(log n) fusions, followed by at most one transfer Each fusion and transfer takes O(1) time Thus, deleting an item from a 2-3-4 tree takes O(log n) time

34 34 Comparison of Map Implementations FindPutEraseNotes Hash Table 1 expected o no ordered map methods o simple to implement Skip List log n high prob. o randomized insertion o simple to implement AVL and 2-3-4 Tree log n worst-case o complex to implement


Download ppt "© 2004 Goodrich, Tamassia 1 2-3-4 Trees 9 10 14 2 5 7."

Similar presentations


Ads by Google