1 / 18

2-4 tree

2-4 tree. Definition search Insertion deletion. Office Hour: Monday 2:00-3:00 pm Thursday 10:00-11:00 am CSB 113. Definition. A 2-4 tree is an m-way search tree T in which an ordering is imposed on the set of keys which reside in each node such that:

chad
Download Presentation

2-4 tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2-4 tree • Definition • search • Insertion • deletion

  2. Office Hour: Monday 2:00-3:00 pm Thursday 10:00-11:00 am CSB 113

  3. Definition • A 2-4 tree is an m-way search tree T in which an ordering is imposed on the set of keys which reside in each node such that: • Each node has a maximum of 4 children and between 1 and 3 keys. • The keys in each node appear in ascending order. • The keys in the first i children are smaller than the ith key. • The keys in the last m-1 children are larger than the ith key. • All external nodes have the same depth.

  4. 6 12 7 8 5 10 15 3 4 11 13 14 17 Example

  5. Search • The keys in the first i children are smaller than the ith key. • The keys in the last m-i children are larger than the ith key

  6. 12 6 7 8 15 11 17 5 10 3 4 13 14 Search

  7. Insertion • Maintain the size and depth of the tree

  8. Insertion Algorithm inserItem(k,x) 1. We search for key k to locate the insertion node v 2. We add the new item (k,x) at node v 3. While overflow(v) if isRoot(v) Create a new empty root above v vsplit(v)

  9. split • Replace node v with two nodes v1 and v2 where: v1 is a 3-node with children v1, v2, v3 storing keys k1 and k2. v2 is a 2-node with children v4, v5, storing key k4. • If v was the root of the tree, create a new root node u; otherwise, let u be the parent of v. • Insert key k3 into u and make v1 and v2 children of u, so that if v was the ith child of u, then v1 and v2 become children i and i+1 of node u respectively.

  10. u v = u2 u3 u1 k1 k2 k3 k4 v1 v2 v3 v4 v5 h1 h1 h2 h2 u u h1 k3 h2 v = u2 u3 u1 v2 v1 k1 k2 k3 k4 u3 u1 k1 k2 k4 v1 v2 v3 v4 v5 v1 v2 v3 v4 v5 split

  11. 4 4 6 4 6 12 (a) (b) (c) 12 4 6 12 15 4 6 15 (d) (e) Example • Sequence of insertions is: 4, 6, 12, 15, 3, 5, 10, and 8.

  12. 12 12 3 4 6 15 3 4 5 6 15 (f) (g) 5 12 5 12 3 4 6 15 3 4 6 10 15 (h) (h) 5 12 3 4 6 8 10 15 (i)

  13. Example • http://www.cs.mcgill.ca/~cs251/ClosestPair/BalancedTreeApplet/BalancedTreeApplet.html

  14. Deletion • Reduce deletion of an item to the case where the item is at the node with leaf children • Otherwise, we replace the item with its inorder successor(or, equivalently, with its inorder predessor) and delete the latter item

  15. Underflow fusion transfer • Deleting an item from a node v may cause an underflow, where node v cecomdes 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 item from u to the merge v’ • After fusion, the underflow may progagate to the parent u • Case 2: an adjacent sibiling w of v is a 3-node or a 4-node • Transfer : • 1. we move a child 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

  16. 12 8 6 6 13 13 13 14 10 14 10 14 10 15 5 8 11 17 (a) Deletion of 12 causing an underflow at 11. 11 u 15 v 5 8 17 (b) Fusion operation. Move key of u to v. Merge v and sibling. 11 u 15 6 v 5 17 (c) 2-4 Tree after fusion operation completes. Fusion example

  17. u 5 7 5 25 25 25 v 4 7 30 30 30 7 37 15 37 37 15 58 18 18 58 58 (a) Deletion of 4 causes underflow at node v (it becomes a 1-node) u w v (b) Transfer operation. 3-node sibling w is identified. Key movement identified. u w v 5 15 18 (c) 2-4 Tree after transfer operation completes. Transfer example

  18. Exercise • Sequence of insertions is: 5, 3, 18, 15, 32, 6, 12, 46,23,77 Sequence of deletion is: 12,32,5,3,18,6,46,77,15,23

More Related