180 likes | 452 Views
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:
E N D
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: • 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.
6 12 7 8 5 10 15 3 4 11 13 14 17 Example
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
12 6 7 8 15 11 17 5 10 3 4 13 14 Search
Insertion • Maintain the size and depth of the tree
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)
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.
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
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 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)
Example • http://www.cs.mcgill.ca/~cs251/ClosestPair/BalancedTreeApplet/BalancedTreeApplet.html
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
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
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
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
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